Can some one please tell me what is wrong with my code. I am trying to make an Arduino robot that avoids obstacles using a ping sensor and two dc motors attached to the motor shield.
Here is the code: Ps: I combined two codes and edited it a little bit
#include <Streaming.h>
#define cout Serial
#define endl '\n'
#define pingPin 4 // Seeed ultrasonic sensor on pin D4.
#define BOUNDARY 30 // (cm) Avoid objects closer than 30cm.
#define INTERVAL 25 // (ms) Interval between distance readings.
// at 250 cm/s that's 6 cm
volatile unsigned long notstalled; // last time we know the robot was moving
const int
PWM_A = 3,
DIR_A = 12,
BRAKE_A = 9,
SNS_A = A0;
void setup() {
// Configure the A output
pinMode(BRAKE_A, OUTPUT); // Brake pin on channel A
pinMode(DIR_A, OUTPUT); // Direction pin on channel A
// Open Serial communication
Serial.begin(9600);
Serial.println("Motor shield DC motor Test:\n");
}
void loop() {
// Set the outputs to run the motor forward
digitalWrite(BRAKE_A, LOW); // setting brake LOW disable motor brake
digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward
analogWrite(PWM_A, 255); // Set the speed of the motor, 255 is the maximum value
delay(5000); // hold the motor at full speed for 5 seconds
Serial.print("current consumption at full speed: ");
Serial.println(analogRead(SNS_A));}
delay(INTERVAL); // Delay between readings.
distance = readDistance(); // Take a distance reading.
cout <<" f"<<distance;
Serial.println(distance); // Print it out.
while((distance >= BOUNDARY)&& //loop til an object is sensed
((millis()-notstalled)<1000)); //or the wheels stop moving
if ((millis()-notstalled)<=1000){ //if it's just an object
cout <<endl<<"bkp "<<endl;
backward(); // Move backward 750ms.
delay(750);
leftTurn(300);} // Turn left 300ms.
else{ //robot has stalled
cout<< '\n' <<"stalled" << '\n';
backward(); delay(1000); //longer backup
leftTurn(500); // bigger turn
}
}// end Main program
Moderator edit: Topic split from hijacked thread, title changed. Please use CODE TAGS when posting code. AWOL.