X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=programming%2Floops%2Floop_match_2_while_loop%2Floop_match_2_while_loop.ino;h=92d930d9d3d50a84e8d18f7092d9d61641f25777;hb=8f9853f962b96d5b01e1db7cef031783c851c8dc;hp=c2df5b457921aca590f9e74f3839f4d948509318;hpb=bd123f688c3566dd1413e3ec17f6b5a7134cf2cb;p=sketchbook_andrea diff --git a/programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino b/programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino index c2df5b4..92d930d 100644 --- a/programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino +++ b/programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino @@ -1,7 +1,9 @@ -/* Exercise 2, with a WHILE loop +/* Exercise 2, with a WHILE loop and a Break statement: +- http://arduino.cc/en/Reference/Break + Test a random number agains a value: a iteretive loop perform 255 runs to see if a random number in range 0-255 - is equal tothe target value of 200. + is equal to the target value set to 200. Light a led in case Light the other LED if a run of 255 test has gone @@ -29,10 +31,7 @@ void setup() { // Serial stuff Serial.begin(9600); Serial.println("Initializing random sequence, please wait for results."); - - // Random stuff randomSeed(analogRead(0)); // Random initializer - } void loop() { // put your main code here, to run repeatedly: @@ -42,20 +41,23 @@ void loop() { // put your main code here, to run repeatedly: while (count < 255) { randomNumber = random(0,255); //Randoom value generated + Serial.print("|"); + count++ ; + delay(REST); if (randomNumber == TARGET) { // When we catch the value + Serial.println(); Serial.print("--> Match found! Counter was at: "); // serial message Serial.println(count); digitalWrite(GREEN, HIGH); delay(WAIT); count++ ; + break; // Interrompe il ciclo } - //Serial.println(count); - count++ ; - delay(REST); } - + Serial.println(); Serial.println("Counter resetted."); // serial staff + count = 0; digitalWrite(RED, HIGH); delay(WAIT); count++ ; @@ -68,3 +70,4 @@ void loop() { // put your main code here, to run repeatedly: } } +