Difference between revisions of "BakeBit - Button/zh"

From FriendlyELEC WiKi
Jump to: navigation, search
(硬件连接)
(运行结果)
 
(8 intermediate revisions by the same user not shown)
Line 27: Line 27:
 
== 示例程序:Button And Buzzer ==
 
== 示例程序:Button And Buzzer ==
  
本示例需要配合蜂鸣器使用,当按下按键时,蜂鸣器会响起,松开按键时,蜂鸣器停止。
+
本示例需要配合蜂鸣器使用。
  
 
=== 硬件连接 ===
 
=== 硬件连接 ===
Line 34: Line 34:
 
::{| class="wikitable"
 
::{| class="wikitable"
 
|-
 
|-
|[[File:BakeBit - Button01.jpg |frameless|150px]]    || '''BakeBit-Button'''<br />[[File:BakeBit - Button01.jpg |frameless|150px]]  
+
|[[File:Button And Buzzer-1.jpg |frameless|300px]]    || [[File:Button And Buzzer-2.jpg |frameless|300px]]  
 
|-
 
|-
 
|}
 
|}
Line 41: Line 41:
  
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
import bakebit
 
 
import time
 
import time
# Connect the BakeBit Ultrasonic Ranger to digital port D4
+
from bakebit import *
# SIG,NC,VCC,GND
+
import math
ultrasonic_ranger = 4
+
  
# Connect the BakeBit LED to digital port D3
+
buzzer_pin = 3 #Port for buzzer
led = 3                                                                     
+
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
  
bakebit.pinMode(led,"OUTPUT")
+
buzzer_on = False
light = 0
+
old_buzzer_on = not buzzer_on
 +
button_pressed = False
  
 
while True:
 
while True:
    try:
+
try:
        # Read distance value from Ultrasonic
+
while True:
distance = bakebit.ultrasonicRead(ultrasonic_ranger)
+
button_status = digitalRead(button) #Read the Button status
        print(distance)
+
# print button_status
if distance > 0:
+
if old_button_status < 0:  
if distance<10:
+
break
if light == 0:
+
 
print("\ton")
+
if button_status != old_button_status:
bakebit.digitalWrite(led,1)
+
break
light = 1
+
 
 +
time.sleep(0.2)
 +
old_button_status = button_status
 +
 
 +
if button_status == 0:
 +
button_pressed = True
 
else:
 
else:
if light == 1:
+
if button_pressed:
print("\toff")
+
buzzer_on = not buzzer_on
bakebit.digitalWrite(led,0)
+
button_pressed = False
light = 0
+
time.sleep(.2)
+
  
    except KeyboardInterrupt:
+
if old_buzzer_on != buzzer_on:
bakebit.digitalWrite(led,0)
+
old_buzzer_on = buzzer_on
        break
+
if buzzer_on:
 
+
analogWrite(buzzer_pin,127)
    except TypeError:
+
print "Buzzing"
        print ("Error")
+
else:
    except IOError:
+
analogWrite(buzzer_pin,0)
        print ("Error")
+
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.py Github]
  
 
=== 运行示例 ===
 
=== 运行示例 ===
Line 91: Line 104:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==示例程序==
+
=== 运行结果 ===
下面是一个简单的BakeBit - Button模块的例子,当没有按键按下时,输出为1,当有按键按下时,输出为0.
+
 
+
<syntaxhighlight lang="python">
+
import time
+
import bakebit
+
 
+
# Connect the BakeBit Button to digital port D3
+
# SIG,NC,VCC,GND
+
button = 3
+
 
+
bakebit.pinMode(button,"INPUT")
+
 
+
while True:
+
    try:
+
        print(bakebit.digitalRead(button))
+
        time.sleep(.5)
+
 
+
    except IOError:
+
        print ("Error")
+
 
+
</syntaxhighlight>
+
  
运行结果如下:
+
当点击按键时,蜂鸣器会响起,再次点击按键,蜂鸣器停止。
[[File:BakeBit - Button_nanopi_NEO.jpg|frameless|600px|BakeBit - Button_nanopi_NEO]]
+
  
 
==相关资料==
 
==相关资料==
 
*[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 Github项目](https://github.com/friendlyarm/BakeBit)
 +
*[BakeBit Starter Kit手册](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_cn.pdf)

Latest revision as of 07:46, 17 April 2017

English

1 介绍

按键
  • BakeBit - Button是一个瞬时(非自锁)按钮开关模块,用于检测按键事件,按钮被释放后自动恢复到常态。
  • 在按钮未被按下时模块输出高电平,按钮被按下后输出低电平。

2 特性

  • 使用标准的2.0mm 4 Pin BakeBit接口
  • 数字信号输出
  • PCB尺寸(mm):20x24

BakeBit - Button.PCB

  • 引脚说明:
名称 描述
GND
5V 电源5V
NC
SIG 信号

3 示例程序:Button And Buzzer

本示例需要配合蜂鸣器使用。

3.1 硬件连接

简单的将 蜂鸣器 模块插入 D3接口,将 按键 插入 D4 接口,如下面这样:

Button And Buzzer-1.jpg Button And Buzzer-2.jpg

3.2 示例源代码

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")

Github

3.3 运行示例

假设你已经参考bakebit教程安装了BakeBit源代码,
要运行示例程序,可以在开发板上进入 BakeBit/Software/Python目录,运行bakebit_prj_Button_And_Buzzer.py:

cd ~/BakeBit/Software/Python
sudo python bakebit_prj_Button_And_Buzzer.py

3.4 运行结果

当点击按键时,蜂鸣器会响起,再次点击按键,蜂鸣器停止。

4 相关资料