Basketball Shot Clock and Game Clock in a Wireless Basketball Scoreboard

Good day to all!!

I am new to programming and I am trying to program a basketball shot clock and a game clock. I am doing the code for the Basketball Shot clock before the Game clock. I googled for such codes and i didnt find some codes that is much related to what i desired.

By the way i have 3 buttons for the Shot clock, 1st button to countdown from 24sec to 0 , 2nd button is to countdown 14 sec to 0, and the last button is to pause and resume countdown. i am doing it wirelessly using xbee s2c.

The display is a 12 v 2 digit multiplexed seven segment. but i am trying it first to appear in the serial monitor.

The problem is that when i pushed the first button it countdowns from 24 to 0 in the serial monitor and upon pressing the 2nd button, the countdown is still from 24 to 0 and not 14-0. also i want to pause the countdown and resume countdown but i find it difficult on what function is to be used.

This is the code for the display

const int SER=8;  
const int LATCH=9; 
const int CLK=10;   
int j;
int m;

const byte seg[] ={0b00111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01101111,
0b00111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01101111,
0b00111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01101111};
const byte seg1[] ={0b00111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01101111};


void setup() {
  Serial.begin(9600);

  
  pinMode(SER, OUTPUT);
  pinMode(LATCH, OUTPUT);
  pinMode(CLK, OUTPUT);
  
  digitalWrite(LATCH, LOW);
  shiftOut(SER, CLK, MSBFIRST, seg[4]);    
  shiftOut(SER, CLK, MSBFIRST, seg1[2]);      
  digitalWrite(LATCH, HIGH);  
}

void loop() {
    if(Serial.available()>0)
  {
    char inChar=Serial.read();
    while(Serial.available()<=0);

//24 seconds
   if(inChar=='P')

 {
for(j=24; j>=0; j--)
 {
  digitalWrite(LATCH, LOW);
  shiftOut(SER, CLK, MSBFIRST, seg[j%10]);    // ones
  shiftOut(SER, CLK, MSBFIRST, seg1[j/10]);   // tens   
  digitalWrite(LATCH, HIGH);
  Serial.println(j);
    delay(1000); 
}
    }
    
//14 seconds
if(inChar=='Q')
{
for ( m=14; m>=0; m--)
 {
  digitalWrite(LATCH, LOW);
  shiftOut(SER, CLK, MSBFIRST, seg[m%10]);    // ones
  shiftOut(SER, CLK, MSBFIRST, se23[m/10]);   // tens   
  digitalWrite(LATCH, HIGH);
  Serial.println(m);
  delay(1000); // wait a second before incrementing

}  
   
    }

    
  }
}

and this is the code for the controller

const int button1= 2;    
const int button2=3;

 
int button1State;  
int lastbutton1State = LOW;   
int button2State;
int lastbutton2State = LOW;





long lastbutton1DebounceTime = 0; 
long debouncebutton1Delay = 50;    
long lastbutton2DebounceTime = 0;  
long debouncebutton2Delay = 50;    


void setup() {
  
  Serial.begin(9600);
  pinMode(sbutton1, INPUT);
  pinMode(button2, INPUT);

}


void loop() {
  // read the state of the switch into a local variable:
  int readingbutton1 = digitalRead(button1);
  int readingbutton2 = digitalRead(button2);

//24 seconds
  if (readingbutton1 != lastbutton1State) {
    lastbutton1DebounceTime = millis();
  }

  if ((millis() - lastbutton1DebounceTime) > debouncebutton1Delay)
  {
    if (readingbutton1 != button1State)
    {
      button1State = readingbutton1;
      if (button1State == HIGH && button2State==LOW)
      {
    Serial.println('P');
      }
    }
  }

//14 seconds
  if (readingbutton2 != lastbutton2State) {
    lastbutton2DebounceTime = millis();
  }

  if ((millis() - lastbutton2DebounceTime) > debouncebutton2Delay)
  {
    if (readingbutton2 != button2State)
    {
      button2State = readingbutton2;
      if (button2State == HIGH && button1State==LOW)
      {
    Serial.println('Q');
      }
    }
  }

  lastbutton1State = readingbutton1;
  lastbutton2State = readingbutton2;
 
}

