Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Ian St. Louis
Created December 7, 2015

Final Project

The summation of the last steps I took to put my car together.

Showcase (no instructions)32
Final Project

Story

Read more

Code

Bluetooth RC Car Control

Arduino
Controls two DC motors and a servo (for steering) using the Adafruit Bluetooth LE Module. Some of their sample code is utilized here as well.
// 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 enA = 8; // enable pin for the L298
int in1 = 7; // output pin 1
int in2 = 6; // output pin 2 for the motors
int enB = 1; // enable pin for the L298
int in3 = 5; // output pin 1
int in4 = 4; // 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 = 90; // 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 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)
        digitalWrite(in1, HIGH); // sets motor to forward.
        digitalWrite(in2, LOW);
        analogWrite(enA, 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)
        digitalWrite(in1, LOW); // sets motor to backwards.
        digitalWrite(in2, HIGH);
        analogWrite(enA, 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("Start ur engines"));

  uart.setRXcallback(rxCallback);
  uart.setACIcallback(aciCallback);
  uart.setDeviceName("cars"); //* 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);
  pinMode(enA, OUTPUT); // all three pins are output pins
  pinMode(in1, OUTPUT);
  pinMode(in2, 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

Ian St. Louis
12 projects • 5 followers

Comments