Aurelius C project- Part 2 Arduino Code

Visit Part 1 for circuit design for the RC car on Arduino.

The first thing you want to do in your Arduino Sketch is to declare 4 ints with your desired pins for the motor connection.My current RC design is in the example below, but you can use any digital pin you want.

int ina = 3;  // front
int inb = 4;  //front motor
int inc = 6;  //rear
int ind = 7;  //rear motor
char data;    //variable to receive data from serial port

Once we’ve declared all the variable we need we’ll complete the setup function. In here we basically just declare what we want out pins that was declared above to be(input or output) and set the baud rate.

void setup()
{
//initialize digital pins as output
pinMode(ina,OUTPUT);
pinMode(inb,OUTPUT);
pinMode(inc,OUTPUT);
pinMode(ind,OUTPUT);

//start serial communication @ 9600bps
Serial.begin(9600);
}

Next what we have to do is create the functions for all the possible vehicle controls (left,right,forward, reverse etc.) This will vary depending on how you hook up the motor and pins up but simple enough to find out. Basically you write to the digital pin whether you want it on (HIGH) or off(LOW). play around with it to make sure all your commands work perfectly.

void forward()
{
digitalWrite(ina,HIGH);
digitalWrite(inb,LOW);
}
void reverse()
{
digitalWrite(ina,LOW);
digitalWrite(inb,HIGH);
}

void stopforward()
{
digitalWrite(ina,LOW);
}
void stopreverse()
{
digitalWrite(inb,LOW);
}

void left()
{
digitalWrite(inc,HIGH);
digitalWrite(ind,LOW);
}
void right()
{
digitalWrite(inc,LOW);
digitalWrite(ind,HIGH);
}

void noturn()
{
digitalWrite(inc,LOW);
digitalWrite(ind,LOW);
}

void stopcar()
{
digitalWrite(ina,LOW);
digitalWrite(inb,LOW);
digitalWrite(inc,LOW);
digitalWrite(ind,LOW);
}

Next were going to write an actioncommand function. This is where the char ‘data’ comes into play. We will create two IF-Statements, first one checking if the serial communication is available and second one finding what car control to perform. An example is if you send the character ‘f’ through the serial monitor it’ll perform the forward function we built above. This is how we peform the actions.
void actioncommand()
{
if(Serial.available())
{
data= Serial.read();
}
if(data==’f’){forward();}
else if(data==’b’){reverse();}
else if(data==’l’){left();}
else if(data==’r’){right();}
else if(data==’a’) {stopforward();}
else if(data==’b’){stopreverse();}
else if(data==’t’){noturn();}
else  if(data==’s’){stopcar();}
}
[/code]
Finally to make it all work we call the actioncommand function within the loop.

void loop()
{
actioncommand();
}

When uploading the sketch make sure you don’t have your bluetooth module connected as it won’t upload your sketch properly. Once uploaded connect the bluetooth and download a simple bluetooth terminal from the app store or Play market. Pair the two and have fun playing with an rc car using your phone.

Next Project: I will probably sometime down the line make a specific Android app using accelerator and such to make it more fun to control rather than sending in characters.