How to make loop only happen if...

Hey guys! I've been meaning to learn programming and playing with arduino for years, and just started today. So please excuse dumb questions and any misunderstandings.

Basically, I have some LED's that I have in 6 different loop parts.

I also have the arduino Serial Printing a random number as soon as it is powered on.

What I need to happen, is for each of the loop parts for the LEDs to only run if the arduino serial prints a specific number, and if it prints a different number, nothing happens.

eg.

if Serial Print = 2
Only run
LoopPart2

OR

if Serial Print = 5
Only run
LoopPart5

and so on...

I'm guessing I should use some form of "if" but im not sure how to write it.

Here is one of my loops. I know its messy and not great, but I'm just starting.

void loopPart6() {
//
digitalWrite(12, HIGH); // turn the red LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
//
digitalWrite(12, HIGH); // turn the red LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
//
digitalWrite(12, HIGH); // turn the red LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(12, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

So how do I get that to run, ONLY if the serial prints a number 6?

Thanks so much for any help, I really really appreciate it!
Sorry again for being hard to understand.

Welcome to ArduioLand....

What you really want, is SwitchCase I'd say.

Switch case is easier to screw up, the syntax is weirder.

I recommend if/else if pattern, eg:

if (condition1) {
//do stuff
} else if (condition2) {
//do something else
} else {
// do this if none of the conditions are matched
}

What do you mean "if the Arduino prints a 6 on serial"?
Do you mean to have it do something if you send a 6 to the Arduino over serial? Or if the Arduino sends a 6 over serial? In the latter case, just call that bit of code when the Arduino decides to send a 6. In the former case, there's a Serial Input Basics thread somewhere around here. (search for that in quotes on these forums, you'll find it - i forget where it is, but it's very frequently linked to)

It's not clear exactly what you want to do - so if you could clarify that...

So how do I get that to run, ONLY if the serial prints a number 6?

I don't understand this bit. You mean, if you receive a 6 on the serial port?

Please read the forum "sticky" and use code tags in your post.

DrAzzy:
Switch case is easier to screw up, the syntax is weirder.

I recommend if/else if pattern, eg:

if (condition1) {
//do stuff
} else if (condition2) {
//do something else
} else {
// do this if none of the conditions are matched
}

What do you mean "if the Arduino prints a 6 on serial"?
Do you mean to have it do something if you send a 6 to the Arduino over serial? Or if the Arduino sends a 6 over serial? In the latter case, just call that bit of code when the Arduino decides to send a 6. In the former case, there's a Serial Input Basics thread somewhere around here. (search for that in quotes on these forums, you'll find it - i forget where it is, but it's very frequently linked to)

It's not clear exactly what you want to do - so if you could clarify that...

Oh ok, thank you!
Yea, I want it to do something (One of my loops) when the Arduino SENDS a 6 (or whatever number, because I have it generating a random number at the beginning)

Thank you for the help!

Switch case is easier to screw up, the syntax is weirder.

Each to his own, but I find that switch/case is clearer to write and read than multiple if/else and the syntax is simple. It does have limitations, but to select actions based on a single number it is made for the job.

The dangers of not posting a complete sketch... (Hint, OP )

I took OP to mean he uses random() into a variable called SerialPrint, which he then Serial.print()s and which he also wants to use as his control.

I personally see nothing weird in the syntax of switch...case.

Sorry for the confusion and thank you everyone for your help!!

Here is what I have so far, I’ve just got it running the 6th loop for now while I tested my circuit, but you get the idea.

I eventually want these series of events to happen (again, sorry for misuse of terminology, Im trying! )

-power on
-generate random number
-run loop that matches that number

• if button on pin 7 is pushed, loop 11 (solid red led) runs
• if button on pin 6 is pushed, loop 12 (solid green led) runs

So im basically stuck at making it use the serial it is printing as a way of picking a loop. I think I can work the button things out myself, but any help is welcome.

Again, just started in the last 24 hours so excuse the mess, I’m using this little puzzle as motivation to learn and get better. thank you for all your help!

``````                                    //LED PARTERN CODE GAME//

/*put info about game here
*
*
*
*/

//Setup Area//

//Setup 1//
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);  //BLUE
pinMode(12, OUTPUT);  //RED
pinMode(11, OUTPUT);  //YELLOW
pinMode(10, OUTPUT);  //GREEN

//Random Number Gen//
long randNumber;
Serial.begin(9600);

// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function

// print a random number from 1 to 10
randNumber = random(5, 7);
Serial.println(randNumber);

