Anyone have a sketch that uses the US for avoidance? I tried this code to find an obstacle <10 cm away, stop, turn right, measure, turn left, measure, then, depending on which distance is greater, if Left, go forward, if right, turn back and go forward, then LOOP.
The program turns right and left, but always goes left. I must be missing something, but it isn’t evident to me. Anyone see anything I missed? Thanks,
Brfl
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <MeOrion.h>
double varDistance;
double varDistRight;
double varDistLeft;
MeUltrasonicSensor ultrasonic_3(3);
MeDCMotor motor_9(9);
MeDCMotor motor_10(10);
void setup(){
}
void loop(){
varDistance = ultrasonic_3.distanceCm();
if((varDistance) > (10)){
motor_9.run(100);
motor_10.run(100);
}else{
motor_9.run(0);
motor_10.run(0);
_delay(0.5);
motor_9.run(100);
motor_10.run(-100);
_delay(0.75);
varDistRight = ultrasonic_3.distanceCm();
motor_9.run(-100);
motor_10.run(100);
_delay(1);
motor_9.run(0);
motor_10.run(0);
varDistLeft = ultrasonic_3.distanceCm();
if((varDistLeft) > (varDistRight)){
motor_9.run(100);
motor_9.run(100);
}else{
motor_9.run(100);
motor_9.run(-100);
_delay(1);
motor_9.run(100);
motor_9.run(100);
}
}
_loop();
}
void _delay(float seconds){
long endTime = millis() + seconds * 1000;
while(millis() < endTime)_loop();
}
void _loop(){
}