Help with a Simple game code

Hi
I have a project that uses 2 Buttons and a relay shield module
Im only using two relays
now i dont know how to code it
the game has two players, each player has 1 button to pass the turn to the other one
if they dont press the button in 8 seconds, the turn is passed to the other player
Each player has 1 series off leds trigger by the relay
only in the first 3 second of the program, both leds blink, otherwise only 1 relay can be turned on at the time.

All of that makes sense. When it is your turn, you press the button. If you don’t press the button in 8 seconds, it is pressed for you.

This part I don’t understand. How many LEDs? What do they do?

Is any score kept? Does the game ever end? Is there a way to win?

the led part is simply sending digital pin power to the numer 6 for one player and numer 7 for the other that ativates the relay that ativates 24 leds (for each player)

can you help me code or send me a demo one?

const byte Player1ButtonPin = 2;
const byte Player1RelayPin = 6;
const byte Player2ButtonPin = 3;
const byte Player2RelayPin = 7;

bool TurnPlayer1;  // 'true' for Player 1, 'false' for Player 2
unsigned long PlayerTurnStartTime = 0;

void setup()
{
  pinMode(Player1ButtonPin, INPUT_PULLUP);
  pinMode(Player1RelayPin, OUTPUT);

  pinMode(Player2ButtonPin, INPUT_PULLUP);
  pinMode(Player2RelayPin, OUTPUT);

  // Turns start with Player 1
  TurnPlayer1 = true;  // Player 1's turn
  digitalWrite(Player1RelayPin, HIGH);  // Turn on Player 1 lights
  digitalWrite(Player2RelayPin, LOW);  // Turn off Player 1 lights
  PlayerTurnStartTime = millis();
}

void loop()
{
  if (TurnPlayer1)
  {
    // Player 1's turn until they press their button or 8 seconds is up
    if (digitalRead(Player1ButtonPin) == LOW || millis() - PlayerTurnStartTime >= 8000)
    {
      // Change player
      digitalWrite(Player1RelayPin, LOW);  // Turn off Player 1 lights
      TurnPlayer1 = false;  // Player 2's turn
      PlayerTurnStartTime = millis();
      digitalWrite(Player2RelayPin, HIGH);  // Turn on Player 2 lights
    }
  }
  else
  {
    // Player 2's turn until they press their button or 8 seconds is up
    if (digitalRead(Player1ButtonPin) == LOW || millis() - PlayerTurnStartTime >= 8000)
    {
      // Change player
      digitalWrite(Player2RelayPin, LOW);  // Turn off Player 2 lights
      TurnPlayer1 = true;  // Player 1's turn
      PlayerTurnStartTime = millis();
      digitalWrite(Player1RelayPin, HIGH);  // Turn on Player 1 lights
    }
  }
}
1 Like

hi again i tried the code you send me but the two relays are always on

Hi,
How have you got your buttons wired?
@johnwasser code needs your buttons between gnd and the input pin, not 5V and the input pin.

Can you please post a circuit diagram?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

oh ok now works ahaahha
thanks
is easy to implement in startup like a sequence of cliks to make like a sinal to the player that the game is about to start??

You could flash the lights:

void setup()
{
  pinMode(Player1ButtonPin, INPUT_PULLUP);
  pinMode(Player1RelayPin, OUTPUT);

  pinMode(Player2ButtonPin, INPUT_PULLUP);
  pinMode(Player2RelayPin, OUTPUT);

  // Signal for attention.  Blink both lights three times and then pause one second:
  for (int i = 0; i < 3; i++)
  {
    digitalWrite(Player1RelayPin, HIGH);  // Turn on Player 1 lights
    digitalWrite(Player2RelayPin, HIGH);  // Turn on Player 2 lights
    delay(300);
    digitalWrite(Player1RelayPin, LOW);  // Turn off Player 1 lights
    digitalWrite(Player2RelayPin, LOW);  // Turn off Player 2 lights
    delay(300);
  }
  delay(700);  // Add this delay to the 300 milliseconds above to get 1 second

  // Turns start with Player 1
  TurnPlayer1 = true;  // Player 1's turn
  digitalWrite(Player1RelayPin, HIGH);  // Turn on Player 1 lights
  digitalWrite(Player2RelayPin, LOW);  // Turn off Player 2 lights
  PlayerTurnStartTime = millis();
}

you are a genius thank you so much