-
-
Notifications
You must be signed in to change notification settings - Fork 50
Add interrupt example to basics #14
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
+46
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
examples/01.Basics/Interrupt/Interrupt.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
Binary file added
examples/01.Basics/Interrupt/schematic.png
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.