Line Follower Code

Hey,
I'm making a line following robot that needs to drop a ping pong ball at the end of a track. I made a small change to my code then all of the sudden stuff isn't working. Here is my original code.

int buttonpin = 0; //Sets pin 0 to be called buttonpin.
int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
//add pins for for ping pong ball device.

void setup() {
pinMode(buttonpin, INPUT); //sets pin 0 as input.
pinMode(leftmotor, OUTPUT); //sets pin 10 as output.
pinMode(rightmotor, OUTPUT); //Sets pin 11 as output.
pinMode(leftline, INPUT); //Sets pin 5 as input.
pinMode(middleline, INPUT); //Sets pin 4 as input.
pinMode(rightline, INPUT); //Sets pin 3 as input.
pinMode(bumper, INPUT); //Sets pin 7 as input.
// configure pins yet to be added listed above.
}

void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline) == HIGH) {
digitalWrite(leftmotor, HIGH); // Stops left motor.
digitalWrite(rightmotor, LOW); //Starts right motor
delay(1);
}
else {
//Do nothing
}
if (digitalRead(rightline) == HIGH) {
digitalWrite(leftmotor, LOW); // Runs left motor.
digitalWrite(rightmotor, HIGH); // Stops left motor.
delay(1);
}
else {
// Do nothing

}
if (digitalRead(middleline) == HIGH) {
digitalWrite(leftmotor, HIGH); //Runs left motor
digitalWrite(rightmotor, HIGH); //Runs right motor.
delay(1);
}
else {
//DO nothing
}
}

This code works fine. Then I made a change to add the motor which is supposed to be triggered by a bumper switch. I added it at the end but now my robot doesn't seem to follow a line and when the bumper is triggered it doesn't seem to spin the motor like it should. The code with the ball added that doesn't work is below

int buttonpin = 0; //Sets pin 0 to be called buttonpin.
int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
int ball = 9; //Ball motor
//add pins for for ping pong ball device.

void setup() {
pinMode(buttonpin, INPUT); //sets pin 0 as input.
pinMode(leftmotor, OUTPUT); //sets pin 10 as output.
pinMode(rightmotor, OUTPUT); //Sets pin 11 as output.
pinMode(leftline, INPUT); //Sets pin 5 as input.
pinMode(middleline, INPUT); //Sets pin 4 as input.
pinMode(rightline, INPUT); //Sets pin 3 as input.
pinMode(bumper, INPUT); //Sets pin 7 as input.
pinMode(ball, OUTPUT); // Sets pin 9 as output.

}

void loop() {
// Main runtime instruction set.
// Read sensor input.
if (digitalRead(leftline) == HIGH) {
digitalWrite(leftmotor, HIGH); // Stops left motor.
digitalWrite(rightmotor, LOW); //Starts right motor
delay(1);
}
else {
//Do nothing
}
if (digitalRead(rightline) == HIGH) {
digitalWrite(leftmotor, LOW); // Runs left motor.
digitalWrite(rightmotor, HIGH); // Stops left motor.
delay(1);
}
else {
// Do nothing

}
if (digitalRead(middleline) == HIGH) {
digitalWrite(leftmotor, HIGH); //Runs left motor
digitalWrite(rightmotor, HIGH); //Runs right motor.
delay(1);
}
else {
//DO nothing
}
if (digitalRead(bumper) == HIGH) {
digitalWrite(leftmotor, LOW); // stops left motor.
digitalWrite(rightmotor, LOW); // Stops right motor.
digitalWrite(ball, HIGH); //starts motor to release ball.
delay(425);
digitalWrite(ball,LOW);
delay(1000);
}
else {
// Do nothing

}
}

Any and all help is really appreciated.

I don't know what is wrong but your code does not match your comments which makes understanding it more difficult as does the fact that the code is not in code tags which makes it more difficult to copy to an editor.

    digitalWrite(leftmotor, LOW); // Runs left motor.
    digitalWrite(rightmotor, HIGH); // Stops left motor.
    digitalWrite(leftmotor, HIGH); //Runs left motor
    digitalWrite(rightmotor, HIGH); //Runs right motor.

