Hi Sparkers,
I made the very first prototype of my internet controlled Car. Because Im lack of knowledge in electronics, it was a little bit hard to find out how to reverse dc motor direction. Luckly I have heard about motor controllers before and after juggling with transistors I have ended up with L293D today.
Here is a video of my first demo:
It’s a mess of cables!
Firmware code:
/* A Spark function to parse the commands */
int rcCarControl(String command);
/* Globals -------------------------------------------------------------------*/
int leftMotor = A6;
int rightMotor = A7;
int forwardMotor = D6;
int backwardMotor = D7;
void setup()
{
//Register Spark function
Spark.function("rccar", rcCarControl);
pinMode(forwardMotor, OUTPUT);
pinMode(backwardMotor, OUTPUT);
pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
}
void loop()
{
// Nothing to do here
}
//Using a bit to store commands
// 1 : LEFT, 5 : FORWARD and LEFT
// 0000 0001, 0000 0101
const byte LEFT = 1;
const byte RIGHT = 2;
const byte FORWARD = 4;
const byte BACKWARD = 8;
int rcCarControl(String command)
{
byte cmd = command.toInt();
if(cmd == 0){
digitalWrite(forwardMotor,LOW);
digitalWrite(backwardMotor,LOW);
digitalWrite(leftMotor,LOW);
digitalWrite(rightMotor,LOW);
return 1;
}
//eg: cmd = 0110 -> 0110 & 0100 = 0100 -> true
if((cmd & FORWARD) == FORWARD)
{
digitalWrite(backwardMotor,LOW);
digitalWrite(forwardMotor,HIGH);
}
else if((cmd & BACKWARD) == BACKWARD)
{
digitalWrite(forwardMotor,LOW);
digitalWrite(backwardMotor,HIGH);
}
else{
digitalWrite(forwardMotor,LOW);
digitalWrite(backwardMotor,LOW);
}
if((cmd & LEFT) == LEFT)
{
digitalWrite(rightMotor,LOW);
digitalWrite(leftMotor,HIGH);
}
else if((cmd & RIGHT) == RIGHT)
{
digitalWrite(leftMotor,LOW);
digitalWrite(rightMotor,HIGH);
}
else
{
digitalWrite(leftMotor,LOW);
digitalWrite(rightMotor,LOW);
}
return 1;
}