-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab01_Interrupt.cpp
More file actions
31 lines (26 loc) · 1.1 KB
/
Lab01_Interrupt.cpp
File metadata and controls
31 lines (26 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const int buttonPin = 2; // Pin D2 is the interrupt pin
const int ledPin = LED_BUILTIN; // Built-in LED pin (pin 13)
volatile bool ledState = LOW; // LED state, volatile because it's used in ISR
unsigned long lastDebounceTime = 0; // Timestamp of the last debounce check
const unsigned long debounceDelay = 100; // Debounce time in milliseconds
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set D2 as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
attachInterrupt(digitalPinToInterrupt(buttonPin), toggleLED, FALLING);
// Attach interrupt on falling edge (button press)
}
void loop() {
// Main loop can perform other tasks
// The LED state is handled in the interrupt service routine (ISR)
}
void toggleLED() {
// Debounce logic: Ignore interrupts that happen too quickly
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTime) > debounceDelay) {
// Toggle the LED state
ledState = !ledState;
digitalWrite(ledPin, ledState);
// Update the last debounce time
lastDebounceTime = currentTime;
}
}