Robo Race / Robo Soccer
This code Below is the Perfect Code for controlling Arduino Bots.
REMEMBER : if you know how to do connections then this code is an elixir for you because unlike previous posts i have provided circuit diagram for connections, but this time this code is prepared by me and i made connections through understanding of code itself.
Enjoy!!
**-----new code starts here, the 4 motor code smooth working-----**
int m1a = 9; // Motor 1, Pin A
int m1b = 10; // Motor 1, Pin B
int m2a = 11; // Motor 2, Pin A
int m2b = 12; // Motor 2, Pin B
char val;
void setup() {
pinMode(m1a, OUTPUT);
pinMode(m1b, OUTPUT);
pinMode(m2a, OUTPUT);
pinMode(m2b, OUTPUT);
Serial.begin(9600);
}
void loop() {
while (Serial.available() > 0) {
val = Serial.read();
Serial.println(val);
}
if (val == 'F') { // Forward
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
} else if (val == 'B') { // Backward
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
} else if (val == 'L') { // Left (pivot left)
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
} else if (val == 'R') { // Right (pivot right)
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
} else if (val == 'S') { // Stop
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
} else if (val == 'I') { // Forward Right
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
} else if (val == 'J') { // Backward Right
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
} else if (val == 'G') { // Forward Left
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
} else if (val == 'H') { // Backward Left
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
}
}
**-----new code ends here-----**
**--------old code starts here-------**
int m1a = 9;
int m1b = 10;
int m2a = 11;
int m2b = 12;
char val;
void setup()
{
pinMode(m1a, OUTPUT); // Digital pin 10 set as output Pin
pinMode(m1b, OUTPUT); // Digital pin 11 set as output Pin
pinMode(m2a, OUTPUT); // Digital pin 12 set as output Pin
pinMode(m2b, OUTPUT); // Digital pin 13 set as output Pin
Serial.begin(9600);
}
void loop()
{
while (Serial.available() > 0)
{
val = Serial.read();
Serial.println(val);
}
if( val == 'F') // Forward
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
}
else if(val == 'B') // Backward
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
}
else if(val == 'L') //Left
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
}
else if(val == 'R') //Right
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
else if(val == 'S') //Stop
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
else if(val == 'I') //Forward Right
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
else if(val == 'J') //Backward Right
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
else if(val == 'G') //Forward Left
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH); digitalWrite(m2b, LOW);
}
else if(val == 'H') //Backward Left
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
}
}
**-------old code ends here------**