Go Down

Topic: Line follower code issues - HELP! (Read 235 times) previous topic - next topic

xezz99

Sep 02, 2017, 05:38 am Last Edit: Sep 03, 2017, 01:31 am by xezz99
So im using an Uno R3 board with two continuous rotation servos and two IR line sensor modules.
The inputs are as follows:right sensor - pin 13 (PWM) and left sensor - pin 12 (PWM)
Right servo (RM1) - Pin 3 (PWM), Left servo (RM1) - pin 2 (PWM)

My servos are currently spinning in opposite directions, my code is as follows;

/*------ Arduino Line Follower Code----- */
/*-------definning Inputs------*/
#define LS 13      // left sensor
#define RS 12      // right sensor
/*-------definning Outputs------*/
#define LM1 2       // left motor
#define RM1 3       // right motor

void setup()
{
  pinMode(LS, INPUT);
  pinMode(RS, INPUT);
  pinMode(LM1, OUTPUT);
  pinMode(RM1, OUTPUT);
}
void loop() 
{   

  if(digitalRead(LS) && digitalRead(RS))   // Move Forward
  {
    analogWrite(LM1, 180);
    analogWrite(RM1, 90);
  }
 
  if(digitalRead(LS) && (RS))    // Turn left
  {
    analogWrite(LM1, 0);
    analogWrite(RM1,90);
  }
 
  if(digitalRead(RS) && (LS))     // turn right
  {
    analogWrite(LM1, 90);
    analogWrite(RM1, 180);
  }
 
  if((LS) && (RS))     // stop
  {
    analogWrite(LM1, 90);
    analogWrite(RM1, 90);
  }
}

This is my first time coding, please help as this is for my project. this needs to be done by the end of the week! any help/advice will be greatly appreciated!

slipstick

You forgot to tell us what it does and you are expecting it to do that's different. Also what are the actual components you're using (links to datasheets would be ideal). Not all servos and IR sensors are the same.

Also please post a circuit diagram showing how everything is connected. In particular how are you driving the servos/motors. If you just have them connected direct to the pins then you may already have killed the Arduino because that is the wrong way to drive motors.

A few immediately obvious problems:

Pin 2 is not a PWM pin on a UNO.

I'm not sure setting values 0, 90, 180 in analogWrites does what you think to the motors. It looks a little as though you may be mixing up driving things via direct PWM and driving servos using the Servo library.

Also in your if statements you keep using (LS) and (RS). LS and RS are the pin numbers. Are you sure you don't mean something else like perhaps the value at those pins?

It might be a good idea to put some Serial.prints in at various places so you can see exactly where in the code you are getting to and what the various values are at the time.

Steve

xezz99

Thanks for that! I'll head out today and get a servo motor driver. I will work on the points you made and I'll re post edited code later on. Thanks for your help.
Nathan

PaulS

Code: [Select]
  if((LS) && (RS))     // stop
This is equivalent to
Code: [Select]
  if((13) && (12))     // stop
Since 13 is true and 12 is true, your robot will always stop.

If statements involving pin numbers are wrong 99.99999999% of the time.
The art of getting good answers lies in asking good questions.

Go Up