sorry about that I copied and pasted parts and i forgot to change the comments. I will fix that and repost.

int buttonpin = 0; //Sets pin 0 to be called buttonpin.
int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
//add pins for for ping pong ball device.

void setup() {
 pinMode(buttonpin, INPUT); //sets pin 0 as input.
 pinMode(leftmotor, OUTPUT); //sets pin 10 as output.
 pinMode(rightmotor, OUTPUT); //Sets pin 11 as output.
 pinMode(leftline, INPUT); //Sets pin 5 as input.
 pinMode(middleline, INPUT); //Sets pin 4 as input.
 pinMode(rightline, INPUT); //Sets pin 3 as input.
 pinMode(bumper, INPUT); //Sets pin 7 as input.
 // configure pins yet to be added listed above.
}

void loop() {
 // Main runtime instruction set.
 // Read sensor input.
 if (digitalRead(leftline) == HIGH) {
   digitalWrite(leftmotor, HIGH); // Starts left motor.
   digitalWrite(rightmotor, LOW); //Stops right motor
   delay(1);
 }
 else {
   //Do nothing
 }
 if (digitalRead(rightline) == HIGH) {
   digitalWrite(leftmotor, LOW); // Stops left motor.
   digitalWrite(rightmotor, HIGH); // Starts left motor.
   delay(1);
 }
 else {
   // Do nothing
    
 }
 if (digitalRead(middleline) == HIGH) {
   digitalWrite(leftmotor, HIGH); //Starts left motor
   digitalWrite(rightmotor, HIGH); //Starts right motor.
   delay(1);
 }
 else {
   //DO nothing
 }
}

This is the other code

int buttonpin = 0; //Sets pin 0 to be called buttonpin.
int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
int ball = 9; //Ball motor
//add pins for for ping pong ball device.

void setup() {
 pinMode(buttonpin, INPUT); //sets pin 0 as input.
 pinMode(leftmotor, OUTPUT); //sets pin 10 as output.
 pinMode(rightmotor, OUTPUT); //Sets pin 11 as output.
 pinMode(leftline, INPUT); //Sets pin 5 as input.
 pinMode(middleline, INPUT); //Sets pin 4 as input.
 pinMode(rightline, INPUT); //Sets pin 3 as input.
 pinMode(bumper, INPUT); //Sets pin 7 as input.
 pinMode(ball, OUTPUT); // Sets pin 9 as output.

}

void loop() {
 // Main runtime instruction set.
 // Read sensor input.
 if (digitalRead(leftline) == HIGH) {
   digitalWrite(leftmotor, HIGH); // Starts left motor.
   digitalWrite(rightmotor, LOW); //Stops right motor
   delay(1);
 }
 else {
   //Do nothing
 }
 if (digitalRead(rightline) == HIGH) {
   digitalWrite(leftmotor, LOW); // Stops left motor.
   digitalWrite(rightmotor, HIGH); // Starts left motor.
   delay(1);
 }
 else {
   // Do nothing
    
 }
 if (digitalRead(middleline) == HIGH) {
   digitalWrite(leftmotor, HIGH); //Starts left motor
   digitalWrite(rightmotor, HIGH); //Starts right motor.
   delay(1);
 }
 else {
   //DO nothing
 }
 if (digitalRead(bumper) == HIGH) {
   digitalWrite(leftmotor, LOW); // Stops left motor.
   digitalWrite(rightmotor, LOW); // Stops right motor.
   digitalWrite(ball, HIGH); //Starts motor to release ball.
   delay(425);
   digitalWrite(ball,LOW); //Stops the motor for ball.
   delay(1000);
//add code for turn around later here.
 }
 else {
   // Do nothing
    
 }
}

If anyone could help me out it would be greatly appreciated. I have to get this robot done for a contest that is in two weeks and I am running out of time.

How is the bumper switch wired ?

I will fix that and repost.

