Basketball Shot Clock

I was wondering if someone can point me in the right direction. I am trying to create a basketball shot clock. The clock should count down from 30 sec to 0.

I want to have 2 buttons

Start / Pause

The part i'm stuck on is how to pause the mill count and start counting down again when start / pause button is pressed.

My long term goal is to add 7 segment displays to show the count down. Also making it wireless using xbees would be nice. I want to take it one step at a time though.

Thanks for the help!

Assuming that you don't need national league accuracy, you can get a decent resolution by noting the result from millis() when the pause button was pressed, the result when the resume button was pressed, and subtract the difference between them from the total count.

How can you exit and re-enter the loop when the button is pressed?

How can you exit and re-enter the loop when the button is pressed?

You don't - there's always a loop, but in it you decide whether to track time, or stay idle, or whatever (using ifs, inner loops, sleep states or anything else you like)

Look up "state machine"

State machine is not the complete answer.
Need to basically reset millis capture so that when shot clock is paused, and then restarted, the time difference accumulates from the latest stop point and not from the shot clock was originally started.

I have been trying to wrap my head around this code. I can't seem to do it.

Would it be possible to post a little bit of sample code to help me out.


Yes, but not until I get home.
Read button press to start the clock - capture millis()
Using blink without delay, you watch millis go by, comparing the currentMillis to a number that is 1000 millis away, updating the output when the currentMillis reaches the future number - then the future number is updated for the next future number.
If the pause button is pressed, you capture millis again and store how much time is left to reach the future number.
When unpause is pressed, you capture millis again and reset the future number to watch for, then keep going.

Thank you!

So far I have been able to get the project to count down. I am unsure how to have is pause/resume and reset based on button presses.

Here is what I have so far

long current_millis;
long previous_millis;
int count = 30;
int duration = 1000;
int button_state = 0;
int previous_button = 0;

void setup(){


void loop(){
current_millis = millis();

if((current_millis - previous_millis)>= duration){
previous_millis = current_millis;
count = count --;

Any help would be greatly appreciated.

Start by not counting until the start button is pressed.
Set a flag to track that you are active counting mode.

If active counting is going on, read the stop button.
When it is pressed, stop counting.
If reset is pressed, put count back to 30.
If start is pressed, continue counting down; if count gets to 0, stop, and wait for reset.

I'mm sorry but I'm not sure how to write the code for that. Thats the part I'm hung up on. It seems easy but I cannot wrap my head around the commands needed.

I'd write it out, but I'm heading out the door and will be assembling cards when I get home. Will see if I can put something together you can follow later this weekend.

Thank you very much!

You press start and the timer counts up as you would expect.

When you press pause, you store that time difference.
When you press un-pause, you reset and start the timer, count up like expected.
The two are added together.

if you never press pause, then one value is zero so you add zero, ie: only the current counter has a value.

press stop and all counters clear or the sum is held until you re-set.

you could re-use the start or stop buttons as pause and have to hold one for 2-3 seconds to reset.

Try this to start - understand how the flags and button pushes are working

unsigned long current_millis;
unsigned long previous_millis;
int count = 30;
unsigned long duration = 1000UL;
int button_state = 0;
int previous_button = 0;
byte startButton = 2;
byte running;

void setup(){
pinMode (startButton, INPUT_PULLUP); // button close connects pin to Gnd

void loop(){
current_millis = millis();
if (running == 0 and digitalRead(button) == 0){
running = 1;
startTime = current_millis;
previousTime = startTime;
if (running == 1 and (startTime - previousTime) >= 100){ // allow time to stop
running = 0;
stopTime = current_millis;
 if(running == 1 && (current_millis - previous_millis)>= duration){
   previous_millis = current_millis;
   count = count --;

Thank you,

I think I am starting to understand I will try plying around with the code.

Slightly on topic, so please excuse, I'm working on a similar project (shot clock) that is free to use and allows for the updates made in the last season by FIBA to the rules for shot clocks.

I've created an Android, iOS, Windows and Mac OS X Application using Delphi based on AppTethering, which works on a bluetooth like protocol. I'm thinking about doing some bluetooth work with an Arduino board with it as well (as well as on another project), but I'll post a fresh thread about that, I just wanted to share this in case it was of interest.