I'm trying to make a robot that when it gets under 21 centimeters away from something, it backs up, turns around and keeps going. I keep getting error messages, could someone please help?
#include <Servo.h>
Servo leftmotor;
Servo rightmotor;
int pwmL,pwmR;
int pingPin = 3;
int threshold = 21;
int cm = 0;
void setup()
{
leftmotor.attach(9); // af-motorshield servo 2
rightmotor.attach(10); // af-motorshield servo 1
Serial.begin(9600);
}
void loop()
{
leftmotor.write(pwmL -180); // for use with continuous motor
rightmotor.write(pwmR +180);
long duration, inches, cm;
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);
pinMode(pingPin,INPUT);
duration = pulseIn(pingPin,HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(10);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
if(cm < threshold){
leftmotor.write(pwmL +180);
rightmotor.write(pwmR -180);
delay(500);
leftmotor.write(pwmL +180);
rightmotor.write(pwmR +180);
delay(1000);
}else{
leftmotor.write(pwmL -180);
rightmotor.write(pwmR +180);
}
}
I keep getting error messages, could someone please help?
Without seeing them? No.
Though it's a bit of a mystery why you added code AFTER the return statement in that function. Seems to indicate that you don't comprehend what return does.
I attached the ping to a project I'm making and tried to program it so that when it gets within 21 cm of something, it backs up, turns around and keeps going.
The code doesn't get any error messages,but when i put my hand or any other object within 21 cm of the pin, it just keeps going forward.
I'm new at coding, could someone please tell me where i went wrong.
#include <Servo.h>
Servo leftmotor;
Servo rightmotor;
int pwmL,pwmR;
int pingPin = 3;
int threshold = 21;
int cm = 0;
void setup()
{
leftmotor.attach(9); // af-motorshield servo 2
rightmotor.attach(10); // af-motorshield servo 1
Serial.begin(9600);
}
void loop()
{
leftmotor.write(pwmL -180); // for use with continuous motor
rightmotor.write(pwmR +180);
long duration, inches, cm;
pinMode(pingPin,OUTPUT);
digitalWrite(pingPin,LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);
pinMode(pingPin,INPUT);
duration = pulseIn(pingPin,HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(10);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
if(cm < threshold){
leftmotor.write(pwmL +180);
rightmotor.write(pwmR -180);
delay(500);
leftmotor.write(pwmL +180);
rightmotor.write(pwmR +180);
delay(1000);
}else{
leftmotor.write(pwmL -180);
rightmotor.write(pwmR +180);
}
}
sorry, i tried again and i didnt get any error messages but when something gets within 21 cm it doesn't turn.
I'm new to coding, could you please explain what you mean about the coding before the return.