I've assembled the kit, connected all the wires and ran a few examples with the board. All successful. Specifically, I tested the example given starting from Page 10 here and it works. I also made an alternate version which makes the 4WD run a predefined path.
// Everything from the previous program except the 'loop' functions remain identical
void loop(void)
{
forward (10000, 10000);
delay (100);
turnright (10000, 10000);
delay (100);
forward (10000, 10000);
stop ();
}
I also was able to successfully run the ping test example, so my Devantech SRF05 sensor is working fine.
The program comes when I try to write a program such that the robot moves forward and prints the changing ping as it advances towards whatever obstacle it's seeing.
// Assign wheels to digital I/O channels
int E1 = 5;
int E2 = 6;
int M1 = 4;
int M2 = 7;
int Sensor = 2;
// Stop movement
void stop (void){
digitalWrite (E1, LOW);
digitalWrite (E2, LOW);
}
// Move forward
void forward (char a, char b){
analogWrite (E1, a);
analogWrite (E2, b);
digitalWrite (M1, LOW);
digitalWrite (M2, LOW);
}
// Move backward
void backward (char a, char b){
analogWrite (E1, a);
analogWrite (E2, b);
digitalWrite (M1, HIGH);
digitalWrite (M2, HIGH);
}
// Turn left
void turnleft (char a, char b){
analogWrite (E1, a);
analogWrite (E2, b);
digitalWrite (M1, HIGH);
digitalWrite (M2, LOW);
}
// Turn right
void turnright (char a, char b){
analogWrite (E1, a);
analogWrite (E2, b);
digitalWrite (M1, LOW);
digitalWrite (M2, HIGH);
}
// Convert microseconds to centimeters
long microsecondsToCentimeters (long microseconds)
{
return microseconds / 29 / 2;
}
// Initialise
void setup (void){
int i;
for (i = 6; i <= 9; i++)
pinMode (i, OUTPUT);
Serial.begin (19200);
}
// Read input
void loop (void){
long duration, cm;
pinMode(Sensor, OUTPUT);
digitalWrite(Sensor, LOW);
delayMicroseconds(2);
digitalWrite(Sensor, HIGH);
delayMicroseconds(5);
digitalWrite(Sensor, LOW);
pinMode(Sensor, INPUT);
duration = pulseIn(Sensor, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print(" cm");
Serial.println();
delay (1000);
forward (10000, 10000);
}
The distance is being printed on the serial monitor, but the 4WD doesn't move and a continuous beep sound can be heard, which I take to mean an error.
Can someone explain to me what I'm doing wrong? Thanks in advance. ^^;