Problem with robot lawn mower code

Good evening. For some time I am venturing into the project of a mower. the robot and consists of two independent motors that allow the robot to move. an independent front wheel. for the moment I added three hc-SR04 sensors in the front to locate the walls and the obstacles of larger size (at a later time will put even the bumper for the obstacles that the sensors will not see). all controlled via a remote control and an IR receiver. I set the progeamma so that squeezing of the buttons the robot moves forward or backward or rotates in one direction or another. plus I put an automatic mode (auto1) in which the robot moves autonomously based on what they "see" the sensors. Here I have a problem. in fact if I press the button that should start the automatic mode, the robot does not always do what it should. sometimes start and goes straight ignoring the sensors. I'm programming for a little problema there will be many repetitions and inelegant things. any correction or advice or improvement will be welcomed willingly. Thank you all in advance

_170616.ino (6.84 KB)

Post your code?

if I was not wrong to charge it should be attached as _170616

Attached != posted.

#include <IRremote.h>

int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results;
#define dir_1 46
#define pwm_1 3
#define dir_2 47
#define pwm_2 2
int STATE=1;
int Arming_Time=0;
int pin=9;
int Pulse=1000;
int triggerPort = 53;
int echoPort = 52;
int triggerPort1 =51 ;
int echoPort1 = 50;
int triggerPort2 =48 ;
int echoPort2 = 49;
byte speed = 125;

void setup(){
    pinMode(pin, OUTPUT);
  for(Arming_Time=0;Arming_Time<500;Arming_Time+=1)
  {
    digitalWrite(pin,HIGH);
  delayMicroseconds(1100);
  digitalWrite(pin,LOW);
  delay(20-(Pulse/1000));
  
}

  
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(pwm_1,OUTPUT);
pinMode(dir_1,OUTPUT);
pinMode(pwm_2,OUTPUT);
pinMode(dir_2,OUTPUT);
pinMode( triggerPort, OUTPUT );
pinMode( echoPort, INPUT );
pinMode( triggerPort1, OUTPUT );
pinMode( echoPort1, INPUT );
pinMode( triggerPort2, OUTPUT );
pinMode( echoPort2, INPUT );
Serial.begin( 9600 );
Serial.println( "Sensore ultrasuoni: ");
}



void loop(){
  

    //porta bassa l'uscita del trigger
digitalWrite( triggerPort, LOW );

 
//invia un impulso di 10microsec su trigger
digitalWrite( triggerPort, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort, LOW );


 
long duration = pulseIn( echoPort, HIGH );

 
long r = 0.034 * duration / 2;

 
Serial.print( "durata: " );
Serial.print( duration );
Serial.print( " , " );
Serial.print( "distanza: " );

 
//dopo 38ms è fuori dalla portata del sensore
if( duration > 38000 ) Serial.println( "fuori portata");
else { Serial.print( r ); Serial.println( "cm" );}
//secondosensore
digitalWrite( triggerPort1, LOW );

digitalWrite( triggerPort1, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort1, LOW );

long duration1 = pulseIn( echoPort1, HIGH );

long r1 = 0.034 * duration1 / 2;

Serial.print( "durata1: " );
Serial.print( duration1 );
Serial.print( " , " );
Serial.print( "distanza1: " );
 

//dopo 38ms è fuori dalla portata del sensore
if( duration1 > 38000 ) Serial.println( "fuori portata1");
else { Serial.print( r1 ); Serial.println( "cm1" );}
//sensore3
//porta bassa l'uscita del trigger
digitalWrite( triggerPort2, LOW );

 
//invia un impulso di 10microsec su trigger
digitalWrite( triggerPort2, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort2, LOW );


 
long duration2 = pulseIn( echoPort2, HIGH );

 
long r2 = 0.034 * duration2 / 2;

 
Serial.print( "durata2: " );
Serial.print( duration2 );
Serial.print( " , " );
Serial.print( "distanza2: " );

 
//dopo 38ms è fuori dalla portata del sensore
if( duration2 > 38000 ) Serial.println( "fuori portata");
else { Serial.print( r2 ); Serial.println( "cm" );}


  
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal
    irrecv.resume();// receive the next value
  }
  
 if ( results.value == 0x20DF02FD ||  results.value == 0x10406 ){  //ruota motore dc a destra 
    avanti();        
  } 
  if ( results.value == 0x20DF22DD ||  results.value == 0x10406 ){  //ruota motore dc a destra 
    STOP();        
  } 
  if ( results.value == 0x20DF609F ||  results.value == 0x10406 ){  //ruota motore dc a destra 
    destra();        
  } 
   if ( results.value == 0x20DF827D ||  results.value == 0x10406 ){  //ruota motore dc a destra 
    INDIETRO();        
    
  } 
   if ( results.value == 0x20DFE01F ||  results.value == 0x10406 ){  //ruota motore dc a destra 
    SINISTRA();        
    
  } 
     if ( results.value == 0x20DF7887 ||  results.value == 0x10406 ){  //ruota motore dc a destra 
   auto1();        
    
  } 
  if ( results.value == 0x20DF8877 ||  results.value == 0x10406 ){  //taglia
   cutON();        
    
  } 

}