But, I'm still not going to read the rules and post the code correctly.

I'm still not going to explain what the problem is.

If anyone could help me out it would be greatly appreciated.

The first comment above leads me to wonder why we should, and the second leads me to wonder how we can.

dsalveson:
I added it at the end but now my robot doesn't seem to follow a line

Do you mean it doesn't follow the line up to the point where it's supposed to drop the ball although it used to? In which case, if you just comment the new block of code out again with /..../ does it work correctly again to follow the line?

(As an aside, what do you mean it doesn't "seem" to follow the line?- does it or doesn't it?)

dsalveson:
when the bumper is triggered it doesn't seem to spin the motor like it should.

Which motor?- the ball drop motor? Have you tested the bumper code block in a small sketch by itself with just minimal pin assignments for the switch and ball motor to see if it works at all?

(See previous comment: what does it mean that it doesn't "seem" to spin..... does it or doesn't it, and what does "as it should" mean?)

Hi,
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
[u]See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html[/u]

Please... Tom..... :slight_smile:

int buttonpin = 0; //Sets pin 0 to be called buttonpin.

Pin 0 is used for serial communication with the computer through the USB port. It's not a good idea to use it for anything else.

Also your comments are not useful. They should not be just rephrasing the code. They should explain how it works.

In this case, one comment above that section is all you need, for example:
// assign pin names:

aarg:

int buttonpin = 0; //Sets pin 0 to be called buttonpin.

Pin 0 is used for serial communication

Ah crap I should have seen that one

