Hello everyone!
Me and my friend are trying to create a project at school, and we have run into an issue that we can't find a solution to on our own. We tried googling for a few hours trying to figure out how to do it, but that hasn't helped us unfortunately.
Basicly we are trying to make a game where two players compete against eachother, trying to react fastest.
We have two buttons installed, one for each player.
Both players have 3 LED's to represent their health.
When the LED in the middle flashes, the player who pushes his button first, turns off one of the opponent players LEDs.
The issue we're having makes it possible for the players to keep holding the button down. We are trying to implement a delay. (If you push the button and the LED in the middle is not flashing, you get a delay.).
We don't want to use the Delay() function, as it causes issues with the other parts of our program.
We use millis(); for randomly turning the LED in the middle on.
This is our code so far:
const int Pl1liv1 = 13; // player1 life
const int Pl1liv2 = 12; // player1 life
const int Pl1liv3 = 11; // player1 life
const int Pl2liv1 = 10; // player2 life
const int Pl2liv2 = 9; // player2 life
const int Pl2liv3 = 8; // player2 life
const int vinder = 6; // LED to represent a player winning.
const int delayPl1 = 5; // LED to represent delay for player1
const int delayPl2 = 4; // LED to represent delay for player2
const int lampe = 3; // the LED in the middle.
const int Pl1skyd = 2; // Player 1 button.
const int Pl2skyd = 7; // Player 2 button.
const int reset = 0;
int Pl1liv = 3;
int Pl2liv = 3;
unsigned long previousMillis = 0;
long interval = random(3000,6000);
int buttonState1 = 0;
int buttonState2 = 0;
int lampestate = LOW;
void setup() {
Serial.begin(9600);
pinMode(Pl1liv1, OUTPUT);
pinMode(Pl1liv2, OUTPUT);
pinMode(Pl1liv3, OUTPUT);
pinMode(Pl2liv1, OUTPUT);
pinMode(Pl2liv2, OUTPUT);
pinMode(Pl2liv3, OUTPUT);
pinMode(vinder, OUTPUT);
pinMode(delayPl1, OUTPUT);
pinMode(delayPl2, OUTPUT);
pinMode(lampe, OUTPUT);
pinMode(Pl1skyd, INPUT);
pinMode(Pl2skyd, INPUT);
pinMode(reset, INPUT);
randomSeed(analogRead(0));
}
void loop() {
unsigned long currentMillis = millis();
buttonState1 = digitalRead(Pl1skyd);
buttonState2 = digitalRead(Pl2skyd);
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (lampestate == LOW){
lampestate = HIGH;
} else {
lampestate = LOW;
}
digitalWrite(lampe, lampestate);
}
if (Pl1liv == 3)
{
digitalWrite (Pl1liv1, HIGH);
digitalWrite (Pl1liv2, HIGH);
digitalWrite (Pl1liv3, HIGH);
}
else if (Pl1liv == 2)
{
digitalWrite (Pl1liv1, HIGH);
digitalWrite (Pl1liv2, HIGH);
digitalWrite (Pl1liv3, LOW);
}
else if (Pl1liv == 1)
{
digitalWrite (Pl1liv1, HIGH);
digitalWrite (Pl1liv2, LOW);
digitalWrite (Pl1liv3, LOW);
}
else if (Pl1liv == 0)
{
digitalWrite (Pl1liv1, LOW);
digitalWrite (Pl1liv2, LOW);
digitalWrite (Pl1liv3, LOW);
digitalWrite (vinder, HIGH);
digitalWrite (delayPl1, HIGH);
}
if (Pl2liv == 3)
{
digitalWrite (Pl2liv1, HIGH);
digitalWrite (Pl2liv2, HIGH);
digitalWrite (Pl2liv3, HIGH);
}
else if (Pl2liv == 2)
{
digitalWrite (Pl2liv1, HIGH);
digitalWrite (Pl2liv2, HIGH);
digitalWrite (Pl2liv3, LOW);
}
else if (Pl2liv == 1)
{
digitalWrite (Pl2liv1, HIGH);
digitalWrite (Pl2liv2, LOW);
digitalWrite (Pl2liv3, LOW);
}
else if (Pl2liv == 0)
{
digitalWrite (Pl2liv1, LOW);
digitalWrite (Pl2liv2, LOW);
digitalWrite (Pl2liv3, LOW);
digitalWrite (vinder, HIGH);
digitalWrite (delayPl2, HIGH);
}
if (buttonState1 == HIGH && lampestate == HIGH)
{
Serial.println(Pl2liv);
Pl2liv--;
buttonState1 = LOW;
lampestate = LOW;
digitalWrite(lampe, LOW);
Serial.println(buttonState1);
Serial.println(lampestate);
}
if (buttonState2 == HIGH && lampestate == HIGH)
{
Serial.println(Pl1liv);
Pl1liv--;
buttonState2 = LOW;
lampestate = LOW;
digitalWrite(lampe, LOW);
Serial.println(buttonState2);
Serial.println(lampestate);
}
}
Basicly we need help implementing a way to count. If player1 pushes the button when the LED (lampe) is not high. He needs to get a delay for 1 sec (perhaps a variable changing from 1 to 0 after a second)
Here is a picture of our board.
Thank you for taking your time to read through this post, hope that you can help