]> git.piffa.net Git - sketchbook_andrea/blobdiff - programming/loops/loop_match_2_while_loop/loop_match_2_while_loop.ino
for loop
[sketchbook_andrea] / programming / loops / loop_match_2_while_loop / loop_match_2_while_loop.ino
index c2df5b457921aca590f9e74f3839f4d948509318..92d930d9d3d50a84e8d18f7092d9d61641f25777 100644 (file)
@@ -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:
   }
 } 
 
+