X-Git-Url: http://git.piffa.net/web?a=blobdiff_plain;f=programming%2Floops%2Floop_match_2_for_loop%2Floop_match_2_for_loop.ino;fp=programming%2Floops%2Floop_match_2_for_loop%2Floop_match_2_for_loop.ino;h=94b3e3aeb3dc8d1658c4b5b851b1f85cec11f2c8;hb=e50f2cf8e7402ea56cd01835be9f88c53876bfd1;hp=0000000000000000000000000000000000000000;hpb=f7cdba7df419dcde095f911daa92deb9dcd283ec;p=sketchbook_andrea diff --git a/programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino b/programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino new file mode 100644 index 0000000..94b3e3a --- /dev/null +++ b/programming/loops/loop_match_2_for_loop/loop_match_2_for_loop.ino @@ -0,0 +1,67 @@ +/* Exercise 2, with a WHILE loop + + 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 + Light a led in case + Light the other LED if a run of 255 test has gone + Log the results (if success) trough serialport + */ + +// Data structure + +const byte GREEN = 13 ; // LED for found value +const byte RED = 12 ; // LEAD for restart + +const int TARGET = 200 ; +long randomNumber = 0L; + +// Staff +const int WAIT = 1000 ; +const int REST = 10 ; +byte count = 0 ; +const byte MAXRUN = 10 ; +byte totalRun = 0 ; + +void setup() { + pinMode(RED,OUTPUT); + pinMode(GREEN,OUTPUT); + // 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: + digitalWrite(GREEN, LOW); + digitalWrite(RED, LOW); + // Serial.println(count); + + for (count == 0; count < 255; count++ , delay(REST)) { + randomNumber = random(0,255); //Randoom value generated + if (randomNumber == TARGET) { // When we catch the value + Serial.print("--> Match found! Counter was at: "); // serial message + Serial.println(count); + digitalWrite(GREEN, HIGH); + delay(WAIT * 5); + digitalWrite(GREEN, LOW); + count++ ; + } + } + + Serial.println("Counter resetted."); // serial staff + digitalWrite(RED, HIGH); + delay(WAIT); + count++ ; + totalRun++ ; + if (totalRun == MAXRUN) { + Serial.println("10 runs done, exit program."); + digitalWrite(RED, HIGH); + delay(WAIT); + exit(0); + } +} +