Having some trouble controlling dc motors

I’m working on this car that detects the edge using IR sensors.

The idea is for it to go forward until it gets to an edge, then back up for 2 seconds and turn one way or the other for a second depending on which sensor reads high.

I have two different sketches that I think are pretty close but I can not seem to get right. If anyone could help me out I would really appreciate it.

This one is using delay for the reverse and turns and the problem is that it reverses for much longer than 2 seconds (close to 20 seconds) and turns for much longer than 1 second.

int motor_left[] = {10, 9};                        
int motor_right[] = {7, 8};                         

int IRpin  = 13;                                         
int IRpin1 = 6;                                          
int IRpin2 = 12;                                        
int IRpin3 = 5;                                         
int ledpin = 11;                                        
int ledpin1 = 4;                                       
int IRvalue = 0;      
                                
int IRvalue1 = 0;                                    
int IRvalue2 = 0;                                   
int IRvalue3 = 0;                                   

// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);                                   
  pinMode(ledpin,  OUTPUT); 
  pinMode(ledpin1, OUTPUT);
  pinMode(IRpin,   INPUT);  
  pinMode(IRpin1,  INPUT);
  pinMode(IRpin2,  INPUT);
  pinMode(IRpin3,  INPUT);
// Setup motors
int i;
for(i = 0; i < 2; i++){                                                                   
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}

// --------------------------------------------------------------------------- Loop

void loop() { 
  
IRvalue = digitalRead(IRpin);                                                     
IRvalue1 = digitalRead(IRpin1);
IRvalue2 = digitalRead(IRpin2);
IRvalue3 = digitalRead(IRpin3);

if(IRvalue == HIGH || IRvalue2 == HIGH){   
digitalWrite(ledpin,HIGH); 
digitalWrite(ledpin1,LOW);    
digitalWrite(motor_left[0], LOW);                               
digitalWrite(motor_left[1], LOW);  
digitalWrite(motor_right[0], LOW);                             
digitalWrite(motor_right[1], LOW);
//
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 
delay(2000);
digitalWrite(motor_left[0], HIGH);                              
digitalWrite(motor_left[1], LOW);  
digitalWrite(motor_right[0], LOW);                           
digitalWrite(motor_right[1], HIGH); 
                                   
delay(1000);                                                                   
}

else{                                                                           
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW); 

digitalWrite(ledpin,LOW);
digitalWrite(ledpin1,HIGH);
  
if(IRvalue1 == HIGH || IRvalue3 == HIGH){                           
digitalWrite(ledpin,HIGH);                                                    
digitalWrite(ledpin1,LOW); 

digitalWrite(motor_left[0], LOW);                                       
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], LOW);                                    
digitalWrite(motor_right[1], LOW);
//
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 
delay(2000);
digitalWrite(motor_left[0], LOW);                                     
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], HIGH);                                   
digitalWrite(motor_right[1], LOW);

                                             
delay(1000);                                                                             
}

else{                                                                                      
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW); 

digitalWrite(ledpin,LOW);
digitalWrite(ledpin1,HIGH);

}
}
}

This method I’m trying to use millis(). For the first couple of minutes it just goes forward and does not change direction but after that it begins to detect the edge and turn. It does not stop, then reverse, then turn it just turns immediately.

int motor_left[] = {10, 9};                        
int motor_right[] = {7, 8};                         

int IRpin  = 13;                                         
int IRpin1 = 6;                                          
int IRpin2 = 12;                                        
int IRpin3 = 5;                                         
int ledpin = 11;                                        
int ledpin1 = 4;                                       
int IRvalue = 0;      
                                
int IRvalue1 = 0;                                    
int IRvalue2 = 0;                                   
int IRvalue3 = 0;  

unsigned long previousMillis = 0; 

const long interval = 2000;                                 

// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);                                   
  pinMode(ledpin,  OUTPUT); 
  pinMode(ledpin1, OUTPUT);
  pinMode(IRpin,   INPUT);  
  pinMode(IRpin1,  INPUT);
  pinMode(IRpin2,  INPUT);
  pinMode(IRpin3,  INPUT);
// Setup motors
int i;
for(i = 0; i < 2; i++){                                                                   
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}

// --------------------------------------------------------------------------- Loop

void loop() { 
unsigned long currentMillis = millis();

IRvalue = digitalRead(IRpin);                                                     
IRvalue1 = digitalRead(IRpin1);
IRvalue2 = digitalRead(IRpin2);
IRvalue3 = digitalRead(IRpin3);

if(IRvalue == HIGH || IRvalue2 == HIGH){   
digitalWrite(ledpin,HIGH); 
digitalWrite(ledpin1,LOW); 

if(currentMillis - previousMillis >= interval){   
digitalWrite(motor_left[0], LOW);                               
digitalWrite(motor_left[1], LOW);  
digitalWrite(motor_right[0], LOW);                             
digitalWrite(motor_right[1], LOW);

}

if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 

}

if (currentMillis-previousMillis >= interval){
digitalWrite(motor_left[0], HIGH);                              
digitalWrite(motor_left[1], LOW);  
digitalWrite(motor_right[0], LOW);                           
digitalWrite(motor_right[1], HIGH); 

}
                                                                  
}

else{                                                                           
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW); 

