Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Interrupts in Arduino Programming
Introduction: Interrupts play a crucial role in Arduino programming as they allow the microcontroller to respond to external events in a timely and efficient manner. In this article, we will explore the importance and utility of interrupts and provide examples of codes and a list of components used in the examples.
Project: For our example project, we will create a simple digital input monitoring system using interrupts. The objective is to detect when a button is pressed and execute a specific action in response. This project can be extended to various applications such as home automation, robotics, and sensor-based systems.
List of Components:
Examples: Example 1: Interrupt-based Button Press Detection
// Pin assignments
const int buttonPin = 2;
const int ledPin = 13;
// Variables
volatile bool buttonPressed = false;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING);
}
void loop() {
if (buttonPressed) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
buttonPressed = false;
}
}
void buttonInterrupt() {
buttonPressed = true;
}
Explanation:
In this example, we use the attachInterrupt()
function to attach an interrupt to the button pin (pin 2). The interrupt is triggered on the falling edge of the button signal, indicating a button press. When the interrupt is triggered, the buttonInterrupt()
function is executed, setting the buttonPressed
flag to true. In the loop()
function, we check the flag and perform the desired action (in this case, blinking an LED) when the button is pressed.
Example 2: Debouncing Interrupts
// Pin assignments
const int buttonPin = 2;
const int ledPin = 13;
// Variables
volatile bool buttonPressed = false;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING);
}
void loop() {
if (buttonPressed) {
if ((millis() - lastDebounceTime) > debounceDelay) {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
buttonPressed = false;
}
}
}
void buttonInterrupt() {
if ((millis() - lastDebounceTime) > debounceDelay) {
buttonPressed = true;
lastDebounceTime = millis();
}
}
Explanation:
Debouncing is a common challenge when working with mechanical buttons. In this example, we introduce a debounce delay to ensure that only valid button presses are detected. The debounceDelay
variable defines the minimum time between two consecutive valid button presses. The lastDebounceTime
variable keeps track of the last debounce time. If a button press is detected within the debounce delay, it is considered valid and the desired action is performed.