Punch Through
Published © MIT

Bean Bus

Drive a tiny RC bus over Bluetooth Low Energy with the LightBlue Bean!

AdvancedFull instructions provided1,014
Bean Bus

Things used in this project

Hardware components

LightBlue Bean
Punch Through LightBlue Bean
×1
RC Car
×1

Software apps and online services

LightBlue App

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

Code snippet #1

Plain text
/*
Bean RC Car Sketch
 
This uses the RC car example at http://legacy.punchthrough.com/bean/?page_id=5157
and needs the LightBlue App
*/
 
//Define the pins for the motor controllers
int mForward = 0;
int mReverse = 1;
int mRight = 2;
int mLeft = 3;
 
//define constant for full PWM signal
#define MOTOR_HIGH 255
 
//Define inputs for LightBlue
#define XYPAD_X 8
#define XYPAD_Y 9
 
 
//Variables to store the sandbox inputs
static uint8_t gas;
static uint8_t steering;
 
void setup()
{
 //Setup pins
 pinMode(mReverse, OUTPUT);
 pinMode(mForward, OUTPUT);
 pinMode(mLeft, OUTPUT);
 pinMode(mRight, OUTPUT);
 
 //Need serial for LightBlue
 Serial.begin(57600);
 Serial.setTimeout(5);
}
 
 
void loop()
{
 /*
 Steering via sanbox controls.
 The gas is set proportionally with a dead zone,
 while steering is either off, left or right (the car was designed to stall the steering motor when turning).
 
 Gas:
 1 --------- 100 160 --------- 255
 Reverse stop forward
 
 Stering:
 1 --------- 100 160 --------- 255
 left stop right
 
 */
 
 //Create a buffer to recieve from LightBlue, along with length
 char rec_buffer[64];
 size_t rec_length = 64;
 
 //Set the length to number of bytes recieved
 rec_length = Serial.readBytes(rec_buffer, rec_length);
 
 if ( rec_length > 0 )
 {
   Bean.setLed(255,255,255);
   // There may be both X and Y values read in
   // a single packet. Handle this case.
   // Byte[0] : X/Y control #
   // Byte[1] : Value [0,255]
   for (int i = 0; i < rec_length - 1; i += 2 )
   {
     if ( rec_buffer[i] == XYPAD_X )
     {
       steering = rec_buffer[i + 1];
     }
     else if ( rec_buffer[i] == XYPAD_Y )
     {
       gas = rec_buffer[i + 1];
     }
   }
 }
 else{
   Bean.setLed(0,0,0);
 }
 
 //Setup steering control and deadzones
 if (steering < 100) {
   //left
   analogWrite(mRight, 0);
   analogWrite(mLeft, MOTOR_HIGH);
 }
 else if (steering > 160) {
   //right
   analogWrite(mRight, MOTOR_HIGH);
   analogWrite(mLeft, 0);
 }
 else {
   //straight
   analogWrite(mRight, 0);
   analogWrite(mLeft, 0);
 }
 
 
 if (gas < 120) {
   //forward (inverted)
   analogWrite(mForward, 255 - gas);
   analogWrite(mReverse, 0);
 }
 else if (gas > 136) {
   //reverse
   analogWrite(mForward, 0);
   analogWrite(mReverse, gas);
 }
 else {
   //stop
   analogWrite(mForward, 0);
   analogWrite(mReverse, 0);
 }
}

Credits

Punch Through

Punch Through

16 projects • 40 followers
We’ve been building connected products since 2009. Our diverse team has expertise in every layer from hardware to software to web.

Comments