]> git.piffa.net Git - sketchbook_andrea/blob - programming/conditional_test/head_tails_ino/head_tails_ino.ino
Common esempi
[sketchbook_andrea] / programming / conditional_test / head_tails_ino / head_tails_ino.ino
1 /*
2   Head tails
3   Generates a random number in order to simulate a coin toss.
4
5
6   Phisical LEDS and serial debug.
7  
8  This example code is in the public domain.
9  */
10
11 // Pin 13 has an LED connected on most Arduino boards.
12 // give it a name:
13 const int head   = 13  ; // LED for HEAD
14 const int tail   = 12 ;  // LEAD for Tails
15 const int PAUSE  = 1000 ;
16 const int REST   = 50 ;
17 long randomNumber = 0L; // Use the "L" to tell compiler it's a long data type, not an int.
18 int hCount = 0;
19 int tCount = 0;
20 int runs   = 0 ;
21
22 // the setup routine runs once when you press reset:
23 void setup() {                
24   // initialize the digital pin as an output.
25   pinMode(head, OUTPUT);     
26   pinMode(tail, OUTPUT);     
27   randomSeed(analogRead(0)); // Random initializer
28   Serial.begin(9600);
29   Serial.println("Initializing random sequence, please wait for results.");
30 }
31
32 // the loop routine runs over and over again forever:
33 void loop() {
34   randomNumber = random();
35   digitalWrite(head, LOW);  
36   digitalWrite(tail, LOW); 
37   delay(REST);   // wait a bit
38   if (randomNumber % 2 == 1) {
39     digitalWrite(head, HIGH);// turn the LED on ON
40     hCount++ ;
41   } 
42   else {
43     digitalWrite(tail, HIGH);// turn the LED ON
44     tCount++ ;
45   }
46
47   delay(PAUSE);               // Long pause
48   runs++;
49   
50   if (runs % 10 == 0) {  // Each 10 runs print a summary to serial
51     Serial.print("Results after more 10 runs, for a total of: ");
52     Serial.println(runs);
53     Serial.print("Tails: \t") ;
54     Serial.println(tCount);
55     Serial.print("Heads: \t");
56     Serial.println(hCount);
57   } 
58 }
59
60
61