delay(2000);
}

//loop Area//

void loop()
{
/*loopPart1();
loopPart2();
loopPart3();
loopPart4();
loopPart5(); */
loopPart6();

}

//multiple Loops Part//

// //

/*// Loop for Blue(13) Red(12) Yellow(11) Green(10)

void loopPart2() {
digitalWrite(13, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(11, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(10, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(2000);              // wait for a second
}

// Loop for Green(10) Yellow(11) Red(12) Blue(13)//

void loopPart3() {
digitalWrite(10, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(11, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(13, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
delay(2000);              // wait for a second
}

//Loop for Red(12) Red(12) Blue(13) Red(12)//

void loopPart4() {
digitalWrite(12, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(13, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(2000);              // wait for a second

}

//Loop for Green(10) Green(10) Green(10) Green(10) Green(10) Green(10) Green(10) Blue(13) //

void loopPart5() {
digitalWrite(10, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(10, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(10, HIGH);   // turn the blue LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(10, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(10, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(10, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(10, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(13, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
delay(2000);              // wait for a second
}

*/

// Loop for x3 RED (12)//

void loopPart6() {
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(1000);              // wait for a second
}
// Area for solving the puzzle

[CODE]
``````

The way you have it, I'd use switch..case as I suggested, in loop(), with the appropriate call to loopPartX() being the content of each case.

edit... with the switch on randNumber of course.

Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

Read this before posting a programming question

How to use this forum

Sorry! Fixed!

JimboZA:
The way you have it, I'd use switch..case as I suggested, in loop(), with the appropriate call to loopPartX() being the content of each case.

edit... with the switch on randNumber of course.

Ok... Thank you for your help! I'm just having trouble understanding where exactly to put it? would you be able to use loopPart6 as an example?

``````void loop()
{

switch (randNumber) {
case 1:
loopPart1();
break;
case 2:
loopPart2();
break;
case 3:
loopPart3();
break;
case 4:
loopPart4();
break;
case 5:
loopPart5();
break;
case 6:
loopPart6();
break;
default:
// catch all if necessary
break;
} // end of switch case

} // end of loop
``````

JimboZA:

``````void loop()
``````

{

switch (randNumber) {
case 1:
loopPart1();
break;
case 2:
loopPart2();
break;
case 3:
loopPart3();
break;
case 4:
loopPart4();
break;
case 5:
loopPart5();
break;
case 6:
loopPart6();
break;
default:
// catch all if necessary
break;
} // end of switch case

} // end of loop

Thanks so much, Sorry but I can not for the life of me get this to work... Not sure if I'm putting it in the wrong place or what, but I feel like such an idiot

Put a serial print in each part of the switch...case so it throws out a message to tell you where it's going, especially if it's going to the default, eg:

``````case 6:
Serial.println("in case 6");
loopPart6();
break;
default:
// catch all if necessary
Serial.println("in default case");
break;
``````

Using serial prints to check logic flow is sop by the way.

Not sure if I’m putting it in the wrong place or what

So please post the program that you are having trouble with. The only thing missing from Jimbo’s example was the generation of the random number but from your previous posts you are doing that “at the beginning” whatever that means.

Also for testing purposes put serial prints inside your functions as well:

``````void loopPart6() {

Serial.println("in loopPart6");  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(500);              // wait for a second
//
digitalWrite(12, HIGH);   // turn the red LED on (HIGH is the voltage level)
delay(500);              // wait for a second
digitalWrite(12, LOW);    // turn the LED off by making the voltage LOW
delay(1000);              // wait for a second
}
``````

UKHeliBob:
the generation of the random number but from your previous posts you are doing that "at the beginning" whatever that means.

Yep it's in the code he posted:

``````randNumber = random(5, 7);
Serial.println(randNumber);
``````

JimboZA:
Put a serial print in each part of the switch...case so it throws out a message to tell you where it's going, especially if it's going to the default, eg:

``````case 6:
``````

Serial.println("in case 6");
loopPart6();
break;
default:
// catch all if necessary
Serial.println("in default case");
break;

``````

Using serial prints to check logic flow is sop by the way.
``````

Woo! Its working, thanks so much! Just one last little problem. Once it starts (and does) an LED loop (say, loopPart6) it goes back to the top and generates another random number.

How do I get it to just stay in the loopPart once it gets there?

Thanks again!

You could move everything from loop() to the bottom of setup(), then it will only ever run once until you reset.

If you do that, remember to leave an empty loop() to keep the compiler happy.

``````void loop()
{
}
``````