Difference between revisions of "BakeBit - Button"
From FriendlyELEC WiKi
(→Source Code) |
|||
(13 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
==Introduction== | ==Introduction== | ||
[[File:BakeBit - Button01.jpg|thumb|Button]] | [[File:BakeBit - Button01.jpg|thumb|Button]] | ||
− | *BakeBit - | + | * The BakeBit - Button is an instant button module and it is used to detect button events. When the button is released the module goes back to its default state. |
− | * | + | * If the button is not pressed the module will output high otherwise it will output low. |
− | == | + | ==Hardware Spec== |
− | * | + | * Standard 2.0mm pitch 4-Pin BakeBit interface |
− | * | + | * Digital output |
− | * | + | * PCB dimension(mm): 20 x 24 |
[[File:BakeBit - Button002.png | frameless|300px|BakeBit - Button.PCB]] | [[File:BakeBit - Button002.png | frameless|300px|BakeBit - Button.PCB]] | ||
− | * | + | * Pin Description: |
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
− | | | + | |Pin || Description |
|- | |- | ||
− | |GND || | + | |GND || Ground |
|- | |- | ||
− | |5V || | + | |5V || Supply Voltage 5V |
|- | |- | ||
− | |NC || | + | |NC || Not Connected |
|- | |- | ||
− | |SIG || | + | |SIG || Signal |
|} | |} | ||
− | |||
− | + | == Code Sample: Button And Buzzer == | |
− | === | + | A BakeBit - Buzzer module is needed in this test case. |
− | + | ||
+ | === Hardware Setup === | ||
+ | Connect the buzzer module to the NanoHat Hub's D3 and the button module to the NanoHat Hub's D4: | ||
::{| class="wikitable" | ::{| class="wikitable" | ||
Line 39: | Line 40: | ||
|} | |} | ||
− | === | + | === Source Code === |
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
− | |||
import time | import time | ||
− | + | from bakebit import * | |
− | + | import math | |
− | + | ||
− | # | + | buzzer_pin = 3 #Port for buzzer |
− | + | button = 4 #Port for Button | |
+ | old_button_status = -1 | ||
+ | pinMode(buzzer_pin,"OUTPUT") # Assign mode for buzzer as output | ||
+ | pinMode(button,"INPUT") # Assign mode for Button as input | ||
− | + | buzzer_on = False | |
− | + | old_buzzer_on = not buzzer_on | |
+ | button_pressed = False | ||
while True: | while True: | ||
− | + | try: | |
− | + | while True: | |
− | + | button_status = digitalRead(button) #Read the Button status | |
− | + | # print button_status | |
− | + | if old_button_status < 0: | |
− | + | break | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | if | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | if button_status != old_button_status: | |
− | + | break | |
− | + | ||
− | + | time.sleep(0.2) | |
− | + | old_button_status = button_status | |
− | + | ||
− | + | if button_status == 0: | |
+ | button_pressed = True | ||
+ | else: | ||
+ | if button_pressed: | ||
+ | buzzer_on = not buzzer_on | ||
+ | button_pressed = False | ||
+ | |||
+ | if old_buzzer_on != buzzer_on: | ||
+ | old_buzzer_on = buzzer_on | ||
+ | if buzzer_on: | ||
+ | analogWrite(buzzer_pin,127) | ||
+ | print "Buzzing" | ||
+ | else: | ||
+ | analogWrite(buzzer_pin,0) | ||
+ | print "\tOff" | ||
+ | |||
+ | time.sleep(0.2) | ||
+ | except KeyboardInterrupt: # Stop the buzzer before stopping | ||
+ | digitalWrite(buzzer_pin,0) | ||
+ | break | ||
+ | except (IOError,TypeError) as e: | ||
+ | print("Error") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | [https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_prj_Button_And_Buzzer | + | [https://github.com/friendlyarm/BakeBit/blob/master/Software/Python/bakebit_prj_Button_And_Buzzer.py Github] |
− | === | + | === Run Code Sample === |
− | + | Before you run the code sample you need to follow the steps in [http://wiki.friendlyarm.com/bakebit bakebit tutorial] to install the BakeBit package.<br /> | |
− | + | Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_Button_And_Buzzer.py" program: | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
cd ~/BakeBit/Software/Python | cd ~/BakeBit/Software/Python | ||
Line 93: | Line 106: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | === | + | === Observation === |
− | + | When the button is clicked the buzzer will beep and if it is clicked again the buzzer will be silent. | |
− | == | + | ==Resources== |
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/8/8a/05-SCHEMATIC_Button.pdf BakeBit - Button.pdf]) | *[Schematic]([http://wiki.friendlyarm.com/wiki/images/8/8a/05-SCHEMATIC_Button.pdf BakeBit - Button.pdf]) | ||
− | *[BakeBit | + | *[BakeBit Github Project Page](https://github.com/friendlyarm/BakeBit) |
− | *[BakeBit Starter | + | *[BakeBit Starter Kit User's Manual](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_en.pdf) |
+ | |||
+ | ==Update Log== | ||
+ | ===December-12-2016=== | ||
+ | * Released English version | ||
+ | |||
+ | ===Jan-19-2017=== | ||
+ | * Renamed "NEO-Hub" to "NanoHat-Hub" | ||
+ | |||
+ | ===Jan-20-2017=== | ||
+ | * Renamed "NanoHat-Hub" to "NanoHat Hub" |
Latest revision as of 07:46, 17 April 2017
Contents
1 Introduction
- The BakeBit - Button is an instant button module and it is used to detect button events. When the button is released the module goes back to its default state.
- If the button is not pressed the module will output high otherwise it will output low.
2 Hardware Spec
- Standard 2.0mm pitch 4-Pin BakeBit interface
- Digital output
- PCB dimension(mm): 20 x 24
- Pin Description:
Pin | Description |
GND | Ground |
5V | Supply Voltage 5V |
NC | Not Connected |
SIG | Signal |
3 Code Sample: Button And Buzzer
A BakeBit - Buzzer module is needed in this test case.
3.1 Hardware Setup
Connect the buzzer module to the NanoHat Hub's D3 and the button module to the NanoHat Hub's D4:
3.2 Source Code
import time from bakebit import * import math buzzer_pin = 3 #Port for buzzer button = 4 #Port for Button old_button_status = -1 pinMode(buzzer_pin,"OUTPUT") # Assign mode for buzzer as output pinMode(button,"INPUT") # Assign mode for Button as input buzzer_on = False old_buzzer_on = not buzzer_on button_pressed = False while True: try: while True: button_status = digitalRead(button) #Read the Button status # print button_status if old_button_status < 0: break if button_status != old_button_status: break time.sleep(0.2) old_button_status = button_status if button_status == 0: button_pressed = True else: if button_pressed: buzzer_on = not buzzer_on button_pressed = False if old_buzzer_on != buzzer_on: old_buzzer_on = buzzer_on if buzzer_on: analogWrite(buzzer_pin,127) print "Buzzing" else: analogWrite(buzzer_pin,0) print "\tOff" time.sleep(0.2) except KeyboardInterrupt: # Stop the buzzer before stopping digitalWrite(buzzer_pin,0) break except (IOError,TypeError) as e: print("Error")
3.3 Run Code Sample
Before you run the code sample you need to follow the steps in bakebit tutorial to install the BakeBit package.
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_Button_And_Buzzer.py" program:
cd ~/BakeBit/Software/Python sudo python bakebit_prj_Button_And_Buzzer.py
3.4 Observation
When the button is clicked the buzzer will beep and if it is clicked again the buzzer will be silent.
4 Resources
- [Schematic](BakeBit - Button.pdf)
- [BakeBit Github Project Page](https://github.com/friendlyarm/BakeBit)
- [BakeBit Starter Kit User's Manual](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_en.pdf)
5 Update Log
5.1 December-12-2016
- Released English version
5.2 Jan-19-2017
- Renamed "NEO-Hub" to "NanoHat-Hub"
5.3 Jan-20-2017
- Renamed "NanoHat-Hub" to "NanoHat Hub"