Is there anyway to interrupt a while loop if a color sensor's value changes?

I want to make my line tracing robot go right when a color sensor on it's right side detects black and turn until the left color sensor detects black color. Is there anyway to do this?

Basically, when Right Color Sensor detects black, it turns right and interrupt the loop and turns left as soon as the Left Color Sensor detects black.

I think this is a programming question, so I am uploading this to Programming Question one instead of Robotics.

Line_Tracer_White_Only.ino (6.12 KB)

The answer is very likely yes but let's see your current program.

while (the left colour sensor does not detect black)
{
  //execute the code here
}
//now execute this code

UKHeliBob:
The answer is very likely yes but let's see your current program.

while (the left colour sensor does not detect black)

{
  //execute the code here
}
//now execute this code

I am a noob in programming, so I don't know very well. I attached my full code on the question thing, so that you can troubleshoot my program :smiley:

I attached my full code on the question thing, so that you can troubleshoot my program

It would have been better if you had copied it into a post here and used code tags (</> icon top/left of the forum editor) around it.

I just downloaded your code and pasted it into the IDE.
    delay(1000000000000000000000000000);What in heavens name is that all about ? How long do you want to wait !

Did you write the program or copy it from somewhere ? It would be helpful if it compiled, don't you think ?

UKHeliBob gave you a simple way to make it work. Making others fix your code won't help you in the long run, and won't give you the satisfaction of making something yourself. You need to first learn the basics of programming, how the Arduino code flows and about loops and conditional statements.

The best way to control a line following robot is with if statements or a while loop in the loop(). I was participating in a robot competition and we took the third or second place back then.

You make the robot turn depending on what your sensors read... You seriously need to learn the basics, it takes a few hours.

David1337:
UKHeliBob gave you a simple way to make it work. Making others fix your code won't help you in the long run, and won't give you the satisfaction of making something yourself. You need to first learn the basics of programming, how the Arduino code flows and about loops and conditional statements.

The best way to control a line following robot is with if statements or a while loop in the loop(). I was participating in a robot competition and we took the third or second place back then.

You make the robot turn depending on what your sensors read... You seriously need to learn the basics, it takes a few hours.

I made it turn as soon as it detects the black line and it didnt work right. It kept on not following the line properly.

Did you make it move in the other direction once it detects white instead of black?

David1337:
Did you make it move in the other direction once it detects white instead of black?

Nope. I made that if left color sensor detects black, it turns left. And if the right color sensor detects black, I made it to go right. However, this didnt work on the cornors as both color sensors detected white and ran straight. That made the robot go out of the track

521minsu:
Nope. I made that if left color sensor detects black, it turns left. And if the right color sensor detects black, I made it to go right. However, this didnt work on the cornors as both color sensors detected white and ran straight. That made the robot go out of the track

Okay, you're almost here. Instead, make the robot go straight only when both sensors detect black. And if both are white, it means that the robot is off track. In this case, have a last_move variable in order to know what was the last direction that the robot turned before it went off the track and keep turning him in the oppostie direction until one of the sensors detects black again.

UKHeliBob:
It would have been better if you had copied it into a post here and used code tags (</> icon top/left of the forum editor) around it.

I just downloaded your code and pasted it into the IDE.
    delay(1000000000000000000000000000);What in heavens name is that all about ? How long do you want to wait !

Did you write the program or copy it from somewhere ? It would be helpful if it compiled, don't you think ?

  1. I wrote the program
  2. The program is too long that it wont go into this post thing
  3. That delay is for stopping the motor, which is not working at all
  4. This program compiles at least for my Mega 2560

That delay is for stopping the motor, which is not working at all

There are better ways. How long do you want the motor to stop for ?

This program compiles at least for my Mega 2560

With delay(1000000000000000000000000000); in it ?

There are better ways. How long do you want the motor to stop for ? [ /quote]
I want to stop the motor until some kind of code signaled it. For example, if one of my color sensors detect red, make the robot stop and move on to the next code after a while.

With delay(1000000000000000000000000000); in it ?

Yes, it still compiles very well and I am just ignoring it as I don't know how to stop a motor with AFMotor library.

First, 521minsu, you really need to learn this stuff yourself.

But, in case you didn't know, you can "halt" your program forever by replacing

delay(1000000000000000000000000000);

with:

while(1);

521minsu:
I attached my full code on the question thing, so that you can troubleshoot my program :smiley:

P.S. You should only ask in the forum for help, not demand us to troubleshoot and debug YOUR project.

521minsu:
Yes, it still compiles very well and I am just ignoring it as I don't know how to stop a motor with AFMotor library.

Which version of the IDE are you using ?
It won't compile for me using 1.5.6-r2

sketch_may14a:5: error: integer constant is too large for 'long' type

UKHeliBob:
Which version of the IDE are you using ?
It won't compile for me using 1.5.6-r2

sketch_may14a:5: error: integer constant is too large for 'long' type

That might be the problem as I am using 1.6.8 version.

P.S. I thought that delay part wasn't gonna effect my program as I wasn't using it in the setup nor the loop. However, it did effect my program somehow and now, I have erased that part.
Can anyone tell me how to stop two motors when color changes?

thought that delay part wasn't gonna effect my program as I wasn't using it in the setup nor the loop.

It was in the drive() function that is called from loop() so formed part of your program.

Can anyone tell me how to stop two motors when color changes?

How long do you want to stop them for ? A period of time or forever ?

Watch the semicolon right after the //Serial.print, even if it's intentional, I wouldn't use it to keep the loop spinning, an empty brackets are much more readable

lineisyJanelle:
Watch the semicolon right after the //Serial.print, even if it's intentional, I wouldn't use it to keep the loop spinning, an empty brackets are much more readable

Whereabouts in the code are you referring to ?