I'm playing around with the rover kit from DF Robot Shop (http://www.robotshop.com/productinfo.aspx?pc=rb-rbo-33). I'm running the code essentially as described in the rover's instruction manual.
I notice that when I give the command for the rover to turn right or left, it spins around in clockwise or counterclockwise circles, respectively. Rather than spinning around endlessly, I really want it to spin 30 degrees left or right, stop, and wait for the next command.
The kit comes with what look like pretty basic 5V DC motors. I think I need to re-write the code so that the motors only spin for 300 or 400 milliseconds before stopping and waiting for the next command. I'm not sure how to do that. I've looked at some of the "timer" libraries on the Arduino forum but they dont' really appear to be what I'm looking for.
Here's the code I'm running. Any general suggestions would be great!
//Control the Rover with four over-serial commands: left, right, forward, backwards.
//Modified the forward, left, right and backward commands to make the rover go
//in the desired direction.
int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control
void setup (void)
{
int i;
for(i=5;i<=8;i++)
pinMode(i, OUTPUT);
Serial.begin(9600);
}
void loop(void)
{
while (Serial.available() < 1) {} // Wait until a character is received
char val = Serial.read();
int leftspeed = 250; //255 is maximum speed
int rightspeed = 255;
switch(val) // Perform an action depending on the command
{
case 'f'://Move Forward
forward (leftspeed,rightspeed);
break;
case 'b'://Move Backwards
reverse (leftspeed,rightspeed);
break;
case 'l'://Turn Left
left (leftspeed,rightspeed);
break;
case 'r'://Turn Right
right (leftspeed,rightspeed);
break;
default:
stop();
break;
case 's'://stop
halt (leftspeed,rightspeed);
break;
}
}
void stop (void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void forward(char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite(E2,b);
digitalWrite(M2,HIGH);
}
void reverse (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite(E2,b);
digitalWrite(M2,LOW);
}
void left (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void right (char a,char b)
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void halt (char a,char b)
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}