This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
oak:tutorials:button [2016/03/19 08:56] djflix created |
oak:tutorials:button [2016/03/20 05:13] (current) djflix |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | |||
| ======Oak: Breadboard Button Lesson====== | ======Oak: Breadboard Button Lesson====== | ||
| - | [Fritzing or photo of final main circuit for this lesson] | + | {{:oak:tutorials:screen_shot_2016-03-20_at_12.34.41.png?direct&200|}} |
| - | Fritzing parts here: https://github.com/digistump/OakCore/issues/38 | + | |
| This lesson will show you how to connect a tactile switch (button) to your Oak and use it to control a sketch. In the examples we will turn a LED on and off by pushing the button. | This lesson will show you how to connect a tactile switch (button) to your Oak and use it to control a sketch. In the examples we will turn a LED on and off by pushing the button. | ||
| Line 10: | Line 8: | ||
| ^ Part ^ Quantity ^Identification^ | ^ Part ^ Quantity ^Identification^ | ||
| - | | Oak with soldered headers |1| | | + | | Oak with soldered *female* headers |1| | |
| | Breadboard| 1| | | | Breadboard| 1| | | ||
| | 5mm RGB Common Cathode LED| 1| | | | 5mm RGB Common Cathode LED| 1| | | ||
| - | | 330R Resistor| 1| Orange-Orange-Red| | + | | 150R Resistor| 1| Brown-Green-Brown| |
| | 10K Resistor| 1| Brown-Black-Orange| | | 10K Resistor| 1| Brown-Black-Orange| | ||
| | 12mm Tactile Button| 1| | | | 12mm Tactile Button| 1| | | ||
| Line 22: | Line 20: | ||
| **Pull-Down Resistor**: | **Pull-Down Resistor**: | ||
| - | In this lesson we will use Pull-Up resistors to make sure we can reliably detect button presses. If an I/O pin is 'left floating' reading it's value can result in either HIGH or LOW. By attaching a resistor between the I/O pin of the Oak and GND we can ensure that if the button is not pressed, the state is always LOW. | + | In this lesson we will use a Pull-Down resistor to make sure we can reliably detect button presses. If an I/O pin is 'left floating' reading it's value can result in either HIGH or LOW. By attaching a resistor between the I/O pin of the Oak and GND we can ensure that if the button is not pressed, the state is always LOW. |
| - | [Fritzing diagram of pull-down vs floating] | + | A pulldown configuration is shown on the left, with a floating circuit on the right (digital pin connection shown in blue, with ''Vcc'' and ground in red/black, respectively): |
| + | |||
| + | [[http://digistump.com/wiki/_media/oak/tutorials/button_pulldown.png|{{oak:tutorials:button_pulldown.png?400 }}]] | ||
| + | |||
| + | [[http://digistump.com/wiki/_media/oak/tutorials/button_floating.png|{{ oak:tutorials:button_floating.png?400 }}]] | ||
| **Interrupt**: | **Interrupt**: | ||
| Line 34: | 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:===== | ||
| + | {{:oak:tutorials:screen_shot_2016-03-20_at_12.34.41.png?direct&400|}} | ||
| + | |||
| + | **1. Resistors** | ||
| + | To build up the circuit for this lesson, start with the resistors. As you can see, the resistors share a common pin. The 10K resistor (Brown-Black-Orange) is the pull-down resistor and the 150R resistor (Brown-Green-Brown) is used to limit the current sent to the LED. Connect the black wire as shown in the image to provide ground to the resistors. | ||
| + | |||
| + | **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 (150 Ohm) resistor, and connect the **anode** to P2 of the Oak using a wire (the blue wire in the example). | ||
| + | |||
| + | **3. Button** | ||
| + | As with the resistors, the button has no specific orientation, but it will connect its top two legs with the bottom two legs. In this example we connect VCC (3.3v) to the bottom-left leg of the button using a red wire. The top-left leg is connected to the pull-down resistor. | ||
| + | |||
| + | **4. The final wire** | ||
| + | To read out the button state, a connection between P6 and the top-left pin of the button is required (shown in green in the example image). | ||
| - | [Fritzing diagram of hooking the Oak to the Button with resistor, the LED and the breadboard] | + | That's it! You should now be able to test the circuit by uploading code to your Oak! |
| - | Steps to make these connections, things to watch out for (polarity, resistor codes etc) | + | |
| =====Code (simple mode):===== | =====Code (simple mode):===== | ||
| - | 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 9 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 9 | + | #define LED_PIN 2 |
| </code> | </code> | ||
| Line 60: | 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 { | ||
| Line 83: | Line 114: | ||
| Then, replace line where you set the pin mode for BUTTON_PIN so that it looks like this: | Then, replace line where you set the pin mode for BUTTON_PIN so that it looks like this: | ||
| + | <code> | ||
| void setup() { | void setup() { | ||
| pinMode(LED_PIN, OUTPUT); | pinMode(LED_PIN, OUTPUT); | ||