树莓派Python编程指南--第一章(起航--启动和运行)

原创
2017/05/07 12:35
阅读数 220

 

1.1 组装好你的树莓派

组装工作之后再讲述

控制二极管发光及关闭

组装完成后,我们开始第一个例子:控制二极管发光及关闭的小程序

 

一下内容保存在diode_on_off.py文件中

import RPi.GPIO ad GPIO

#不再提示警告
GPIO.setwarnings(False)
#设置GPIO模式为物理引脚
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

#设置两个物理引脚 19 和 21 一个高电平 一个低电平 输入电压让二极管发光
gpio_left = 19
gpio_right = 21

GPIO.setup(gpio_left, GPIO.OUT)
GPIO.setup(gpio_right, GPIO.OUT)

def on():
    GPIO.output(gpio.left, GPIO.HIGH)
    GPIO.output(gpio.right, GPIO.LOW)

def off():
    GPIO.output(gpio.left, GPIO.LOW)
    GPIO.output(gpio.left, GPIO.LOW)

while True:
    print("1 to on")
    print("0 to off")
    print("q to exit")

    user_choice = input("Choice:")

    if user_choice == "1":
        on()
    elif user_choice == "0":
        off()
    elif (user_choice == "q") or (user_choice == "Q") :
        GPIO.cleanup()
        break
    else:
        print("Enter Error")

    


pi@xiaoxiao:~/workspace $ python3 Test/diode_on_off.py
1 to on
0 to off
q to exit
Choice:1
1 to on
0 to off
q to exit
Choice:2
Enter Error
1 to on
0 to off
q to exit
Choice:q
pi@xiaoxiao:~/workspace $

第二个例子:打开相机及拍照

 

 

1.2 可能遇到的问题

 

1.3 树莓派快速指南

 

1.4Python3

 

1.5 小结

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部