美文网首页
30个Python物联网小实验:使用按钮开灯关灯1

30个Python物联网小实验:使用按钮开灯关灯1

作者: A遇上方知友 | 来源:发表于2019-05-21 14:30 被阅读0次
image

使用按钮开灯关灯

  • 接线图非常简单,LED接 GPIO17 号口,按钮接 GPIO2 号口,负极接GND地线。
image
  • 代码也非常简单:

<pre class="prettyprint hljs livescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(2)

button.when_pressed = led.on
button.when_released = led.off

pause()</pre>

  • 执行代码,按下按钮,发光二极管就会亮,松开就会灭。

学习Python中的小伙伴,需要学习资料的话,可以到我的微信公众号:Python学习知识圈,后台回复:“01”,即可拿Python学习资料

使用按钮开灯关灯(二)

  • 还有一种 source 的写法,直接把按钮的状态提供给发光二极管,达到同样的效果。

<pre class="prettyprint hljs gradle" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(2)

led.source = button

pause()</pre>

按钮按下和松开

  • 先上效果
image

<pre class="hljs nginx" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">GPIO2
</pre>

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import Button

button = Button(2)

while True:
if button.is_pressed:
print("Button is pressed")
else:
print("Button is not pressed")</pre>

等待按钮按下才执行

  • 直接上代码吧:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import Button

button = Button(2)

button.wait_for_press()
print("Button was pressed")</pre>

  • 程序执行到 button.wait_for_press() 这一句之后,会先停下,等待按钮按下之后,才会继续执行。

按下执行特定函数

  • 直接上代码:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import Button
from signal import pause

def say_hello():
print("Hello!")

button = Button(2)

button.when_pressed = say_hello

pause()</pre>

  • 按下按钮,执行 say_hello() 这个函数。

松开执行特定函数

  • 上效果图:
image
  • 上代码:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import Button
from signal import pause

def say_hello():
print("Hello!")

def say_goodbye():
print("Goodbye!")

button = Button(2)

button.when_pressed = say_hello
button.when_released = say_goodbye

pause()</pre>

  • 按下会执行 say_hello() 函数,松开会执行 say_goodbye() 函数。

长按4秒关机

  • 上代码:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import Button
from subprocess import check_call
from signal import pause

def shutdown():
check_call(['sudo', 'poweroff'])

shutdown_btn = Button(2, hold_time=4)
shutdown_btn.when_held = shutdown

pause()</pre>

  • 增加了一个 hold_time=4 长按4秒才触发的初始化,触发之后执行 shutdown() 函数关机。

小游戏:谁的反应快?

  • 接线图:两个按钮分别连接 GPIO2、3 号口,LED连接 GPIO 17 号口。
image
  • 上代码:

<pre class="prettyprint hljs livescript" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from gpiozero import Button, LED
from time import sleep
import random

led = LED(17)

player_1 = Button(2)
player_2 = Button(3)

time = random.uniform(2, 5)
sleep(time)
led.on()

while True:
if player_1.is_pressed:
print("Player 1 wins!")
break
if player_2.is_pressed:
print("Player 2 wins!")
break

led.off()</pre>

  • 程序开始执行后,在(2,5)秒随机点亮LED灯,谁抢险按下按钮,就会显示谁赢了。

相关文章

  • 30个Python物联网小实验:使用按钮开灯关灯1

    使用按钮开灯关灯 接线图非常简单,LED接 GPIO17 号口,按钮接 GPIO2 号口,负极接GND地线。 代码...

  • 30个Python物联网小实验3:使用按钮开灯关灯

    使用按钮开灯关灯 接线图非常简单,LED接 GPIO17 号口,按钮接 GPIO2 号口,负极接GND地线。 代码...

  • 开灯,关灯......

    我和先生大学同班同学,我和他在一起的时间,比我们和各自父母在一起的时间都长。我们都已进入不惑之年,到了这个年龄,最...

  • 开灯关灯

    眼看着下班时间快到了,小尤急匆匆地把手上的工作做完,终于赶在下班时间之前把一切收拾整齐,踏出公司大门的那一刻她才想...

  • 开灯关灯

    早上,老公热完饭,到外面把车启动开,等着送朋友去邻县。 “哒哒…哒哒…”,老爷子屋里又不断传来开关灯的声音。...

  • 〖开灯与关灯〗

    〖开灯与关灯〗 ——〈开灯心语〉 开灯 ——光明, 当下最好 ——自然更好; 关灯 ——无明, 当下糟糕...

  • 弟弟语录2则

    妈妈:我们关灯睡觉了。 弟弟:不要关灯,要开灯。 妈妈:关灯了。 弟弟:开灯啦。 姐姐:关掉! 弟弟:开掉! 姐姐...

  • (家)‖1、2、3,关灯/开灯。

    “1、2、3,关灯了/开灯了”——罗胖边说边闭好眼睛。 “好”——我闭着眼睛应到。 再睁开眼睛的时候,身边环境和那...

  • 关灯开灯之间(哲理诗)

    关灯开灯之间(哲理诗) 文/黄影 开灯眼前一亮,关灯心里一亮 开灯,看到一个人间 关灯,回到我...

  • 商学院筑梦 灯光需求

    1、演员上场(剪影效果)不要开灯 2、演员聚拢,开灯 3、屏幕黑起 关灯 4、全体演员上场开灯,快闪灯

网友评论

      本文标题:30个Python物联网小实验:使用按钮开灯关灯1

      本文链接:https://www.haomeiwen.com/subject/mbcjzqtx.html