// Forum: https://forum.arduino.cc/t/esp32-counter-getting-stuck/1141613
// Sketch by: blenks9
#include "SevSeg.h" /*Include seven segment library*/
SevSeg sevseg; /*Seven Segment Variable*/
int state1; /*Variable to store pushbutton state*/
int state2; /*Variable to store pushbutton state*/
int state3;
int count = 0; /*Variable that will store counter value*/
const int ledPin = 13;
#define button1 23 /*ESP32 pin for pushbutton */
#define button2 22 /*ESP32 pin for pushbutton */
void setup() {
pinMode(button1, INPUT_PULLUP); /*Assign button as input*/
byte sevenSegments = 1; /*Number of seven segments we are using*/
byte CommonPins[] = {}; /*Define common pins*/
byte LEDsegmentPins[] = {15, 2, 4, 5, 18, 19, 21}; /*ESP32 digital pins defined for seven segment sequence pin a to g*/
bool resistorsOnSegments = true;
sevseg.begin(COMMON_ANODE, sevenSegments, CommonPins, LEDsegmentPins, resistorsOnSegments);/*configuration of the seven-segment */
sevseg.setBrightness(80); /*Brightness of seven segment*/
pinMode(button2, INPUT_PULLUP); /*Assign button as input*/
pinMode(ledPin, OUTPUT);
}
void loop() {
state1 = digitalRead(button1); /*Read pushbutton state*/
if (state1 == LOW) { /*LOW state when pushup button is pressed*/
count--; /*Increase display value by 1*/
sevseg.setNumber(count); /*display the count value*/
sevseg.refreshDisplay(); /*refresh 7-segment */
delay(200);
}
sevseg.setNumber(count);/*display the count value*/
sevseg.refreshDisplay();/* refresh 7-segment*/
state2 = digitalRead(button2); /*Read pushbutton state*/
if (state2 == LOW) { /*LOW state when pushup button is pressed*/
count = 9;
} /*Increase display value by 1*/
if (count == 1) {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(500); // Wait for 500 milliseconds
digitalWrite(ledPin, LOW); // Turn off the LED
delay(500); // Wait for another 500 milliseconds
}
}