Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add interrupt example to basics #14

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/01.Basics/Interrupt/Interrupt.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Interrupt
Use a pushbutton to control an LED; powered by interrupts.

This sample uses an interrupt service routine (ISR) named `blink` to toggle
a global state variable and then uses the built-in LED to display the value
of the cached state to the user. The work of updating the cached state happens
asynchronously in the interrupt service routine.

This sample can easily be modified from behaving as a button into behaving as
a switch, by changing the interrupt mode from CHANGE to LOW. Furthermore, this
example can be modified to update the built-in LED from inside in the ISR,
which would allow the loop function to be empty yet still allow actions and
reactions to occur.

This example code is in the public domain (adapted from
https://www.arduino.cc/en/Reference/attachInterrupt).

adapted 1 APR 2017
by Zachary J. Fields
*/

const byte interruptPin = 2;
volatile byte state = LOW;

void blink() {
state = !state; // toggle the cached state
}

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
// initialize digital pin as input with pull-up resistor (i.e. always HIGH, unless deliberately pulled LOW)
pinMode(interruptPin, INPUT_PULLUP);
// attach an interrupt service routine to be executed when pin state changes
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

// the loop function runs over and over again forever
void loop() {
// update the LED to reflect the cached state updated with interrupts
digitalWrite(LED_BUILTIN, state);
}

1 change: 1 addition & 0 deletions examples/01.Basics/Interrupt/Interrupt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use a pushbutton to control an LED; powered by interrupts.
Binary file added examples/01.Basics/Interrupt/layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/01.Basics/Interrupt/schematic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.