digitalWrite(ledpin,LOW);
digitalWrite(ledpin1,HIGH);
  
if(IRvalue1 == HIGH || IRvalue3 == HIGH){                           
digitalWrite(ledpin,HIGH);                                                    
digitalWrite(ledpin1,LOW); 


if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW);                                       
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], LOW);                                    
digitalWrite(motor_right[1], LOW);

}

if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 

}

if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW);                                     
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], HIGH);                                   
digitalWrite(motor_right[1], LOW);

}                                                                                                                        
}

else{                                                                                      
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW); 

digitalWrite(ledpin,LOW);
digitalWrite(ledpin1,HIGH);

}
}
}

Here’s OP’s pic:

d362136484f8fd023756f2bac00a73cc54548275.jpg

You haven’t said what the sensors’ values mean. Is a high over the edge or on the table?

I think in the millis() example you need to set a flag (say backingUp) when it detects the edge so that when it’s backed up a tiny bit and the sensors no longer detect the edge it carries on backing up enough. Then clear the flag and turn. Something like that anyway: that’s off the top of my head.

Yes high means the sensor has gone over the edge.

To me it is not visibly backing up as far as I can tell in the millis() example. Are you saying it could be backing up a tiny amount and moving on to the next step without waiting because the sensor has changed from high to low?

I’m pretty new at messing with this stuff but if it was moving on to the next step because it had reversed a small amount and the sensor was no longer high wouldn’t it just jump straight to the else statement and go forward without turning?

I cannot wrap my head around why it is ignoring the reverse part in that one.

int motor_left[] = {10, 9};   // these pins go to the L293D for for the Left Motor    
int motor_right[] = {7, 8};   // these pins go to the l293d for the right motor                     

// these are the pins that read the IR sensors
int IRpin  = 13;                                         
int IRpin1 = 6;                                          
int IRpin2 = 12;                                        
int IRpin3 = 5;  

// ledpin is a red led for the reverse and turn and ledpin 1 is a green led for going forward                                       
int ledpin = 11;                                        
int ledpin1 = 4;


// the initial values are low                                       
int IRvalue = 0;      
int IRvalue1 = 0;                                    
int IRvalue2 = 0;                                   
int IRvalue3 = 0;  


unsigned long previousMillis = 0; 

const long interval = 2000;                                 

// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);                                   
  pinMode(ledpin,  OUTPUT); 
  pinMode(ledpin1, OUTPUT);
  pinMode(IRpin,   INPUT);  
  pinMode(IRpin1,  INPUT);
  pinMode(IRpin2,  INPUT);
  pinMode(IRpin3,  INPUT);
// Setup motors
int i;
for(i = 0; i < 2; i++){                                                                   
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}
}

// --------------------------------------------------------------------------- Loop

void loop() { 
unsigned long currentMillis = millis();


// check all of the IR values on the sensors
IRvalue = digitalRead(IRpin);                                                     
IRvalue1 = digitalRead(IRpin1);
IRvalue2 = digitalRead(IRpin2);
IRvalue3 = digitalRead(IRpin3);


// if the sensors on the right side are high the car needs to stop for 2 seconds, reverse for 2 seconds and turn left for 2 seconds. The red led (ledpin) turns on and the green led (ledpin1) turns off.

if(IRvalue == HIGH || IRvalue2 == HIGH){   
digitalWrite(ledpin,HIGH); 
digitalWrite(ledpin1,LOW); 

if(currentMillis - previousMillis >= interval){   
digitalWrite(motor_left[0], LOW);                               
digitalWrite(motor_left[1], LOW);  
digitalWrite(motor_right[0], LOW);                             
digitalWrite(motor_right[1], LOW);

}

if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 

}

if (currentMillis-previousMillis >= interval){
digitalWrite(motor_left[0], HIGH);                              
digitalWrite(motor_left[1], LOW);  
digitalWrite(motor_right[0], LOW);                           
digitalWrite(motor_right[1], HIGH); 

}
                                                                  
}

// if the sensors are not reading high then go forward and the green led is on while the red led is off.
else{                                                                           
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW); 

digitalWrite(ledpin,LOW);
digitalWrite(ledpin1,HIGH);

If the sensors on the left side are high. stop for 2 seconds, reverse for 2 seconds and turn right for 2 seconds. The green led turns off and the red led turns on.
  
if(IRvalue1 == HIGH || IRvalue3 == HIGH){                           
digitalWrite(ledpin,HIGH);                                                    
digitalWrite(ledpin1,LOW); 


if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW);                                       
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], LOW);                                    
digitalWrite(motor_right[1], LOW);

}

if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW); 
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], LOW); 
digitalWrite(motor_right[1], HIGH); 

}

if(currentMillis - previousMillis >= interval){
digitalWrite(motor_left[0], LOW);                                     
digitalWrite(motor_left[1], HIGH); 
digitalWrite(motor_right[0], HIGH);                                   
digitalWrite(motor_right[1], LOW);

}                                                                                                                        
}


// if not the motors go forward.
else{                                                                                      
digitalWrite(motor_left[0], HIGH); 
digitalWrite(motor_left[1], LOW); 
digitalWrite(motor_right[0], HIGH); 
digitalWrite(motor_right[1], LOW); 

digitalWrite(ledpin,LOW);
digitalWrite(ledpin1,HIGH);

}
}
}