void cutON()
{
  digitalWrite(pin, HIGH); 
  delayMicroseconds(1500); 
  digitalWrite(pin, LOW); 
  delay(20-(1250/1000)); 
}

void avanti()
{
  digitalWrite(dir_1,LOW);
  digitalWrite(dir_2,HIGH);
  analogWrite(pwm_1,speed);
  analogWrite(pwm_2,speed);  
}
void STOP()
{
  digitalWrite(pwm_1,0);
  digitalWrite(pwm_2,0);
  
}
void SINISTRA()
{
    digitalWrite(dir_1,HIGH);
    digitalWrite(dir_2,HIGH);
    analogWrite(pwm_1,speed);
    analogWrite(pwm_2,speed);
  
}
void destra()
{
    digitalWrite(dir_1,LOW);
    digitalWrite(dir_2,LOW);
    analogWrite(pwm_1,speed);
    analogWrite(pwm_2,speed);
  
}
void INDIETRO()
{
    digitalWrite(dir_1,HIGH);
    digitalWrite(dir_2,LOW);
    analogWrite(pwm_1,speed);
    analogWrite(pwm_2,speed);
  
}

void auto1(){
  
  //porta bassa l'uscita del trigger
digitalWrite( triggerPort, LOW );

 
//invia un impulso di 10microsec su trigger
digitalWrite( triggerPort, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort, LOW );


 
long duration = pulseIn( echoPort, HIGH );

 
long r = 0.034 * duration / 2;

 
Serial.print( "durata: " );
Serial.print( duration );
Serial.print( " , " );
Serial.print( "distanza: " );

 
//dopo 38ms è fuori dalla portata del sensore
if( duration > 38000 ) Serial.println( "fuori portata");
else { Serial.print( r ); Serial.println( "cm" );}
//secondosensore
digitalWrite( triggerPort1, LOW );

digitalWrite( triggerPort1, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort1, LOW );

long duration1 = pulseIn( echoPort1, HIGH );

long r1 = 0.034 * duration1 / 2;

Serial.print( "durata1: " );
Serial.print( duration1 );
Serial.print( " , " );
Serial.print( "distanza1: " );
 

//dopo 38ms è fuori dalla portata del sensore
if( duration1 > 38000 ) Serial.println( "fuori portata1");
else { Serial.print( r1 ); Serial.println( "cm1" );}
//sensore3
//porta bassa l'uscita del trigger
digitalWrite( triggerPort2, LOW );

 
//invia un impulso di 10microsec su trigger
digitalWrite( triggerPort2, HIGH );
delayMicroseconds( 10 );
digitalWrite( triggerPort2, LOW );


 
long duration2 = pulseIn( echoPort2, HIGH );

 
long r2 = 0.034 * duration2 / 2;

 
Serial.print( "durata2: " );
Serial.print( duration2 );
Serial.print( " , " );
Serial.print( "distanza2: " );

 
//dopo 38ms è fuori dalla portata del sensore
if( duration2 > 38000 ) Serial.println( "fuori portata");
else { Serial.print( r2 ); Serial.println( "cm" );}
 

  
if (r2<50){
  if (r1<30){
  digitalWrite(dir_1,HIGH);
  digitalWrite(dir_2,LOW);
  analogWrite(pwm_1,speed);
  analogWrite(pwm_2,speed);
  }
  else
  {
  digitalWrite(dir_1,LOW);
  digitalWrite(dir_2,LOW);
  analogWrite(pwm_1,speed);
  analogWrite(pwm_2,speed);
  }
}
else if (r1<50){
  if (r2<30){
  digitalWrite(dir_1,HIGH);
  digitalWrite(dir_2,LOW);
  analogWrite(pwm_1,speed);
  analogWrite(pwm_2,speed); 
  }
  else{
  digitalWrite(dir_1,HIGH);
  digitalWrite(dir_2,HIGH);
  analogWrite(pwm_1,speed);
  analogWrite(pwm_2,speed);
  } 
}
else if (r<30){
  digitalWrite(dir_1,HIGH);
  digitalWrite(dir_2,LOW);
  analogWrite(pwm_1,speed);
  analogWrite(pwm_2,speed);
}
else {
  digitalWrite(dir_1,LOW);
  digitalWrite(dir_2,HIGH);
  analogWrite(pwm_1,speed);
  analogWrite(pwm_2,speed);
}

}
int triggerPort = 53;
int echoPort = 52;
int triggerPort1 =51 ;
int echoPort1 = 50;
int triggerPort2 =48 ;
int echoPort2 = 49;

If you can't number ALL the variables in a collection, OR, better yet, use arrays, I can't be bothered to read any farther.