1 /* Exercise 2, with a WHILE loop and a Break statement:
2 - http://arduino.cc/en/Reference/Break
4 Test a random number agains a value:
5 a iteretive loop perform 255 runs to see if a random number in range 0-255
6 is equal to the target value set to 200.
9 Light the other LED if a run of 255 test has gone
10 Log the results (if success) trough serialport
15 const byte GREEN = 13 ; // LED for found value
16 const byte RED = 12 ; // LEAD for restart
18 const int TARGET = 200 ;
19 long randomNumber = 0L;
22 const int WAIT = 1000 ;
25 const byte MAXRUN = 10 ;
30 pinMode(GREEN,OUTPUT);
33 Serial.println("Initializing random sequence, please wait for results.");
34 randomSeed(analogRead(0)); // Random initializer
37 void loop() { // put your main code here, to run repeatedly:
38 digitalWrite(GREEN, LOW);
39 digitalWrite(RED, LOW);
40 // Serial.println(count);
43 randomNumber = random(0,255); //Randoom value generated
47 if (randomNumber == TARGET) { // When we catch the value
49 Serial.print("--> Match found! Counter was at: "); // serial message
50 Serial.println(count);
51 digitalWrite(GREEN, HIGH);
54 break; // Interrompe il ciclo
59 Serial.println("Counter resetted."); // serial staff
61 digitalWrite(RED, HIGH);
65 if (totalRun == MAXRUN) {
66 Serial.println("10 runs done, exit program.");
67 digitalWrite(RED, HIGH);