Ok, I am going to to explain this all again to get everyone on the same page. This line follower is supposed to follow a line from one end to the other, it is not a circle. When it gets to the end it hits a tower which then triggers the ball mechanism (which works, I have tested it) to release the ball into the tower. When I posted the original code (by the way whoever was chewing me out for not posting the code correctly I am very new to this and don't know how to) the robot worked fine. When I added the if else statement at the end all of the sudden everything just stopped working and it wouldn't follow the line, I put the original code back on and it once again followed the line.

Here are some quick answers to some questions above.

The bumper switch is wired with an input and an output with 5v in then to pin 7.

The buttonpin no longer exists because I took it out of the whole sketch due to some other things to please ignore that.

The comments are in there to explain what each line does to my teacher.

I will use the tag for my code next time also, sorry about that.

Yes, I tested the ball motor and the bumper in a sketch by themselves and it worked fine.

Thank you all for being so helpful and I will post the code as an attachment below.

Line_Follower_Instruction_Set_Motor.ino (1.81 KB)

I will use the tag for my code next time also, sorry about that.

But you didn't post it anyway.

by the way whoever was chewing me out for not posting the code correctly I am very new to this and don't know how to

After 17 posts, you haven't noticed the sticky post at the top that says, "How to use this forum, please read."?

The buttonpin no longer exists because I took it out of the whole sketch due to some other things to please ignore that.

No, you didn't.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

We need to see how you are wiring your switches and motor, as well as how you are powering your project.

Thanks... Tom.. :slight_smile:

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

This has a lot of wires so that would be very time consuming but my line sensors read high while on black and low while on white. The motors are 2 wire and are going through transistors. The bumper switch is 2 wire which reads high when pressed and low when not pressed.

Sorry about that last post how nothing was done correctly I read the guidelines and understand this better now. My code below it posted correctly.

int buttonpin = 0; //Sets pin 0 to be called buttonpin.
int leftmotor = 10; //Sets pin 10 to control Left Motors.
int rightmotor = 11; //Sets ping 11 to control Right Motor
int leftline = 5; //Left line follower input.
int middleline = 4; //Middle line follower input.
int rightline = 3; //Right line follower input.
int bumper = 7; //Bumber swithch input
int ball = 9; //Ball motor
//add pins for for ping pong ball device.

void setup() {
  pinMode(buttonpin, INPUT); //sets pin 0 as input.
  pinMode(leftmotor, OUTPUT); //sets pin 10 as output.
  pinMode(rightmotor, OUTPUT); //Sets pin 11 as output.
  pinMode(leftline, INPUT); //Sets pin 5 as input.
  pinMode(middleline, INPUT); //Sets pin 4 as input.
  pinMode(rightline, INPUT); //Sets pin 3 as input.
  pinMode(bumper, INPUT); //Sets pin 7 as input.
  pinMode(ball, OUTPUT); // Sets pin 9 as output.

}

void loop() {
  // Main runtime instruction set.
  // Read sensor input.
  if (digitalRead(leftline) == HIGH) {
    digitalWrite(leftmotor, HIGH); // Stops left motor.
    digitalWrite(rightmotor, LOW); //Starts right motor
    delay(1);
  }
  else {
    //Do nothing
  }
  if (digitalRead(rightline) == HIGH) {
    digitalWrite(leftmotor, LOW); // Runs left motor.
    digitalWrite(rightmotor, HIGH); // Stops left motor.
    delay(1);
  }
  else {
    // Do nothing
     
  }
  if (digitalRead(middleline) == HIGH) {
    digitalWrite(leftmotor, HIGH); //Runs left motor
    digitalWrite(rightmotor, HIGH); //Runs right motor.
    delay(1);
  }
  else {
    //DO nothing
  }
  if (digitalRead(bumper) == HIGH) {
    digitalWrite(leftmotor, LOW); // stops left motor.
    digitalWrite(rightmotor, LOW); // Stops right motor.
    digitalWrite(ball, HIGH); //starts motor to release ball.
    delay(425);
    digitalWrite(ball,LOW);
    delay(1000);
  }
  else {
    // Do nothing
     
  }
}

This code for some reason doesn't work. When I take out the statement that says

if (digitalRead(bumper) == HIGH) {
    digitalWrite(leftmotor, LOW); // stops left motor.
    digitalWrite(rightmotor, LOW); // Stops right motor.
    digitalWrite(ball, HIGH); //starts motor to release ball.
    delay(425);
    digitalWrite(ball,LOW);
    delay(1000);
  }
  else {
    // Do nothing

Then the code works fine.

This code for some reason doesn't work.

Can you please remind me what it does do ?

The bumper switch is wired with an input and an output with 5v in then to pin 7.

Can you please be clearer as to how this switch is wired, possibly with a diagram ? Have you got a pull down resistor in the circuit, for example ?

Can you please remind me what it does do ?

It is for a line following robot. When it hits a tower at the end of the track it needs to drop a ping pong ball. The device to do this is called Ball at the very top of the code.

Can you please be clearer as to how this switch is wired, possibly with a diagram ? Have you got a pull down resistor in the circuit, for example ?

This isn't very good and only includes the switch, none of the other components.

It is for a line following robot.

I realise that but what does the program do that it shouldn't or not do that it should ?

No resistor in the bumper circuit means that the input to the pin is floating at an uncertain, and possibly changing voltage. Consider setting its pinMode to INPUT_PULLUP, connecting the switch so that the pin is taken to GND when closed and changing the program logic to match.

I realise that but what does the program do that it shouldn't or not do that it should ?

The program doesn't SEEM to be reading the line sensor input, weather or not it is I don't know. The program works fine when I do not have this statement in the code.

 if (digitalRead(bumper) == HIGH) {
    digitalWrite(leftmotor, LOW); // stops left motor.
    digitalWrite(rightmotor, LOW); // Stops right motor.
    digitalWrite(ball, HIGH); //starts motor to release ball.
    delay(425);
    digitalWrite(ball,LOW);
    delay(1000);
  }
  else {
    // Do nothing

When I add this to the end of my original code all of the sudden stuff stops working and the robot keeps stopping and then turning unexpectedly. When this isn't in the code it follows the line as expected.

Consider setting its pinMode to INPUT_PULLUP

I think this may be the problem I will try that and get back to you but it may take a while because my business teacher is nice and is letting me work on this and his class at the same time and we just got new Arduino Boards so I am setting up the DUE right now. Thanks!