This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
oak:tutorials:button [2016/03/20 04:53] djflix Added Fritzing image for connecting the button |
oak:tutorials:button [2016/03/20 05:13] (current) 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 45: | Line 62: | ||
| **2. LED** | **2. LED** | ||
| - | After adding the resistors, you can add the LED. When connecting the LED, double-check the polarity. The shorter leg is the cathode (-) and the longer leg is the anode (+). Connect the **cathode** to the top (10K) resistor, and connect the **anode** to P2 of the Oak using a wire (the blue wire in the example). | + | After adding the resistors, you can add the LED. When connecting the LED, double-check the polarity. The shorter leg is the cathode (-) and the longer leg is the anode (+). Connect the **cathode** to the top (150 Ohm) resistor, and connect the **anode** to P2 of the Oak using a wire (the blue wire in the example). |
| **3. Button** | **3. Button** | ||
| Line 58: | Line 75: | ||
| The simple mode allows you to check for the current button state and react accordingly. This example assumes Pin 6 for the button and Pin 2 for the LED. You can define these at the top section of your sketch like this: | The simple mode allows you to check for the current button state and react accordingly. This example assumes Pin 6 for the button and Pin 2 for the LED. You can define these at the top section of your sketch like this: | ||
| <code> | <code> | ||
| - | #DEFINE BUTTON_PIN 6 | + | #define BUTTON_PIN 6 |
| - | #DEFINE LED_PIN 2 | + | #define LED_PIN 2 |
| </code> | </code> | ||
| 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 { | ||