Multi Button LED Help for Hockey

I posted the basically the following post in the wrong forum section I believe. I couldn't delete it, so I hope a moderator can for me.....

Hello, I'm a total newbie to this site as well as programming in general. There is definitely a lot of useful info on here. I've checked out some of the information regarding timers involving LEDs, but I haven't found anything that specifically answered what I'm looking to do. I recently bought a table hockey game, and I want to simulate what happens at an actual hockey game.

I'd like to use a breadboard for this project, but please let me know if using the Arduino itself would be easier. I apologize for not being very knowledgeable in this subject matter. In terms of lights, I only plan on using one green LED and one red LED for this project. Basically, I want to press a button on the breadboard and have a green LED be off for 180 seconds and then turn on for 5 seconds to simulate the end of a period, and then turn off. To start the next cycle, I'd like to just push the same button. When a goal is scored, I'd like to press a button on the breadboard to make the red LED turn on instantly for 5 seconds and then turn back off, and not interrupt the 180 second timer for the green LED.

I've read so many forums on here and am still confused. Can anyone help me with this project? Any help for developing this code would be greatly appreciated, and I apologize if I have offended anyone with this post for my lack of experience.

Hello and welcome,

Your project looks simple, but I understand that if you are very new to scripting, it can be confusing.

So here is an example that will do the first part that you described. Study it carefully. Once you understood how it works, the second part should be very easy to add to this code.

#include <Button.h> // https://github.com/JChristensen/Button

const uint8_t button1Pin  =  2; // Connect a button from pin 2 to GND
const uint8_t greenLedPin = 13; // Use the build-in LED for this example

Button button1( button1Pin, true, true, 10 );


void setup()
{
  Serial.begin( 9600 );
  pinMode( greenLedPin, OUTPUT );
}


void loop()
{
  uint32_t currentMillis = millis();
  static uint32_t button1ReleasedMillis = 0;
  static bool button1Released = false;
  static uint32_t greenLedStartMillis = 0;
  static bool greenLedOn = false;
  
  button1.read();

  if ( button1.wasReleased() )
  {
    Serial.println( "button1 released -> turning green LED off if needed, and starting 180 seconds timer..." );
    
    if ( greenLedOn )
    {
      greenLedOn = false;
      digitalWrite( greenLedPin, LOW );
    }
    button1ReleasedMillis = currentMillis;
    button1Released = true;
  }

  if ( button1Released && currentMillis - button1ReleasedMillis >= 180000UL )
  {
    Serial.println( "180 seconds timer finished -> turning green LED on for 5 seconds..." );
    
    digitalWrite( greenLedPin, HIGH );
    greenLedStartMillis = currentMillis;
    greenLedOn = true;
    button1Released = false;
  }

  if ( greenLedOn && currentMillis - greenLedStartMillis >= 5000UL )
  {
    Serial.println( "5 seconds timer finished -> turning green LED off..." );
    
    greenLedOn = false;
    digitalWrite( greenLedPin, LOW );
  }
}

Good luck :slight_smile:

I recommend that you forget about timers for now and begin with the simple "button" sketch that is included with the Arduino software, there is a companion tutorial on the site. The sketch is located at File->Examples->Digital->Button. The tutorial shows how to hook up the button on a breadboard.

The example sketch uses the LED built into the Arduino board. Your second step should be to use your green or red LED instead, so that you push a button and the sketch turns on the LED.

Once that is working, add your second button to turn on the other LED. If you understand how it works for one LED, it will be a matter of copying / pasting and renaming variables to add the second.

At the point were you have a sketch that reads two buttons and controls two LEDs you can add the timing code for your hockey game.

Thank you all very much for your help, including with the coding. On the second button for my breadboard, how would I connect that to ground for Arduino since I think it only has one ground port? I haven’t received my kit that I ordered yet, so I’m just looking to get a head start, so I’ve used Fritzing a little bit. Any help on setting up my breadboard with that I’d really appreciate as well. Would I just add this code this exact code to the end of the green LED code you made or what? Thank you all again. As far as the coding for the red LED, here it goes, but I know I’m probably wrong at some point…

#include <Button.h> // https://github.com/JChristensen/Button

const uint8_t button2Pin = 7; // Connect a button from pin 7 to GND?
const uint8_t redLedPin = 12; //

Button button2( button2Pin, true, true, 10 );

