Below is the code I am using to control the robot but i want it to do certain commands for a certain ammount of time so that I can acheive this I want to be able to do the following of 1 command, is it easy to change this basic code below to do the following?
Start
Forward for x amount of time
Stop
Right for x amount of time
Stop
Forward for x amount of time
Stop
Right for x amount of time
Stop
Forward for x amount of time
Stop
Left for x amount of time
Stop
Forward for x amount of time
Stop
Add times in after testing
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
char command = '\0';
int pwm_speed; //from 0 - 255
void setup() {
// initialize the digital pin as an output.
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
}
void loop() {
char *help_menu = "'e' go forward\n'd' stop\n's' left\n'f' right\n'c' go backward\n";
if (Serial.available()>0)
{
command = Serial.read();
}
switch (command)
{
case 'e': //go forward
Serial.println("Go!!\r\n");
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 'c': //backward
Serial.println("Back!!\r\n");
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 's': //left
Serial.println("Left!!\r\n");
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 'f': //right
Serial.println("Right!!\r\n");
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 255); //PWM speed control
analogWrite(E2, 255); //PWM speed control
command = '\0'; //reset command
break;
case 'd': //stop
Serial.println("Stop!\r\n");
analogWrite(E1, 0); //PWM speed control
analogWrite(E2, 0); //PWM speed control
command = '\0'; //reset command
break;
case '\0':
//do nothing
break;
default:
Serial.println(help_menu);
command = '\0'; //reset command
break;
}
}