Trying to get this little wall avoiding bot runnin

Hi Guys,
I have started a new little project. It is a wall avoiding autonomous robot. I would eventually like to add gps. But right now I am just having a hard time with the IR sensor part. I need to bot to preform certain tasks as it senses different obstacles.

I currently have 3 sharp Ir detectors. 2 normal, and 1 long range. The 2 are angled for turning sensors. And the 1 is straight forward for back and forth movement sensory.

It is an Arduino, an H bridge, and a Cheap RC.

I have this code so far. I can get the turning to work on its own. And the forward/backward on it’s own. I can even sometimes get them both to work together. But it is not reliable.

Am I just not supplying enough current?
Or is there something that is confusing my code, and slowing things down.

Any ideas would be greatly appreciated. I might add that this code is a revision of many different versions. So the current operation is simple forward backward. And I tried some new things to get the code to include turning so if it gets real strange at one point sorry. That’s what experimenting will get you. :wink:

Thanks,
Timothy
“Keepin Upful”

int motorPin11 = 3; // declares motor pins    
int motorPin12 = 4;
int motorPin21 = 5;
int motorPin22 = 6;

int ircReader = 2;     // This sets the input pin of the IR Reader
float ircVal[10];       // array to store first 10 values coming from the sensor
float ircValSum=0;      // sum of first 10 readings
float ircValAvg=0;      // average of the first 10 readings

int irlReader = 3;     // This sets the input pin of the IR Reader
float irlVal[10];       // array to store first 10 values coming from the sensor
float irlValSum=0;      // sum of first 10 readings
float irlValAvg=0;      // average of the first 10 readings


int irrReader = 1;     // This sets the input pin of the IR Reader
float irrVal[10];       // array to store first 10 values coming from the sensor
float irrValSum=0;      // sum of first 10 readings
float irrValAvg=0;      // average of the first 10 readings


void setup()
{
 pinMode(motorPin11, OUTPUT);    
 pinMode(motorPin12, OUTPUT);
 pinMode(motorPin21, OUTPUT);
 pinMode(motorPin22, OUTPUT);
 Serial.begin(9600);        
}

void loop()

  {
    ircValAvg = analogRead(ircReader);      
                                                  // debug serial prints
    Serial.println("IR Center");
    Serial.println(ircValAvg);    
    irlValAvg = analogRead(irlReader);
    Serial.println("IR Left");
    Serial.println(irlValAvg);
    irrValAvg = analogRead(irrReader);
    Serial.println("IR Right");
    Serial.println(irrValAvg);
    
    
    if (ircValAvg <200)
      { goFor();
        Serial.println("Forward");
      }
      else if (ircValAvg >201) 
      { goBack();
        Serial.println("Backward");  
        }
        
        digitalRead(irlValAvg);
        digitalRead(irrValAvg);
        
       if (irlValAvg <250)
        { noTurn();
          Serial.println("No Turn");
        }
          else if(irrValAvg >250)
           {turnL();
           Serial.println("Turn Left");
           }
          else if(irlValAvg >250)
           {turnR();
           Serial.println("Turn Right");  
        }

  }
  
void goFor()                   
{
  digitalWrite(motorPin11, HIGH);
  digitalWrite(motorPin12, LOW);
}

void goBack()
{
  digitalWrite(motorPin11, LOW);
  digitalWrite(motorPin12, HIGH);
}

void noTurn()
{
  digitalWrite(motorPin21, LOW);
  digitalWrite(motorPin22, LOW);
}


void turnR()
{
  digitalWrite(motorPin21, LOW);
  digitalWrite(motorPin22, HIGH);
}

void turnL()
{
  digitalWrite(motorPin21, HIGH);
  digitalWrite(motorPin22, LOW);
}

void stop()
{
  digitalWrite(motorPin11, LOW);
  digitalWrite(motorPin12, LOW);
}

void turnAroundL()
{
  goBack();
  turnR();
  delay(1000);
  goFor();
  turnL();
  delay(1000);
}

void turnAroundR()
{
  goBack();
  turnL();
  delay(1000);
  goFor();
  turnR();
  delay(1000);
}

One obvious thing I see is that all of your "turn" functions set the digital outputs to the same states. That's probably not correct.

Also, there no reason to use floats for the values from your sensors. The analogRead returns an integer from 0-1023. By converting to float you're just wasting memory and making your sketch run slower.

void turnR()
{
 digitalWrite(motorPin21, LOW);
 digitalWrite(motorPin22, HIGH);
}

void turnL()
{
 digitalWrite(motorPin21, LOW);
 digitalWrite(motorPin22, HIGH);
}

void stop()
{
 digitalWrite(motorPin21, LOW);
 digitalWrite(motorPin22, HIGH);
}

void neut()
{
 digitalWrite(motorPin21, LOW);
 digitalWrite(motorPin22, HIGH);
}

In fact, all those functions do the same thing with the motor control pins.

PS. you can use the hash button (#) just above the text box to display code in a box like that.

Hi Guys,
Sorry. Yeah your right. I was rereading that code once I posted it and it is all sorts of messed up. This version may be getting a little closer to proper. This project has been plagued with computer crashes, and lost code.

Also thanks for the (#) code tip. Good to know

int motorPin11 = 3; // declares motor pins    
int motorPin12 = 4;
int motorPin21 = 5;
int motorPin22 = 6;

int ircReader = 2;     // This sets the input pin of the IR Reader
float ircVal[10];       // array to store first 10 values coming from the sensor
float ircValSum=0;      // sum of first 10 readings
float ircValAvg=0;      // average of the first 10 readings

int irlReader = 3;     // This sets the input pin of the IR Reader
float irlVal[10];       // array to store first 10 values coming from the sensor
float irlValSum=0;      // sum of first 10 readings
float irlValAvg=0;      // average of the first 10 readings


int irrReader = 1;     // This sets the input pin of the IR Reader
float irrVal[10];       // array to store first 10 values coming from the sensor
float irrValSum=0;      // sum of first 10 readings
float irrValAvg=0;      // average of the first 10 readings


void setup()
{
 pinMode(motorPin11, OUTPUT);    
 pinMode(motorPin12, OUTPUT);
 pinMode(motorPin21, OUTPUT);
 pinMode(motorPin22, OUTPUT);
 Serial.begin(9600);        
}

void loop()

  {
    ircValAvg = analogRead(ircReader);      
                                                  // debug serial prints
    Serial.println("IR Center");
    Serial.println(ircValAvg);    
    irlValAvg = analogRead(irlReader);
    Serial.println("IR Left");
    Serial.println(irlValAvg);
    irrValAvg = analogRead(irrReader);
    Serial.println("IR Right");
    Serial.println(irrValAvg);
    
    
    if (ircValAvg <200)
      { goFor();
        Serial.println("Forward");
      }
      else if (ircValAvg >201) 
      { goBack();
        Serial.println("Backward");  
        }
        
        digitalRead(irlValAvg);
        digitalRead(irrValAvg);
        
       if (irlValAvg <250)
        { noTurn();
          Serial.println("No Turn");
        }
          else if(irrValAvg >250)
           {turnL();
           Serial.println("Turn Left");
           }
          else if(irlValAvg >250)
           {turnR();
           Serial.println("Turn Right");  
        }

  }
  
void goFor()                   // these functions are simply combinations of outs on the pins that make the car move in a certain way
{
  digitalWrite(motorPin11, HIGH);
  digitalWrite(motorPin12, LOW);
}

void goBack()
{
  digitalWrite(motorPin11, LOW);
  digitalWrite(motorPin12, HIGH);
}

void noTurn()
{
  digitalWrite(motorPin21, LOW);
  digitalWrite(motorPin22, LOW);
}


void turnR()
{
  digitalWrite(motorPin21, LOW);
  digitalWrite(motorPin22, HIGH);
}

void turnL()
{
  digitalWrite(motorPin21, HIGH);
  digitalWrite(motorPin22, LOW);
}

void stop()
{
  digitalWrite(motorPin11, LOW);
  digitalWrite(motorPin12, LOW);
}

void turnAroundL()
{
  goBack();
  turnR();
  delay(1000);
  goFor();
  turnL();
  delay(1000);
}

void turnAroundR()
{
  goBack();
  turnL();
  delay(1000);
  goFor();
  turnR();
  delay(1000);
}