Last active
January 21, 2016 13:19
-
-
Save timvisee/de2c0160b32843a2f2a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Button pin | |
| int button = 2; | |
| int buttonState = LOW; | |
| int counter = 0; | |
| int timerTimeout = 2000; | |
| long timer = -1; | |
| // Led pins | |
| int led1 = 7; | |
| int led2 = 6; | |
| int led3 = 5; | |
| int led4 = 4; | |
| int led5 = 5; | |
| // Antwoord | |
| int antwoord; | |
| // Called on setup | |
| void setup() { | |
| // Set up LED pins | |
| pinMode(led1, OUTPUT); | |
| pinMode(led2, OUTPUT); | |
| pinMode(led3, OUTPUT); | |
| pinMode(led4, OUTPUT); | |
| pinMode(led5, OUTPUT); | |
| pinMode(button, INPUT); | |
| Serial.begin(9600); | |
| // Randomize | |
| randomSeed(analogRead(1)); | |
| } | |
| // Start the timer | |
| void startTimer() { | |
| timer = millis() + timerTimeout; | |
| } | |
| // Stop the timer | |
| void stopTimer() { | |
| timer = -1; | |
| } | |
| // Check whether the timer is done | |
| bool isTimerDone() { | |
| return millis() >= timer && timer >= 0; | |
| } | |
| // Called on loop | |
| void loop() { | |
| // Generate a random number | |
| int willekeurig = random(1, 16); | |
| antwoord = willekeurig; | |
| // led 1 gaat aan als het getal hoger/gelijk is dan 8 | |
| if(willekeurig >= 8) { | |
| digitalWrite(led1, HIGH); | |
| willekeurig -= 8; | |
| } | |
| // led 2 gaat aan als het getal hoger/gelijk is dan 4 | |
| if(willekeurig >= 4) { | |
| digitalWrite(led2, HIGH); | |
| willekeurig -= 4; | |
| } | |
| // led 3 gaat aan als het getal hoger/gelijk is dan 2 | |
| if(willekeurig >= 2) { | |
| digitalWrite(led3, HIGH); | |
| willekeurig -= 2; | |
| } | |
| // led 4 gaat aan als het getal gelijk is aan 1 | |
| if(willekeurig == 1) { | |
| digitalWrite(led4, HIGH); | |
| willekeurig -= 1; | |
| } | |
| // start de Timer | |
| startTimer(); | |
| // Button loop to catch the button presses | |
| while(!isTimerDone() || counter == 0) { | |
| buttonState = digitalRead(button); | |
| // When the button is pressed | |
| if(buttonState == HIGH && lastButtonState == LOW) { | |
| // Increase the button counter | |
| counter++; | |
| startTimer(); | |
| lastButtonState = HIGH; | |
| delay(50); | |
| } | |
| // Button released | |
| else if(buttonState == LOW && lastButtonState == HIGH) { | |
| lastButtonState = LOW; | |
| startTimer(); | |
| delay(50); | |
| } | |
| } | |
| // Stop de timer. | |
| stopTimer(); | |
| // Laat resultaat zien | |
| if(counter == antwoord) { | |
| digitalWrite(7, LOW); | |
| digitalWrite(6, LOW); | |
| digitalWrite(5, LOW); | |
| digitalWrite(4, LOW); | |
| digitalWrite(3, HIGH); | |
| delay(3000); | |
| } else { | |
| digitalWrite(7, LOW); | |
| digitalWrite(6, LOW); | |
| digitalWrite(5, LOW); | |
| digitalWrite(4, LOW); | |
| digitalWrite(3, HIGH); | |
| delay(3000); | |
| } | |
| // Doe alle lampjes uit | |
| digitalWrite(7, LOW); | |
| digitalWrite(6, LOW); | |
| digitalWrite(5, LOW); | |
| digitalWrite(4, LOW); | |
| digitalWrite(3, LOW); | |
| // Heel even wachten voor volgende ronde | |
| delay(500); | |
| counter = 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment