@@ -67,6 +67,42 @@ const unsigned long DEBOUNCE_DELAY_MS = 50; // 50ms debounce delay
67
67
// State-based debouncing to prevent hysteresis issues
68
68
volatile bool lastButtonState = HIGH; // Track last stable state (HIGH = released)
69
69
70
+ // Global lambda function (declared at file scope) - ISR in IRAM
71
+ IRAM_ATTR std::function<void ()> changeModeLambda = []() {
72
+ // Simple debouncing: check if enough time has passed since last interrupt
73
+ unsigned long currentTime = millis ();
74
+ if (currentTime - lastButtonInterruptTime < DEBOUNCE_DELAY_MS) {
75
+ return ; // Ignore this interrupt due to bouncing
76
+ }
77
+
78
+ // Read current pin state to determine edge type
79
+ bool currentState = digitalRead (BUTTON_PIN);
80
+
81
+ // State-based debouncing: only process if state actually changed
82
+ if (currentState == lastButtonState) {
83
+ return ; // No real state change, ignore (hysteresis/noise)
84
+ }
85
+
86
+ // Update timing and state
87
+ lastButtonInterruptTime = currentTime;
88
+ lastButtonState = currentState;
89
+
90
+ if (currentState == LOW) {
91
+ // FALLING edge detected (button pressed) - set flag for main loop
92
+ // volatile variables require use of temporary value transfer
93
+ uint32_t temp = buttonPressCount + 1 ;
94
+ buttonPressCount = temp;
95
+ buttonPressed = true ;
96
+ ledStateChanged = true ; // Signal main loop to toggle LED
97
+ } else {
98
+ // RISING edge detected (button released) - set flag for main loop
99
+ // volatile variables require use of temporary value transfer
100
+ uint32_t temp = buttonReleaseCount + 1 ;
101
+ buttonReleaseCount = temp;
102
+ buttonReleased = true ;
103
+ }
104
+ };
105
+
70
106
void setup () {
71
107
Serial.begin (115200 );
72
108
delay (1000 ); // Allow serial monitor to connect
@@ -83,38 +119,7 @@ void setup() {
83
119
// This toggles the LED on button press (FALLING edge)
84
120
Serial.println (" Setting up CHANGE mode lambda for LED toggle" );
85
121
86
- // Simplified lambda with minimal captures
87
- std::function<void ()> changeModeLambda = []() {
88
- // Simple debouncing: check if enough time has passed since last interrupt
89
- unsigned long currentTime = millis ();
90
- if (currentTime - lastButtonInterruptTime < DEBOUNCE_DELAY_MS) {
91
- return ; // Ignore this interrupt due to bouncing
92
- }
93
-
94
- // Read current pin state to determine edge type
95
- bool currentState = digitalRead (BUTTON_PIN);
96
-
97
- // State-based debouncing: only process if state actually changed
98
- if (currentState == lastButtonState) {
99
- return ; // No real state change, ignore (hysteresis/noise)
100
- }
101
-
102
- // Update timing and state
103
- lastButtonInterruptTime = currentTime;
104
- lastButtonState = currentState;
105
-
106
- if (currentState == LOW) {
107
- // FALLING edge detected (button pressed) - set flag for main loop
108
- buttonPressCount++;
109
- buttonPressed = true ;
110
- ledStateChanged = true ; // Signal main loop to toggle LED
111
- } else {
112
- // RISING edge detected (button released) - set flag for main loop
113
- buttonReleaseCount++;
114
- buttonReleased = true ;
115
- }
116
- };
117
-
122
+ // Use the global lambda function
118
123
attachInterrupt (BUTTON_PIN, changeModeLambda, CHANGE);
119
124
120
125
Serial.println ();
0 commit comments