This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
|
oak:tutorials:button [2016/03/20 04:56] djflix Make define lower case |
oak:tutorials:button [2016/03/20 05:12] djflix |
||
|---|---|---|---|
| Line 36: | Line 36: | ||
| * HIGH (pin is high) | * HIGH (pin is high) | ||
| - | It's important to note that an Interrupt actually interrupts what you're doing! So keep your interrupt handlers as simple as possible. Otherwise they might interfere with other time-sensitive operations. | + | In interrupt will interrupt anything the microcontroller is doing (even if it is in the middle of a function!) but will return after executing the interrupt handler. See some example of a good and a bad handleButtonPress() interrupt handler: |
| + | |||
| + | <code> | ||
| + | bool event_has_happened = false; | ||
| + | |||
| + | void good_handleButtonPress() { | ||
| + | //set a bool to true so we can handle it in loop() | ||
| + | event_has_happened = true; | ||
| + | } | ||
| + | |||
| + | void bad_handleButtonPress() { | ||
| + | // wait user to release button | ||
| + | delay(500); | ||
| + | //Handle the button press now so we won't forget it happened! | ||
| + | call_another_slow_function(); | ||
| + | } | ||
| + | </code> | ||
| =====Circuit:===== | =====Circuit:===== | ||
| Line 74: | Line 91: | ||
| <code> | <code> | ||
| void loop() { | void loop() { | ||
| - | if(digitalRead(BUTTON_PIN) == LOW) { | + | if(digitalRead(BUTTON_PIN) == HIGH) { |
| digitalWrite(LED_PIN, HIGH); | digitalWrite(LED_PIN, HIGH); | ||
| } else { | } else { | ||