The attachInterrupt() interrupt command
FunduinoLast updated on
Den laufenden Programmablauf sofort auf einen Tastendruck reagieren lassen
You need this
12 x 12 mm pushbutton - colored button, 24 V / 50 mA, various colors — 12*12*6mm - White
0,25 € *Item no. F23106071About the product — 12 x 12 mm pushbutton - colored button, 24 V / 50 mA, various colors — 12*12*6mm - White
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino
2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V
1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W
1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W
All 6 parts together 19,70 € *Ready to ship immediately · Delivery time: 1-3 business days
* All prices include statutory VAT, plus shipping costs.
Material
What an interrupt is
Structure
The attachInterrupt() command
Sketch: Toggle LED using Interrupt
#define LED 7
#define TASTER 2
/*
Je nach Zustand der Variablen LEDStatus ist die LED ein- oder ausgeschaltet.
Beim Start des Programms ist sie ausgeschaltet.
volatile sorgt dafuer, dass Hauptprogramm und Interrupt-Routine
immer denselben aktuellen Wert sehen.
*/
volatile bool LEDStatus = LOW;
void setup()
{
pinMode(LED, OUTPUT);
// Mit INPUT_PULLUP liegt der Taster im Ruhezustand auf HIGH
// und wird beim Druecken auf LOW gezogen.
pinMode(TASTER, INPUT_PULLUP);
/*
FALLING = der Interrupt wird genau dann ausgeloest,
wenn der Pegel von HIGH nach LOW wechselt, also beim Druecken.
Mit CHANGE wuerde er auch beim Loslassen ausloesen und
der Zustand der LED waere danach wieder derselbe.
*/
attachInterrupt(digitalPinToInterrupt(TASTER), LEDSchalten, FALLING);
}
void loop()
{
// LED entsprechend dem gespeicherten Zustand schalten
digitalWrite(LED, LEDStatus);
}
// Interrupt-Service-Routine: wird beim Tastendruck aufgerufen
void LEDSchalten()
{
// LEDStatus umdrehen: aus true wird false, aus false wird true
LEDStatus = !LEDStatus;
}Rate the instructions