Things used in this project

Code

Bluetooth RC Car ControlC/C++
Uses Adafruit Bluetooth LE chip and code with some edits to control two dc motors and a microservo, allowing the car to go forwards, backwards, left and right.
// 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];

// initializing my components
int enB = 6; // enable pin for the L298
int in3 = 7; // output pin 1
int in4 = 8; // output pin 2 for the motors
#include <Servo.h> // include my servo library
Servo myservo;  // create servo object to control a servo

   // initial servo position
int pos = 85; // for my particular motor, the range is exactly from 0-180 degrees. i want the servo to start in the middle


/**************************************************************************/
/*!
    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: "));

  // constantly check if these are happening
      switch(buffer[2]) {
      case 53:
        Serial.println ( "up");
        digitalWrite(in3, HIGH); // sets motor to forward.
        digitalWrite(in4, LOW);
        analogWrite(enB, 255); // write enable pin to highest speed (0-255)
        break; // will stop the code so that the following code in the loop does not run
      case 54:
        Serial.println ( "down");
        digitalWrite(in3, LOW); // sets motor to backwards.
        digitalWrite(in4, HIGH);
        analogWrite(enB, 255); // write enable pin to highest speed (0-255)
        break;
      case 55: // if this buffer[2] == 55, turn left by setting servo position to 160 degrees, or increment by 5
        Serial.println ( "left");
        pos +=5;
        myservo.write(pos);
//      myservo.write(160);         
        break;
      case 56: // if this buffer[2] == 56, turn right by setting servo position to 20 degrees
        Serial.println ( "right");        
        pos -= 5;
        myservo.write(pos);
//        myservo.write(20);
        break;
    }


     
  /* 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("NEWNAME"); /* 7 characters max! */
  uart.begin();

// setup the pinmodes for the L298 Motor controller
  pinMode(enB, OUTPUT); // all three pins are output pins
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  // attaches the servo on pin 3 to the servo object
  myservo.attach(3); // initialize servo on pin 3
  
}

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


void loop()
{
  uart.pollACI();

}

Credits

11391511 1607839886097766 5764443667056288811 n
Ian St. Louis
12 projects • 2 followers
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog