jsusnik7:
What type of connection is required - Digital or Analog? Depending on your reply it seems that digital, but it would be possible then to control car's speed?
Nice project and yes, my main reason for rebuilding RC Car is Bluetooth controlling with Andoid app
Thank you.
It can be connected using an analog signal "analogWrite(PIN, 0 - 255)" this will give you speed control. Or you can have one constant speed in which a simple digital HIGH or LOW will work too.
If your using an Arduino UNO, then your PWM pins are shown by the ~ symbol on the board. If you are not using a UNO, then your PWM pins are 3,5,6,9,10,11.
This was the second code I made for my robot, a revision adding in different speeds. This should give you an Idea as to what you can do. But its better to send a string of bytes and not simple characters. I have code for that too, but for now try this.
/*
simple control test
*/
volatile int gear = 150; //default speed First Gear
volatile char val; // variable to receive data from the serial port
byte M1L = 3; // PWM left drive motor
byte M2L = 5; // PWM left drive motor
byte M1R = 9; // PWM right drive motor
byte M2R = 6; // PWM right drive motor
byte ledpin = 13; // LED connected to pin 2 (on-board LED)
/*IMPORTANT*/
//M1 => High & M2 => Low: forward movement
//M1 => Low & M2 => High: Backward movement
void setup()
{
pinMode(ledpin, OUTPUT); // pin 13 (on-board LED) as OUTPUT
pinMode(M1L, OUTPUT);
pinMode(M1R, OUTPUT);
pinMode(M2L, OUTPUT);
pinMode(M2R, OUTPUT);
Serial.begin(9600); // start serial communication at 115200bps
Help();
}
void loop() {
if( Serial.available() > 0) // if data is available to read
{
digitalWrite(ledpin, HIGH); // this will turn on the LED if you receive a command
val = Serial.read();
volatile int skewGear = gear/2;
if(val == '1')
{
Serial.println("First Gear ");
gear = 150;
}
if (val == '2')
{
Serial.println("Second Gear ");
gear = 200;
}
if (val == '3')
{
Serial.println("Third Gear ");
gear = 255;
}
if( val == 'W' || val == 'w' )//forwards
{
Serial.println("Moving Forwards");
// Left Motor : Right Motor
analogWrite(M1L, gear); analogWrite(M1R, gear);
digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);
}
else if( val == 'S' || val == 's' )//backwards
{
Serial.println("Moving Backwards");
digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);
analogWrite(M2L, gear); analogWrite(M2R, gear);
}
else if( val == 'A' || val == 'a' )//Left
{
Serial.println("LEFT");
digitalWrite(M1L, LOW); analogWrite(M1R, gear);
analogWrite(M2L, gear); digitalWrite(M2R, LOW);
}
else if( val == 'D' || val == 'd' )
{
Serial.println("RIGHT");
analogWrite(M1L, gear);digitalWrite(M1R, LOW);
digitalWrite(M2L, LOW); analogWrite(M2R, gear);
}
else if( val == 'T' || val == 't' )
{
Serial.println("Skew Left Forward");
analogWrite(M1L, skewGear); analogWrite(M1R, gear);
digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);
}
else if( val == 'Y' || val == 'y' )
{
Serial.println("Skew Right Forward");
analogWrite(M1L, gear); analogWrite(M1R, skewGear);
digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);
}
else if( val == 'G' || val == 'g' )
{
Serial.println("Skew Left Backward");
digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);
analogWrite(M2L, skewGear); analogWrite(M2R, gear);
}
else if( val == 'H' || val == 'h' )
{
Serial.println("Skew Right Backward");
digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);
analogWrite(M2L, gear); analogWrite(M2R, skewGear);
}
else if(val == ' ' || val == 'x' || val == 'X') // motors will stop if space bar is hit or x|X
{
digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);
digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);
}
else if(val == '*') {
Help();
}
else{}
}
else{
digitalWrite(ledpin, LOW);
}
}
void Help() {
Serial.println("Commands, Press * to bring up help menu");
Serial.println("W = Forward");
Serial.println("S = Backward");
Serial.println("A = Left");
Serial.println("D = Right");
Serial.println("T = Skew Forward Left");
Serial.println("Y = Skew Forward Right");
Serial.println("G = Skew Backward Left");
Serial.println("H = Skew Backward Right");
Serial.println("Space Bar = All stop");
return;
}