# Should i use a new "While loop" or just use "if statements"?

Hello there im in charge of programming for a “meteorite hunt project” where the robot has to detect and pick up the right coloured meteorites and then return them to the home base. I have an issue however, when the robot collects the puck and goes to home base. Should i start another while loop or just use if statements? Here is my code. Thank you very much for your time.

``````{
int th=20; //value for pink puck
int dst=100;// distance for ir sensor
int br=9;  //value for home base
int fu=130; // value for a full tray
while(1)
{

if
(digital(11)&&(digital(12)))//if both switches are hit reverse and turn
{ motor(0,-100);
motor(1,-100);
sleep(1.0);
motor(0,100);
motor(1,-100);
sleep(1.0);
}
else{
if(digital(11))//if left switch is hit turn
{
motor(0,100);
motor(1,-100);
sleep(1.0);

}
else
{
if(digital(12))//if right switch is hit turn
{
motor(0,-100);
motor(1,100);
sleep(1.0);
}
}

if (analog(0)>th)// if wrong puck is detected, reverse and turn from puck
{
motor(0,-100);//left motor reverse
motor(1,-100);// right motor reverse
sleep(2.0);// reverse for 2 seconds
motor(0,-100);
motor(1,100);//turn
}

if (analog(0)<=th)// if correct puck is detected, spin gears for 5 seconds
{
motor(2,10);
sleep(5.0);
motor(2,0);
sleep(1.0);
}

if(analog(5)>100)//if something is close reverse for 1 second and turn away
{
motor(0,-100);// reverse
motor(1,-100);//reverse
sleep(1.0);
motor(0,100);
motor(1,-100);
sleep(1.0);
}
if ((analog(0)<=th)&&(analog(6)<=fu))//if the right puck is detected and the tray is full
{
motor(0,-100);
motor(1,-100);
sleep(2.0);
motor(0,100);
motor(1,-100);
sleep(1.0);
}
else
{
if((analog(0)<=th)&&(analog(6)<=fu))//if the wrong puck is detected and the tray is full
{
motor(0,-100);
motor(1,-100);
sleep(2.0);
motor(0,100);
motor(1,-100);
sleep(1.0);
}
}
if((analog(1)<=br)&&(analog(2)<=br)&&(analog(6)>fu))// if it detects homebase without full tray. reverse and turn
{
motor(0,-100);
motor(1,-100);
sleep(1.0);
motor(0,100);
motor(1,-100);
sleep(1.0);
}

if((analog(1)<=br)&&(analog(2)<=br)&&(analog(6)<=fu))//if the goals are detected and 3 pucks are collected approach    stop drop pucks, reverse and turn around
{
motor(0,-100);
motor(1,100);
sleep(2.0);
motor(1,-100);
motor(2,-100);
sleep(2.0);
motor(2,100);
sleep(5.0);
}

}
}
}
``````

Should i start another while loop or just use if statements?

Should I wear a blue suit to work on Tuesday or should I bike to work on Thursday? That's not an either/or question, and neither is yours.

while statements loop. if statements don't. If you need a loop, then an if statement is useless.

There is something very odd about your code from the perspective of the normal Arduino IDE. What programming environment are you using ? As Paul says your question makes no logical sense at the moment.

Arduino IDE has 2 required functions, setup() and loop().

In setup() you put the things that only run once. In loop() you put your real time code that deals with events and allows multitasking. http://www.gammon.com.au/forum/?id=11411

You can also include libraries, #define values, declare variables, and add other functions outside of setup() and loop() though if you want variables inside loop() to hold their values, you must declare them as static.

Your while( 1 ) is like trying to make loop() when you don't know it's there.

What you should do for your robot is make a finite state machine.