void setup()
{
Serial.begin( 9600 );
pinMode( redLedPin, OUTPUT );
}

void loop()
{
uint32_t currentMillis = millis();
static uint32_t button2ReleasedMillis = 0;
static bool button2Released = false;
static uint32_t redLedStartMillis = 0;
static bool redLedOn = false;

button2.read();

if ( button2.wasReleased() )
{
Serial.println( “button2 released → turning green LED off if needed, and starting 180 seconds timer…” );

if ( redLedOn )
{
redLedOn = true;
digitalWrite( redLedPin, HIGH);
}
button2ReleasedMillis = currentMillis;
button2Released = true;
}

if ( button2Released && currentMillis - button2ReleasedMillis >= 500UL )
{
Serial.println( “turning red LED on for 5 seconds…” );

digitalWrite( redLedPin, HIGH );
redLedStartMillis = currentMillis;
redLedOn = true;
button2Released = false;
}

if ( redLedOn && currentMillis - redLedStartMillis >= 500UL )
{
Serial.println( “5 seconds timer finished → turning red LED off…” );

redLedOn = false;
digitalWrite( redLedPin, LOW );
}
}

if ( button2.wasReleased() )
  {
    Serial.println( "button1 released -> turning green LED off if needed, and starting 180 seconds timer..." );

If button 2 was released, print a message saying that button 1 was released. I see. Not.

Edited, thanks!

On the second button for my breadboard, how would I connect that to ground for Arduino since I think it only has one ground port?

Which Arduino will you be using ? Most have several ground pins on board but even if there were only one you could connect it to a row of sockets on the breadboard and connect several switches to that.

It would be the Uno R3, so would I need to go the socket route?

You mentioned a breadboard. You can join all the ground connections on that.

Thanks, yeah I just watched a demonstration of that. Basically two wires, one going into the 5V supply, and one going into a ground pin, and plug up a USB cable, and I have power, correct?

Breadboards normally have long contact strips along their edges intended to be used a 5V and GND bus bars to distribute power to components on the breadboard. A convenient source of 5V and GND are the pins on the Arduino but be careful not to exceed the power available from them.

One "gotcha" is that some breadboards have a gap in the middle of the bus bars that has to be bridged if power is required along the length of the breadboard.

Thank you. I ordered what seemed to be a rather typical 400 hole breadboard, so hopefully, I won't have that problem.

Here is the inner of a typical breadboard:

Edit: just in case you wonder how it works :smiley:

I have a small idea of how it does from the numerous videos I watched haha, but yeah, that's the kind of board I ordered. For this project, hopefully I won't have the problems UKHeliBob mentioned. Almost all of the videos I've watched with buttons have had a button that crosses over that big gap I believe Bob was talking about. For this project, would I have to have both buttons to cross over the gap? And, even if I wouldn't have that problem in this project, what would I have to do in order to power both sides? You all have been major helps to me, and I was wondering if anyone could help me actually diagram how I should do this project where both LEDs on the breadboard? Thank you all again.

Almost all of the videos I've watched with buttons have had a button that crosses over that big gap I believe Bob was talking about.

Wrong gap. The big gap is there deliberately to separate the halves of the breadboard. The gaps that I meant are in the bus bars along the top and bottom edges of the diagram in post #12

Oh ok, I guess the only way to be sure if I need to bridge that gap is to actually try it out then, right?

Here is an example breadboard for the code in my other post. Click for bigger image... Resistor is 220 Ohm.

GoBraves21:
Oh ok, I guess the only way to be sure if I need to bridge that gap is to actually try it out then, right?

If you look carefully at the breadboard the lines are usually broken at the point they need bridging.

breadboard with split bus bars

Ahhh, I get it now UKHeliBob. Thank you very much!

Thank you as well for that diagram guix! Super helpful! By the way, how did the code for the red LED light look? I know that some of my descriptions are off, but what about the code itself? Thanks again!

I'm not sure.. you just copied my code and changed the variables names, and that doesn't reflects what you explained for the second part:

When a goal is scored, I'd like to press a button on the breadboard to make the red LED turn on instantly for 5 seconds and then turn back off, and not interrupt the 180 second timer for the green LED.

You can only have one setup() and one loop() in a sketch, so you have to take my code, and add things to it.

Think again and try harder, it's really not that hard especially since I did 3/4 of the work for you... :smiley_cat: