3 Generates a random number in order to simulate a coin toss.
6 Phisical LEDS and serial debug.
8 This example code is in the public domain.
11 // Pin 13 has an LED connected on most Arduino boards.
13 const int head = 13 ; // LED for HEAD
14 const int tail = 12 ; // LED for Tails
15 const int PAUSE = 1000 ;
17 long randomNumber = 0L; // Use the "L" to tell compiler it's a long data type, not an int.
22 // the setup routine runs once when you press reset:
24 // initialize the digital pin as an output.
25 pinMode(head, OUTPUT);
26 pinMode(tail, OUTPUT);
27 randomSeed(analogRead(0)); // Random initializer
29 Serial.println("Initializing random sequence, please wait for results.");
32 // the loop routine runs over and over again forever:
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
43 digitalWrite(tail, HIGH);// turn the LED ON
48 if (runs % 10 == 0) { // Each 10 runs print a summary to serial
49 Serial.print("Results after more 10 runs, for a total of: ");
51 Serial.print("Tails: \t") ;
52 Serial.println(tCount);
53 Serial.print("Heads: \t");
54 Serial.println(hCount);
57 delay(PAUSE); // Long pause