constexpr int OFF = HIGH;
// LEDs
constexpr int led1_pin = 13;
constexpr int led2_pin = 12;
constexpr int led3_pin = 11;
constexpr int led4_pin = 10;
// buttons
constexpr int button1_pin = A1;
constexpr int button2_pin = A2;
constexpr int button3_pin = A3;
const int buttonPin = A1; // set the button pin
int value = 0; // initialize the value to zero
int lastButtonState = HIGH; // initialize the button state
unsigned long lastDebounceTime = 0; // the last time the button was toggled
unsigned long incrementTime = 0; // the last time the value was incremented
unsigned long holdTime = 0; // the time the button has been held down
const unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
bool checker = false;
const int LED_PINS[] = {led1_pin, led2_pin, led3_pin, led4_pin};
const int BUTTON_PINS[] = {button1_pin, button2_pin};
const int LONG_PRESS_DELAY = 1000;
const int LONG_PRESS_INTERVAL = 300;
unsigned long last_press_time[] = {0, 0};
unsigned long last_update_time = 0;
unsigned long current_time = 0;
int number = -1;
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(LED_PINS[i], OUTPUT);
digitalWrite(LED_PINS[i], OFF);
}
for (int i = 0; i < 2; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
update_leds();
}
void update_leds() {
for (int i = 0; i < 4; i++) {
digitalWrite(LED_PINS[i], (number >> i) & 1);
}
}
void loop() {
current_time = millis();
update_leds();
last_update_time = current_time;
// Check if button 2 was pressed
if (digitalRead(BUTTON_PINS[1]) == LOW) {
if (millis() - last_press_time[0] >= 10000 ) {
number+=15;
last_press_time[0] = - 700;
}
} else {
last_press_time[0] = 0;
}
number %= 16;
int buttonState = digitalRead(buttonPin); // read the state of the button
if (buttonState != lastButtonState) { // if the button state has changed
lastDebounceTime = millis(); // record the time the button state changed
}
if ((millis() - lastDebounceTime) > debounceDelay) { // if the button state has been stable for the debounce time
if (buttonState == LOW) { // if the button is pressed
holdTime = millis(); // record the time the button was pressed
incrementTime = millis(); // reset the increment time
} else { // if the button is released
if (holdTime > 0) { // if the button was held
int increment = 0; // initialize the increment to zero
if ((millis() - holdTime) >= 1000) { // if the button was held for at least 1000 ms
increment = 1; // increment the value
holdTime = millis(); // reset the hold time
} else if ((millis() - incrementTime) >= 300) { // if the increment time has elapsed
increment = 1; // increment the value
incrementTime = millis(); // reset the increment time
}
if (increment) { // if the value should be incremented
value++; // increment the value
Serial.println(value); // print the value to the serial monitor
}
}
holdTime = 0; // reset the hold time
}
lastButtonState = buttonState; // save the button state
}
}
}