How to stop the timer using push button and 7 segment?

Hello, I hope everyone fine , I need to make a stopwatch using 7 segment, 74HC595 chip, push button and arduino uno ,
What i want is when i press the button, the timer stop and shows the numbers!
the code below start from 0 to 9

int DS_pin1 = 13;
int STCP_pin1 =12;
int SHCP_pin1 = 11;
void setup() {
pinMode(DS_pin1,  OUTPUT);
  pinMode(STCP_pin1, OUTPUT);
  pinMode(SHCP_pin1, OUTPUT);

void loop() {
for (int K=0;K<10;K++){  
for (int H=0;H<10;H++){
  digitalWrite(STCP_pin1,LOW);
  shiftOut(DS_pin1, SHCP_pin1, LSBFIRST,dec_digits1[H]);
  shiftOut(DS_pin1, SHCP_pin1, LSBFIRST,dec_digits1[K]);
  digitalWrite(STCP_pin1,  HIGH);
  delay(100);
}
}
} 

Thank you all


Okey. Fine.

What is happening?

There's no question to reply to.

That code does not compile and looks incomplete.....

This code run numbers from zero to nine over and over and it run fine , i need to add a push button and when I press it, the loop stop and shows the number.

I apologise if the question is not understood because I am Arabic and I am not good at English.

Thanks

No. That code runs nothing. It does not compile.

Try this link to find how to do things: Arduino Reference - Arduino Reference

Try example code for how to use a button.

Your code does not compile.
Missing setup() closing brace, ("}"),
and
'dec_digits1' was not declared in this scope.

Sorry i miss the first line , this the code


int dec_digits1  [10] {1,79,18,6,76,36,32,15,0,4};

int DS_pin = 13;
int STCP_pin =12;
int SHCP_pin = 11;
void setup() {

pinMode(BUTTON_PIN, INPUT);
pinMode(DS_pin1,  OUTPUT);
  pinMode(STCP_pin1, OUTPUT);
  pinMode(SHCP_pin1, OUTPUT);
}

void loop() {

for (int K=0;K<10;K++){  
for (int H=0;H<10;H++){
  digitalWrite(STCP_pin1,LOW);
  shiftOut(DS_pin1, SHCP_pin1, LSBFIRST,dec_digits1[H]);
  shiftOut(DS_pin1, SHCP_pin1, LSBFIRST,dec_digits1[K]);
  digitalWrite(STCP_pin1,  HIGH);
  delay(100);
}
}

}

I try this new code below for the push button but it not work


define BUTTON_PIN 7

int dec_digits1  [10] {1,79,18,6,76,36,32,15,0,4};

int DS_pin = 13;
int STCP_pin =12;
int SHCP_pin = 11;
void setup() {
pinMode(DS_pin1,  OUTPUT);
  pinMode(STCP_pin1, OUTPUT);
  pinMode(SHCP_pin1, OUTPUT);
}

void loop() {
for (int K=0;K<10;K++){  
for (int H=0;H<10;H++){
  digitalWrite(STCP_pin1,LOW);
  shiftOut(DS_pin1, SHCP_pin1, LSBFIRST,dec_digits1[H]);
  shiftOut(DS_pin1, SHCP_pin1, LSBFIRST,dec_digits1[K]);
  digitalWrite(STCP_pin1,  HIGH);
  delay(100);
}
}

if (digitalRead(BUTTON_PIN) == HIGH) {
delay(1000);
}
}

I corrected it, look at the comments, I researched and tried a lot of examples but don't work with the current code, I hope you help me

During delay, the button will not work.

Use millis() instead.

Your code does not compile again.

Several erros:

DS_pin1' was not declared in this scope
STCP_pin1' was not declared in this scope
.... and more

Here is a YouTube video describing what you want to do.

Search YouTube for “Dronebot Workshop” or “Ralph S. Bacon” and seven segment display. You will get circuit examples and code examples that will give you a good start.

No, You did not. You changed it for no use.
Start compiling the code! Else nothing will happen!
The error report shows when things are wrong.

That line is not correct. Start it with # and end it with a semikolon.

Read the reference to know how to define an input. You need to declare the input pin. INPUT_PULLUP is good. Then connect the button between the input pin and GND.

Try this code.
If you press the button the countdown stops.
If you press it again and hold it for more than 500 msec, and release the button the count restarts, and when you press the button again it stops, and so on.......

(Note: the button needs to be connected to pin 2)

"595 - Wokwi ESP32, STM32, Arduino Simulator

Thank you friend it’s work , but some time not working I don’t know why , But in case it doesn't work, the screen light drops when pressed but the number doesn't stop.

Oops.

https://cplusplus.com/doc/tutorial/preprocessor/

1 Like

Sorry to ask You but please make and post schematics showing all connections including power supplies.

Sorry, cookies are demanded, and I refuse that. Please spell it out here.
Okey, the semikolon is not needed.... (Just found out)
My reply is based on compiling the code.

The beauty of c++ and compiler directives... very confusing for newcomers

I did exactly what ruilviana is doing in the attached file , But so far, the button works sometimes and sometimes it doesn't work.

Please post this last code.
Using delay often crashes button reading.

Latest.