For this tutorial, you’ll need:
Watch this full video:
Full Code:
#define led1 4 #define led2 5 #define led3 6 #define led4 7 #define led5 8 #define led6 9 #define led7 10 #define ledWin 11 #define ledLoose 12 #define button 2 int ledNumber = 0; int numberOfLed = 7; int randomNumber = 0; int currentNumber = 0; void turnLedON (int ); // define lal function int play (); //play void win(); void loose(); void setup() { // put your setup code here, to run once: Serial.begin(9600); for (int i=led1; i<=led7; i++) { pinMode(i, OUTPUT); digitalWrite(i, LOW); } pinMode(button, INPUT); pinMode(ledWin, OUTPUT); digitalWrite(ledWin, LOW); pinMode(ledLoose, OUTPUT); digitalWrite(ledLoose, LOW); } void loop() { // put your main code here, to run repeatedly: if(digitalRead(button) == HIGH) { currentNumber = play(); delay(50); digitalWrite(ledLoose, LOW); digitalWrite(ledWin, LOW); } else { if(currentNumber!=0) { if(currentNumber%2==0) // le reste de la division par 2 est = 0 { win(); } else { loose(); } } } } void turnLedON (int number) //keep in mind we can use switch next time and we will { if(number == 0) { for (int i = led1; i<=led7; i++) { digitalWrite(i, LOW); } } if(number == 1) { digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); digitalWrite(led6, LOW); digitalWrite(led7, LOW); digitalWrite(led1, HIGH); } if(number == 2) { digitalWrite(led1, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); digitalWrite(led6, LOW); digitalWrite(led7, LOW); digitalWrite(led2, HIGH); } if(number == 3) { digitalWrite(led2, LOW); digitalWrite(led1, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); digitalWrite(led6, LOW); digitalWrite(led7, LOW); digitalWrite(led3, HIGH); } if(number == 4) { digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led1, LOW); digitalWrite(led5, LOW); digitalWrite(led6, LOW); digitalWrite(led7, LOW); digitalWrite(led4, HIGH); } if(number == 5) { digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led1, LOW); digitalWrite(led6, LOW); digitalWrite(led7, LOW); digitalWrite(led5, HIGH); } if(number == 6) { digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); digitalWrite(led1, LOW); digitalWrite(led7, LOW); digitalWrite(led6, HIGH); } if(number == 7) { digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); digitalWrite(led5, LOW); digitalWrite(led6, LOW); digitalWrite(led1, LOW); digitalWrite(led7, HIGH); } } int play () { randomNumber = random(1, numberOfLed+1); //random(1, 5) -> 1 2 3 4 options turnLedON(randomNumber); return (randomNumber); } void win() { digitalWrite(ledWin, HIGH); delay(500); digitalWrite(ledWin, LOW); delay(500); } void loose() { digitalWrite(ledLoose, HIGH); delay(500); digitalWrite(ledLoose, LOW); delay(500); }