Hey Guys, I currently have an Arduino duemilanove connected to a Polulu dual-serial low voltage motor controller.
I’m trying to make a robot that goes forward and turns left if it gets too close to a wall, below is the code involved.
void loop()
{
val_dist = analogRead(dist_sensor); // read the value from the sensor
val_dist = convDistance(val_dist);
Serial.print("Distance from wall is :");
Serial.print(val_dist);
Serial.println(" cm");
if (val_dist > 10)
{
moveForward();
}
else
{
turnLeft();
}
}
void moveForward()
{
Serial.println("MOVING FORWARD");
// Create the packet buffer
unsigned char buffer[4];
buffer[0] = 0x80;
buffer[1] = 0x00;
buffer[2] = 0x04;
buffer[3] = 0x7F;
SendPacket(buffer, 4);
buffer[0] = 0x80;
buffer[1] = 0x00;
buffer[2] = 0x06;
buffer[3] = 0x7F;
SendPacket(buffer, 4);
delay(1000);
}
void turnLeft()
{
Serial.println("TURNING LEFT");
unsigned char buffer[4];
buffer[0] = 0x80;
buffer[1] = 0x00;
buffer[2] = 0x04;
buffer[3] = 0x7F;
SendPacket(buffer, 4);
buffer[0] = 0x80;
buffer[1] = 0x00;
buffer[2] = 0x07;
buffer[3] = 0x7F;
SendPacket(buffer, 4);
delay(1000);
}
The code works initially and the robot keeps going straight. However, once it nears the wall and the distance is less than 10cm, it just brakes and stop there. But in the serial monitor is is printing “TURNING LEFT” which means it is in the turnLeft() function and the data is being sent. I don’t understand why is it stopping. Can anybody enlighten me?? Thanks!