I dont have any program code for the pause and resume button because i am having a problem with this one. can anyone help me pls.

Read and understand the doing several things at once example.

How do you think your for loop counting down from 24 to 0 with the built in 1 sec pause will get magically stopped if you press on the button? Your code won't even see a new message has arrived until it has finished the countdown.

if(inChar=='P')

 {
for(j=24; j>=0; j--)
 {
  digitalWrite(LATCH, LOW);
  shiftOut(SER, CLK, MSBFIRST, seg[j%10]);    // ones
  shiftOut(SER, CLK, MSBFIRST, seg1[j/10]);   // tens   
  digitalWrite(LATCH, HIGH);
  Serial.println(j);
    delay(1000); 
if(j==0){
break;
}
    }
}

this is the code where the countdown from 24 to 0 will stop at 0. But i cant do the thing that when 24 seconds is counting down and when i pressed the 14 seconds button the countdown will now 14 seconds to 0 and vice versa.

thank you for the help J-M-L.

How would j ever become to zero in your countdown loop besides by looping and hitting 0 with the j--, which then would exit the loop... your break statement is totally useless.

Again read the example I pointed you to, get rid of the delay and let the loop() function be the controller of the countdown, by checking at every pass if you have received a new command. Use millis() etc... --> see the example.

Good day arduino community! i badly needed your help. i have codes for looping the 24 countdown shotclock using sk6812 led strip but i don't have the idea on how to code for the buttons for reset, go/stop, and the 14 seconds countdown. i wish to have a controller that works in wifi connection where the 7segment display will be the client and the controller will be the server. Can anyone help me and guide me through this project? what will i do next? i have very poor knowledge in coding, hope you can answer my dumb questions :slight_smile: Big thanks

here's the code :slight_smile:

joemar_codex.ino (4.22 KB)

i have codes for looping the 24 countdown shotclock using sk6812 led strip

What the heck does "looping the shotclock" mean?

but i don't have the idea on how to code for the buttons for reset, go/stop, and the 14 seconds countdown.

How are the switches wired? Can you determine when each changes state? Can you describe exactly what you want each switch to do, any better than "looping the shotclock"?

what i mean sir for the looping the shotclock is i have it working after i plug in the arduino and the led strips connected it will countdown from 24 to 0 then back to 24 then 0. then again and again. My problem is that i don;t know how to have buttons for start/stop RESET and for 14 sec countdown. Please sir i badly needed your help :slight_smile: Big thanks sir!

By the way sir the 7segment display are wired similar to this

Imgur

i used 7 leds per segment.

I want to have 3 buttons sir.

1 button for START/STOP
1 button for RESET
1 button for 14sec COUNTDOWN

How will i add the codes for this functions sir? BIG THANKS!!! :slight_smile:

How will i add the codes for this functions sir?

With a text editor.

You seem to have an aversion to answering questions. There is no sense in replying if you won't tell us how the switches are wired. What pins are they connected to? Post a schematic.

Post the code you are trying to edit.

Pretend that we have the attention span of a ADHD three year old. There are so many posts each day that it is easy to forget what your issue is, among the hundreds I, and many others, read each day.

good day sir +PaulS can i ask if the ESP wifi shield client and server mode can have stable communication? i tired to have the controller as server and it sends the command but the receiver (CLIENT) takes time to process it. And i think the connection was lost during transmission after how many seconds. How will i solve the delay in the transmission? can i use wifi library to make it stable? and what wifi library will i use? BIG THANKS SIR!

by the way sir here are the AT commands that i used in configuring the client and server mode.

CLIENT MODE

AT+CWMODE=1
AT+CIPMUX=1
AT+CWJAP
AT+CIPSTART

SERVER MODE

AT+CWMODE=3
AT+CIPMUX=1
AT+CIPSERVER
AT+CIPSTO=7200

can i ask if the ESP wifi shield client and server mode can have stable communication?

Sure. And Trump can be elected president. Doesn't mean that it is a likely event, or one that will ever be repeated.

i tired to have the controller as server and it sends the command but the receiver (CLIENT) takes time to process it.

Servers do not send commands. Clients make requests. Servers execute the requests, and provide responses.

And, of course, the whole process is slow.

XBees would have been my preferred solution.

Can i ask how will i make the connection and the transmission good? i really need to do the esp to esp communication because that was the requirement of my thesis project. thank you sir. :slight_smile: Is there any at commands that i missed?

Can i ask how will i make the connection and the transmission good?

That comes down to the reliability of the hardware and software you are using. I can't help you with either of them.

Can you suggest sir what wifi library is good to use? Is adding a memory to the ram of the esp8266 server will solve the problem? Big thanks sir! :slight_smile:

sir i tried to used cipsta but the response is ERROR. how will i correct this? thanks!

Hi dears

looking for your help,

I had made a backward timer with the help of the internet, I just want to add this 24-sec shot clock along with that existing program and also want to pause both timer and short clock program

hereby I enclosed you the program and the circuit diagram which I used,

const int clk_ML = 2;
const int clk_MR = 3;
const int clk_SL = 4;
const int clk_SR = 5;
const int rst_ML = 6;
const int rst_MR = 7;
const int rst_SL = 8;
const int rst_SR = 9;
const int DE_ML = 10;
const int DE_MR = 11;
const int DE_SL = 12;
const int DE_SR = 13;
const int inc = A0;
const int ok = A1;
const int buzzer = A2;
int i = 0;
int j = 0;
int var = 0;
int var1 = 0;
int var2 = 0;
int var3 = 0;
int var4 = 0;
int secR = 0;
int secL = 0;
int MinL = 0;
int MinR = 0;
int X = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
boolean Setup = true;
boolean Stop = false;
void setup()
{
pinMode(clk_ML, OUTPUT);
pinMode(clk_MR, OUTPUT);
pinMode(clk_SL, OUTPUT);
pinMode(clk_SR, OUTPUT);
pinMode(rst_ML, OUTPUT);
pinMode(rst_MR, OUTPUT);
pinMode(rst_SL, OUTPUT);
pinMode(rst_SR, OUTPUT);
pinMode(DE_ML, OUTPUT);
pinMode(DE_MR, OUTPUT);
pinMode(DE_SL, OUTPUT);
pinMode(DE_SR, OUTPUT);
pinMode(inc, INPUT_PULLUP);
pinMode(ok, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
all_rst();
digitalWrite(DE_ML, HIGH);
digitalWrite(DE_MR, HIGH);
digitalWrite(DE_SL, HIGH);
digitalWrite(DE_SR, HIGH);
delay(500);
Display_Test();
}
void loop()
{
if (Setup)
{
digitalWrite(DE_ML, LOW);
digitalWrite(DE_MR, LOW);
digitalWrite(DE_SL, LOW);
digitalWrite(DE_SR, HIGH);
while (digitalRead(ok) != LOW)
{
if (digitalRead(inc) == LOW)
{
X = clk_SR;
secR = secR + 1;
c_inc();
delay(250);
if (secR > 9)
{
secR = 0;
}
}
}
delay(250);
digitalWrite(DE_ML, LOW);
digitalWrite(DE_MR, LOW);
digitalWrite(DE_SL, HIGH);
digitalWrite(DE_SR, LOW);
while (digitalRead(ok) != LOW)
{
if (digitalRead(inc) == LOW)
{
X = clk_SL;
secL = secL + 1;
c_inc();
if (secL > 5)
{
secL = 0;
digitalWrite(rst_SL, HIGH);
digitalWrite(rst_SL, LOW);
}
delay(250);
}
}
delay(250);
digitalWrite(DE_ML, LOW);
digitalWrite(DE_MR, HIGH);
digitalWrite(DE_SL, LOW);
digitalWrite(DE_SR, LOW);
while (digitalRead(ok) != LOW)
{
if (digitalRead(inc) == LOW)
{
X = clk_MR;
MinR = MinR + 1;
c_inc();
delay(250);
if (MinR > 9)
{
MinR = 0;
}
}
}
delay(250);
digitalWrite(DE_ML, HIGH);
digitalWrite(DE_MR, LOW);
digitalWrite(DE_SL, LOW);
digitalWrite(DE_SR, LOW);
while (digitalRead(ok) != LOW)
{
if (digitalRead(inc) == LOW)
{
X = clk_ML;
MinL = MinL + 1;
c_inc();
delay(250);
if (MinL > 9)
{
MinL = 0;
}
}
}
digitalWrite(DE_MR, HIGH);
digitalWrite(DE_SL, HIGH);
digitalWrite(DE_SR, HIGH);
var1 = secR;
var2 = secL;
var3 = MinR;
var4 = MinL;
Setup = false;
}
var1 = var1 - 1;
if (var1 == -1 && var2 == 0 && Stop == false)
{
var1 = 9;
var2 = 5;
var3 = var3 - 1;
}
if (var1 == -1 && Stop == false)
{
var2 = var2 - 1;
var1 = 9;
}
if (var3 == -1)
{
var3 = 9;
var4 = var4 - 1;
}
if (var1 == 9 && var2 == 5 && var3 == 9 && var4 == -1)
{
Stop = true;
buzz();
}
while (Stop)
{
digitalWrite(DE_ML, LOW);
digitalWrite(DE_MR, LOW);
digitalWrite(DE_SL, LOW);
digitalWrite(DE_SR, LOW);
delay(300);
digitalWrite(DE_ML, HIGH);
digitalWrite(DE_MR, HIGH);
digitalWrite(DE_SL, HIGH);
digitalWrite(DE_SR, HIGH);
delay(300);
}
delay(1000);
digitalWrite(rst_SR, HIGH);
digitalWrite(rst_SR, LOW);
for (a = 0; a < var1; a++)
{
digitalWrite(clk_SR, HIGH);
digitalWrite(clk_SR, LOW);
}
digitalWrite(rst_SL, HIGH);
digitalWrite(rst_SL, LOW);
for (b = 0; b < var2; b++)
{
digitalWrite(clk_SL, HIGH);
digitalWrite(clk_SL, LOW);
}
digitalWrite(rst_MR, HIGH);
digitalWrite(rst_MR, LOW);
for (c = 0; c < var3; c++)
{
digitalWrite(clk_MR, HIGH);
digitalWrite(clk_MR, LOW);
}
digitalWrite(rst_ML, HIGH);
digitalWrite(rst_ML, LOW);
for (d = 0; d < var4; d++)
{
digitalWrite(clk_ML, HIGH);
digitalWrite(clk_ML, LOW);
}
}
void Display_Test()
{
all_rst();
var = 10;
for (j = 0; j < 10; j++)
{
var = var - 1;
for (i = 0; i < var; i++)
{
digitalWrite(clk_ML, HIGH);
digitalWrite(clk_MR, HIGH);
digitalWrite(clk_SL, HIGH);
digitalWrite(clk_SR, HIGH);
digitalWrite(clk_ML, LOW);
digitalWrite(clk_MR, LOW);
digitalWrite(clk_SL, LOW);
digitalWrite(clk_SR, LOW);
}
delay(250);
all_rst();
}
}
void all_rst()
{
digitalWrite(rst_ML, HIGH);
digitalWrite(rst_MR, HIGH);
digitalWrite(rst_SL, HIGH);
digitalWrite(rst_SR, HIGH);
digitalWrite(rst_ML, LOW);
digitalWrite(rst_MR, LOW);
digitalWrite(rst_SL, LOW);
digitalWrite(rst_SR, LOW);
}
void c_inc()
{
digitalWrite(X, HIGH);
digitalWrite(X, LOW);
}
void buzz()
{
for (e = 0; e < 10; e++)
{
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
delay(100);
}
}