Hey Arduino Magicians!
I’m doing an assignment in school where i’m supposed to make some Arduino stuff happen’. But, I’m stock now.
…The problem is; I’ve 6 LEDs connected to the Arduino board and 6 buttons connected to the board. And the LEDs connected to 1 button each . 1 LED is the startLED connected as the startPin. The same with one of the buttons. So, whats happening is;
- when I press the startbutton the startLED turns on. This is what it is supposed to do.
- After that when I press one of the other buttons, nothing happens, but it is noticed by the arduino board. This is also what it is supposed to do.
- After that, when I press down a second button, the startLED turns of. This is also what it is supposed to do
- But, at the same time the startLED turns of, I would like the LED which is connected to the button pressed last to turn on. THIS IS NOT HAPPENING!!! And i have no idea how to make it happen.
…So please, could some one help me
…Also, if anyone has a code, that could make the startLED turn on at a random time, for each time the startButton is pressed in an interval from 3 to 10 seconds, that would be great
This is the code as it looks now:
int startButtonPin = 13; // the number of the pushbutton pin
int startButtonState = 0;
int lampeLEDPin = 12; // the number of the LED pin
int lampeLEDPinState = LOW;
int numberOfGamers = 5;
int gameButtonPins = {
3, 5, 7, 9, 11 };
int gameButtonStates = {
0, 0, 0, 0, 0 };
boolean isGameButtonPressed = {
false, false, false, false, false };
int gameWinner = 0;
int numberOfButtonsPressed = 0;
int numberToWin = 2; // Winner knappen (anden gang en knap bliver trykket)
int gameLEDPins = {
2, 4, 6, 8, 10 };
int gameLEDStates = {
LOW, LOW, LOW, LOW, LOW };
boolean isGameOn = false;
boolean hasGameBeenPlayed = false;
void setup() {
Serial.begin(9600);
pinMode(startButtonPin, INPUT);
pinMode(lampeLEDPin, OUTPUT);
int i = 0;
while(i < numberOfGamers) {
pinMode(gameButtonPins*, INPUT); *
_ pinMode(gameLEDPins*, OUTPUT);_
_ i = i + 1; _
_ }_
_}_
void loop(){
_ startButtonState = digitalRead(startButtonPin);_
_ Serial.print("isGameOn: ");_
_ Serial.println(isGameOn);_
_ if (startButtonState == HIGH && isGameOn == false) {_
_ isGameOn = true;_
_ }_
_ if(isGameOn) {_
_ lampeLEDPinState = HIGH;_
_ for(int i = 0; i < numberOfGamers; i = i + 1) {_
gameButtonStates _= digitalRead(gameButtonPins);
if(gameButtonStates == HIGH && isGameButtonPressed == false) {
isGameButtonPressed = true;
numberOfButtonsPressed = numberOfButtonsPressed + 1;
gameLEDStates = false;
}
if(numberOfButtonsPressed == numberToWin) {
isGameOn = false;
hasGameBeenPlayed = true;
gameWinner = gameButtonPins;
}
}
}
else {
lampeLEDPinState = LOW;
numberOfButtonsPressed = 0;
for(int i = 0; i < numberOfGamers; i ++) {
isGameButtonPressed = false;
if (i != gameWinner) {
gameLEDStates = LOW;
}
digitalWrite(gameLEDPins, gameLEDStates);
}
}
if(hasGameBeenPlayed == true) {
gameLEDStates[gameWinner] = HIGH;
}
for(int i = 0; i < numberOfGamers; i = i + 1) {
digitalWrite(gameLEDPins, gameLEDStates);
}
digitalWrite(lampeLEDPin, lampeLEDPinState);
}*_