ir sensor robot

Hey everyone I would really appreciate some help, I am building a robot that follows a line along a course the bracket holding the sensors is connected to a servo motor so it turns as well as the robot. I am also using an h bridge and sensor shield. I am having some trouble with my code though. when I run this the servo motor just starts shaking a bunch and the right motor will not move at all, however, if I copy the code into a new workspace and run them separately it works exactly as it is intended to. So I am pretty sure it isn’t a hardware problem. I would really appreciate some help with this.

Right now the only thing in the loop is trying to move the right motor but it does nothing.

#include <Servo.h>

Servo Servo1;

float voltageRead0 = A0;  //left
float voltageRead1 = A1;  //right
float voltageRead2 = A2;  //rightmiddle
float voltageRead3 = A3;  //leftmiddle
float voltageRead4 = A4;  //middle

float voltage0;
float voltage1;
float voltage2;
float voltage3;
float voltage4;

int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 5;
int LED5 = 8;
int servoPin = 10;
int Angle = 80;




const int ENA = 9;
const int IN1 = 7;
const int IN2 = 6;

const int ENB = 3;
const int IN3 = 4;
const int IN4 = 2;





int sensor1;
int sensor2;
int sensor3;
int sensor4;
int sensor5;
int Change = 300;


int ChangeAngle ( int Angle, float volt0, float volt1, float volt2, float volt3, float volt4) {

  if (volt0 > sensor1) {

    digitalWrite(LED1, HIGH);

    if (Angle < 110) {

      Angle = Angle + 4;
      return Angle;

    }
  }

  else {
    digitalWrite(LED1, LOW);
    
  }


  if (volt1 > sensor2) {

    digitalWrite(LED2, HIGH);

    if (Angle < 110) {

      Angle = Angle + 2;
      return Angle;
    }
  }

  else {
    digitalWrite(LED2, LOW);
    
  }


  if (volt2 > sensor3) {

    digitalWrite(LED3, HIGH);
    

  }

  else {
    digitalWrite(LED3, LOW);
    
  }


  if (volt3 > sensor4) {

    digitalWrite(LED4, HIGH);

    if (Angle > 50) {

      Angle = Angle - 2;
      return Angle;
    }
    
  }

  else {
    digitalWrite(LED4, LOW);
    
  }

  if (volt4 > sensor5) {

    digitalWrite(LED5, HIGH);

    if (Angle > 50) {

      Angle = Angle - 4;
      return Angle;
    }
  }

  else {
    digitalWrite(LED5, LOW);
    
  }
 return Angle;

}
void driveForward () {

  analogWrite (ENA, 80);
  analogWrite (ENB, 80);

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}



void turnRight() {


  analogWrite(ENA, 10);
  analogWrite(ENB, 80);

  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void turnLeft() {

  analogWrite(ENA, 100);
  analogWrite(ENB, 0);

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);

}

void chooseMovement( int degree) {
    if (degree > 88) {

      turnRight();
    }

    else if (degree < 70) {

      turnLeft();

    }

    else {

      driveForward();
    }
  }



void setup() {
  
  Servo1.attach(servoPin);
  Serial.begin(9600);
  
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);

  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);

  pinMode(ENB, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  
  Serial.print(Angle);
  
  sensor1 = analogRead(voltageRead0) + Change;
  sensor2 = analogRead(voltageRead1) + Change;
  sensor3 = analogRead(voltageRead2) + Change;
  sensor4 = analogRead(voltageRead3) + Change;
  sensor5 = analogRead(voltageRead4) + Change;

  digitalWrite(LED1, HIGH);
  delay(200);
  digitalWrite(LED2, HIGH);
  delay(200);
  digitalWrite(LED3, HIGH);
  delay(200);
  digitalWrite(LED4, HIGH);
  delay(200);
  digitalWrite(LED5, HIGH);
  delay(200);
  digitalWrite(LED1, LOW);
  delay(200);
  digitalWrite(LED2, LOW);
  delay(200);
  digitalWrite(LED3, LOW);
  delay(200);
  digitalWrite(LED4, LOW);
  delay(200);
  digitalWrite(LED5, LOW);
  delay(200);



}

void loop() {


  voltage0 = analogRead(voltageRead0);
  voltage1 = analogRead(voltageRead1);
  voltage2 = analogRead(voltageRead2);
  voltage3 = analogRead(voltageRead3);
  voltage4 = analogRead(voltageRead4);

  Serial.print(voltage0, 2);
  Serial.print("\t");
  Serial.print(voltage1, 2);
  Serial.print("\t");
  Serial.print(voltage2, 2);
  Serial.print("\t");
  Serial.print(voltage3, 2);
  Serial.print("\t");
  Serial.print(voltage4, 2);
  Serial.print("\t");
  Serial.println(Angle);

  

  analogWrite(ENA, 100);
  analogWrite(ENB, 0);

  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);


}

Servo shaking and one motor not working sounds exactly like a hardware problem. Almost certainly insufficient power but could also be poor connections. So a schematic diagram and details of the components including what is powering it all might help.

Why are your input pin numbers defined as floats?

Steve

If I just try to run the left motor it works perfectly, but if I try to run just the left and nothing else it doesn't move, so I don't think it is an insufficient power problem, otherwise, the left motor wouldn't run. I am running the Arduino with the power from my laptop when it is plugged in with the cable and the h-bridge is powered by 2 9 volt batteries which should be more than enough considering they are 3v motors.

This what the schematic for my L298N Hbridge looks like but with 2 9 volts

The servo is just the basic SG90 that came in the set, and that is just plugged into the shield.
I've attached the schematic for the sensors and pic of the robot although it's a bit messy and hard to make out what's what. But that is mostly the wiring for the sensors, which I don't believe is the problem.

for some reason I unable to post the photo of the robot itself, but I dont think it will be that usefull anyways

note: Im not sure why the input pins are floats that was just in the code we were given. Also, the servo motor is moving around a lot without any code in the main loop and I am not sure why.

duncan2342:
If I just try to run the left motor it works perfectly, but if I try to run just the left and nothing else it doesn't move

You might know what you mean but I can't make any sense of that. And schematics that are of what you DON'T have really aren't much use.

Are you really running 2 x 9V batteries in SERIES? So 18V? That's a bit much for 3V motors. What sort of batteries? If they're little 9V PP3 smoke detector batteries then they cannot provide enough power for motors.

And what exactly is the "H-bridge and sensor shield" that the servo is plugged into?

Steve

Sorry I meant to say " if I just try to run the left motor it works perfectly, but if I try to run just the right and nothing else it doesn't move"

The schematics are the exact same as what I am using just with different input channels in the Arduino and different batteries.

and yea I am running 2 9V PP3's in series

H-bridge is the L298N Motor driver board and the Arduino sensor shield is just a type of Arduino shield that goes on top.

I also found the solution in another in case anyone runs into the same problem. The servo library affects the PMW of pins 9 and 10 as they are linked to the same timmer, so if you use the servo library you cant use pin 9 or 10 for the PMW of the motors. All I had to do was switch the motor pmw to pin 11 and the whole thing runs perfectly.

Thank you for the help,

Duncan Adshade