Need help with LED + button projekt.

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 :slight_smile:

…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 :slight_smile:

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);
}*_

I'm not sure why you code isn't working, but you may want to look at a completely different way to code it.

See this thread on using a Finite State Machine see the code posted in this thread
http://forum.arduino.cc/index.php/topic,106335.0.html
by wildbill

Hi also read,

http://forum.arduino.cc/index.php/topic,148850.0.html

to see how to post your code using tags.

Tom...... :slight_smile:

Is your code able to compile?

const int startButtonPin = 13;     // the number of the pushbutton pin
int startButtonState = 0;

const int lampeLEDPin =  12;      // the number of the LED pin
int lampeLEDPinState = LOW;

int numberOfGamers = 5;

const 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)


const 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[i], INPUT);
    pinMode(gameLEDPins[i], 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[i] = digitalRead(gameButtonPins[i]);

      if (gameButtonStates[i] == HIGH && isGameButtonPressed[i] == false) {
        isGameButtonPressed[i] = true;
        numberOfButtonsPressed = numberOfButtonsPressed + 1;
        gameLEDStates[i] = false;
      }

      if (numberOfButtonsPressed == numberToWin) {
        isGameOn = false;
        hasGameBeenPlayed = true;
        gameWinner = gameButtonPins[i];
      }
    }
  }
  else {
    lampeLEDPinState = LOW;
    numberOfButtonsPressed = 0;
    for (int i = 0; i < numberOfGamers; i ++) {
      isGameButtonPressed[i] = false;
      if (i != gameWinner) {
        gameLEDStates[i] = LOW;
      }
      digitalWrite(gameLEDPins[i], gameLEDStates[i]);
    }
  }


  if (hasGameBeenPlayed == true) {
    gameLEDStates[gameWinner] = HIGH;
  }

  for (int i = 0; i < numberOfGamers; i = i + 1) {
    digitalWrite(gameLEDPins[i], gameLEDStates[i]);
  }

  digitalWrite(lampeLEDPin, lampeLEDPinState);
}