Andres Duarte
Published

Final Project

arduino bluetooth vehicle (final designed chassis)h

BeginnerProtip9,299
Final Project

Things used in this project

Story

Read more

Schematics

schematic

Code

bluetooth motor control for vehicle from modified callback echo

Arduino
/*********************************************************************
This is an example for our nRF8001 Bluetooth Low Energy Breakout

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/1697

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Kevin Townsend/KTOWN  for Adafruit Industries.
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in any redistribution
*********************************************************************/

// This version uses call-backs on the event and RX so there's no data handling in the main loop!

#include <SPI.h>
#include "Adafruit_BLE_UART.h"

#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST 9

Adafruit_BLE_UART uart = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

boolean messageReceived = false;
uint8_t message[20];

// L298 dual H-bridge to Arduino connections 

// DC Motor 1
int M1_1 = 6; // M1_1 connected to digital pin 6 
int M1_2 = 5; // M1_2 connected to digital pin 5 

// DC Motor 2
int M2_1 = 4; // M2_1 connected to digital pin 4 
int M2_2 = 3; // M2_2 connected to digital pin 3 

// defines button input 
char button = '0';

/**************************************************************************/
/*!
    Motor control functions
*/
/**************************************************************************/

void up() {
  digitalWrite (M1_1, HIGH);
  digitalWrite (M1_2, LOW);
  digitalWrite (M2_1, LOW);
  digitalWrite (M2_2, HIGH);
}

void left() {
  digitalWrite (M1_1, HIGH); 
  digitalWrite (M1_2, LOW);
  digitalWrite (M2_1, HIGH);
  digitalWrite (M2_2, LOW);
}

void right() {
  digitalWrite (M1_1, LOW);
  digitalWrite (M1_2, HIGH); 
  digitalWrite (M2_1, LOW); 
  digitalWrite (M2_2, HIGH);
}

void down() {
  digitalWrite (M1_1, LOW);
  digitalWrite (M1_2, HIGH);
  digitalWrite (M2_1, HIGH);
  digitalWrite (M2_2, LOW);
}

void stopp() {
  digitalWrite (M1_1, LOW); 
  digitalWrite (M1_2, LOW);
  digitalWrite (M2_1, LOW);
  digitalWrite (M2_2, LOW);
}


/**************************************************************************/
/*!
    This function is called whenever select ACI events happen
*/
/**************************************************************************/
void aciCallback(aci_evt_opcode_t event)
{
  switch(event)
  {
    case ACI_EVT_DEVICE_STARTED:
      Serial.println(F("Advertising started"));
      break;
    case ACI_EVT_CONNECTED:
      Serial.println(F("Connected!"));
      break;
    case ACI_EVT_DISCONNECTED:
      Serial.println(F("Disconnected or advertising timed out"));
      break;
    default:
      break;
  }
}

/**************************************************************************/
/*!
    This function is called whenever data arrives on the RX channel
*/
/**************************************************************************/
void rxCallback(uint8_t *buffer, uint8_t len)
{
  Serial.print(F("Received "));
  Serial.print(len);
  Serial.println(F(" bytes: "));
  for(int i=0; i<len; i++)
   Serial.print((char)buffer[i]); 

  Serial.print(F(" ["));

  for(int i=0; i<len; i++)
  {
    Serial.print(" 0x"); Serial.print((char)buffer[i], HEX); 
  }
  Serial.println(F(" ]"));


  // vehicle control

   if ((char)buffer[1] == 'B') { // char is a  data type that takes up 1 byte of memory that stores a character value
    if((char)buffer[3] == '0') {
      button = '0';    //creates a state when no buttons are pressed so that the car stops instead of continuously performing the task of the last button pressed 
    } else {
      button = (char)buffer[2];     // selects direction, which creates a specific motor output.
    }
  }

  if (button == '0') {
    stopp();                  // stop  
  } else if (button == '5') { // up
    up();
  } else if (button == '6') { // down 
    down();
  } else if (button == '7') { // left 
    left();
  } else if (button == '8') { // right 
    right();
  }


  /* Echo the same data back! */
  uart.write(buffer, len);
}

/**************************************************************************/
/*!
    Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void)
{ 
  Serial.begin(9600);
  while(!Serial); // Leonardo/Micro should wait for serial init
  Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Callback Echo demo"));

  uart.setRXcallback(rxCallback);
  uart.setACIcallback(aciCallback);
  uart.setDeviceName("GABO"); /* 7 characters max! */
  uart.begin();

  pinMode(M1_1, OUTPUT); // makes it so that pins connected to h-bridge work as outputs
  pinMode(M1_2, OUTPUT);
  pinMode(M2_1, OUTPUT);
  pinMode(M2_2, OUTPUT);
}

/**************************************************************************/
/*!
    Constantly checks for new events on the nRF8001
*/
/**************************************************************************/


void loop()
{
  uart.pollACI();
  
}

Credits

Andres Duarte

Andres Duarte

14 projects • 8 followers

Comments