daanme
January 11, 2024, 7:57pm
1
Hello Everyone,
I want to create a 3x 7 digit timer if I push a button then the timer must begin with counting, and when I release the button it must stop with counting en turning the display off until I push the button again.
if I use this code then it starts with counting the way I wanted. but I don't know how to make the code so the button is working?
I hope someone can help me with this code or help me in the good direction.
#include <ShiftRegister74HC595.h>
#define SDI 7
#define SCLK 6
#define LOAD 5
#define DIGITS 3
int button = A0;
ShiftRegister74HC595<DIGITS> sr(SDI, SCLK, LOAD);
int value,digit1,digit2,digit3;
uint8_t digits[] = {B11000000, //0
B11111001, //1
B10100100, //2
B10110000, //3
B10011001, //4
B10010010, //5
B10000010, //6
B11111000, //7
B10000000, //8
B10010000 //9
};
void setup() {
pinMode(button, INPUT_PULLUP);
}
void loop() {
for(int i=0; i<=999; i++)
{
showNumber(i);
delay(100);
}
delay(2000);
sr.setAllLow();
}
void showNumber(int num)
{
digit1=num % 10 ;
digit2=(num / 10) % 10 ;
digit3=(num / 100) % 10 ;
//Send them to 7 segment displays
uint8_t numberToPrint[]= {digits[digit3],digits[digit2],digits[digit1]};
sr.setAll(numberToPrint);
}
Try this code:
When you release the button, the display freezes the value for 2 seconds and then erases everything.
When you press the button it restarts from 0.
Simulated @ " 595_Start_Stop - Wokwi ESP32, STM32, Arduino Simulator
#include <ShiftRegister74HC595.h>
#define SDI 7
#define SCLK 6
#define LOAD 5
#define DIGITS 3
int button = A0;
bool flag = true;
ShiftRegister74HC595<DIGITS> sr(SDI, SCLK, LOAD);
int value, digit1, digit2, digit3;
uint8_t digits[] = {B11000000, //0
B11111001, //1
B10100100, //2
B10110000, //3
B10011001, //4
B10010010, //5
B10000010, //6
B11111000, //7
B10000000, //8
B10010000 //9
};
//------------------------------------------------------------------
void setup() {
pinMode(button, INPUT_PULLUP);
}
//------------------------------------------------------------------
void loop() {
if (digitalRead(button) == LOW)
{
flag = true;
for (int i = 0; i <= 999; i++) {
if (digitalRead(button) == HIGH)
break;
showNumber(i);
delay(100);
}
}
if (digitalRead(button) == HIGH and flag == true)
{
flag = false;
delay(2000);
sr.setAllLow();
}
}
//------------------------------------------------------------------
void showNumber(int num){
digit1 = num % 10 ;
digit2 = (num / 10) % 10 ;
digit3 = (num / 100) % 10 ;
//Send them to 7 segment displays
uint8_t numberToPrint[] = {digits[digit3], digits[digit2], digits[digit1]};
sr.setAll(numberToPrint);
}
2 Likes
kolaha
January 11, 2024, 9:46pm
3
#include <ShiftRegister74HC595.h>
#define SDI 7
#define SCLK 6
#define LOAD 5
#define DIGITS 3
const byte button = A0;
ShiftRegister74HC595<DIGITS> sr(SDI, SCLK, LOAD);
uint8_t digits[] = {
B11000000, //0
B11111001, //1
B10100100, //2
B10110000, //3
B10011001, //4
B10010010, //5
B10000010, //6
B11111000, //7
B10000000, //8
B10010000 //9
};
void setup() {
pinMode(button, INPUT_PULLUP);
}
void loop() {
while (digitalRead(button));
delay(20);
int i = 0;
while (!digitalRead(button)) {
showNumber(i);
delay(100);
i++;
}
uint32_t Mills = millis();
bool reset = true;
while (millis() - Mills < 2000) {
if (!digitalRead(button)) {
reset = false;
break;
}
}
if (reset)sr.setAllLow();
}
void showNumber(int num){
uint8_t numberToPrint[] = {digits[(num / 100) % 10], digits[(num / 10) % 10], digits[num % 10]};
sr.setAll(numberToPrint); //Send them to 7 segment displays
}
1 Like
daanme
January 12, 2024, 9:38pm
4
Thanks a lot, this is great I learned new things everyday here thanks to you people!
system
Closed
July 10, 2024, 9:38pm
5
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.