Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int buttonPin = 2;
- bool lastButtonState = false;
- unsigned long pressStartTime = 0;
- unsigned long lastReleaseTime = 0;
- bool mode1 = false;
- bool mode2 = false;
- int option1 = 0;
- int option2 = 0;
- const unsigned long longPressThreshold = 3000; // 3 seconds to enter Mode 1
- const unsigned long veryLongPressThreshold = 5000; // 5 seconds to exit Mode 1
- const unsigned long doubleClickThreshold = 300; // ms between double clicks
- void setup() {
- pinMode(buttonPin, INPUT);
- Serial.begin(9600);
- }
- void loop() {
- bool reading = digitalRead(buttonPin);
- if (reading != lastButtonState) {
- if (reading == HIGH) {
- pressStartTime = millis();
- // Check for double click
- if ((millis() - lastReleaseTime) < doubleClickThreshold) {
- if (!mode1 && !mode2) {
- mode2 = true;
- Serial.println("Entered Mode 2");
- } else if (mode2) {
- mode2 = false;
- Serial.println("Exited Mode 2");
- }
- }
- } else {
- // Button released
- unsigned long pressDuration = millis() - pressStartTime;
- Serial.print("Press duration: ");
- Serial.println(pressDuration);
- // Exiting Mode 1
- if (mode1 && pressDuration >= veryLongPressThreshold) {
- mode1 = false;
- Serial.println("Exited Mode 1 (via long press)");
- }
- // Entering Mode 1
- else if (!mode1 && !mode2 && pressDuration >= longPressThreshold) {
- mode1 = true;
- Serial.println("Entered Mode 1");
- }
- // Normal click in Mode 1
- else if (mode1 && pressDuration < longPressThreshold) {
- option1 = (option1 + 1) % 5;
- Serial.print("Mode 1 Option: ");
- Serial.println(option1 + 1);
- }
- // Normal click in Mode 2
- else if (mode2 && pressDuration < longPressThreshold) {
- option2 = (option2 + 1) % 5;
- Serial.print("Mode 2 Option: ");
- Serial.println(option2 + 1);
- }
- lastReleaseTime = millis();
- }
- lastButtonState = reading;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement