Greetings,
I have this problem I cant seen to fix with my arduino.. I want to make a little game where randomly 1 out of 7 LEDS blinks and then the 7 leds start to blink from left to right. the purpose of the game is to press the button when the random led that blinked at the beginning is lit. if you can do so a sound plays, if you fail another sound plays..
the problem is that the integer i save my random value for the led in cant be read in the
if(statusknop == HIGH && leds[randomnummer] == HIGH){
as i can never win the game. ive been trying for a while now but cant seen to find a solution. the rest of the code is working properly
see my code below. im sorry that most of my stuff is in dutch ![]()
i am sorry for the bad english ![]()
greetz,
an arduino noob
int leds[7] = {2,3,4,5,6,7,8};
int buzzer = 13;
int knop = 9;
int potv;
int randomnummer;
unsigned long currentMillis = millis();
boolean statusknop;
void setup() {
Serial.begin(9600);
Serial.println("Het spel start op");
pinMode(leds[0], OUTPUT);
pinMode(leds[1], OUTPUT);
pinMode(leds[2], OUTPUT);
pinMode(leds[3], OUTPUT);
pinMode(leds[4], OUTPUT);
pinMode(leds[5], OUTPUT);
pinMode(leds[6], OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(knop, INPUT_PULLUP);
pinMode(potv, INPUT);
randomSeed (analogRead(1));
}
void loop() {
Serial.println("huidige snelheid van het spel: " + String(potv) + " ms per LED");
randomnummer = random(0, 6);
randomled();
while(statusknop == LOW){
digitalWrite(leds[0], 1);
wachten();
controle();
digitalWrite(leds[0], 0);
digitalWrite(leds[1], 1);
wachten();
controle();
digitalWrite(leds[1], 0);
digitalWrite(leds[2], 1);
wachten();
controle();
digitalWrite(leds[2], 0);
digitalWrite(leds[3], 1);
wachten();
controle();
digitalWrite(leds[3], 0);
digitalWrite(leds[4], 1);
wachten();
controle();
digitalWrite(leds[4], 0);
digitalWrite(leds[5], 1);
wachten();
controle();
digitalWrite(leds[5], 0);
digitalWrite(leds[6], 1);
wachten();
controle();
digitalWrite(leds[6], 0);
}
}
void randomled(){
digitalWrite(leds[randomnummer], 1);
delay(2000);
digitalWrite(leds[randomnummer], 0);
}
void controle(){
potv = analogRead(A0);
if(statusknop == HIGH && leds[randomnummer] == HIGH){
gewonnen();
}else if(statusknop == HIGH){
verloren();
}
}
void gewonnen(){
Serial.println("gewonnen!!");
for (int i=0; i <= 8; i++){
digitalWrite(leds[3], HIGH);
digitalWrite(buzzer, HIGH);
delay(95);
digitalWrite(buzzer, LOW);
digitalWrite(leds[3], LOW);
delay(95);
}
delay(1500);
}
void verloren(){
Serial.println("verloren probeer opnieuw..");
for (int i=0; i <= 1; i++){
digitalWrite(buzzer, HIGH);
digitalWrite(leds[0], 1);
digitalWrite(leds[1], 1);
digitalWrite(leds[2], 1);
digitalWrite(leds[3], 1);
digitalWrite(leds[4], 1);
digitalWrite(leds[5], 1);
digitalWrite(leds[6], 1);
delay(500);
digitalWrite(buzzer, LOW);
digitalWrite(leds[0], 0);
digitalWrite(leds[1], 0);
digitalWrite(leds[2], 0);
digitalWrite(leds[3], 0);
digitalWrite(leds[4], 0);
digitalWrite(leds[5], 0);
digitalWrite(leds[6], 0);
delay(500);
}
}
void wachten(){
unsigned long int huidige_tijd = millis();
while(millis() - huidige_tijd < potv){
statusknop = digitalRead(knop);
}
return statusknop;
}