I'm trying to jump start the motors with a brief high voltage then have them run at a slow speed when the ping time is above 500. When ping time is below 500 I'm trying to again jump start the motors with a brief high voltage in the opposite direction, then move backwards a bit at an angle for a few seconds. More or less how the first generation of Roombas ran.
The problem I'm running into is that when ping time is above 500 the motors only run at a very high speed. When I delete the conditional if statement, essentially taking the ultrasonic sensor out of the picture it works as I intend it to at least in 1 direction. Jump starting then running at a slow speed.
int trigPin=12;
int echoPin=11;
int pingTravelTime;
int motor1pin1=2;
int motor1pin2=4;
int motor2pin1=7;
int motor2pin2=8;
int motor1speedcontroller=6;
int motor2speedcontroller=3;
void setup() {
// put your setup code here, to run once:
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(motor1pin1,OUTPUT);
pinMode(motor1pin2,OUTPUT);
pinMode(motor2pin1,OUTPUT);
pinMode(motor2pin2,OUTPUT);
pinMode(motor1speedcontroller,OUTPUT);
pinMode(motor2speedcontroller,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin,LOW);
delayMicroseconds(10);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
pingTravelTime=pulseIn(echoPin,HIGH);
delay(25);
Serial.println(pingTravelTime);
if (pingTravelTime > 500){
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, HIGH);
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, HIGH);
analogWrite(motor2speedcontroller,255);
analogWrite(motor1speedcontroller,255);
delay(5);
analogWrite(motor2speedcontroller,22);
analogWrite(motor1speedcontroller,27);
}
if (pingTravelTime < 500){
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
analogWrite(motor2speedcontroller,255);
analogWrite(motor1speedcontroller,255);
delay(5);
analogWrite(motor2speedcontroller,22);
analogWrite(motor1speedcontroller,35);
delay(2000);
}
}