Last active
September 4, 2016 11:46
-
-
Save RyosukeKamei/1c7d517486730a06baaade40572300d6 to your computer and use it in GitHub Desktop.
Raspberry Pi 3でpythonを使いスイッチ制御でLEDを光らせる! ref: http://qiita.com/RyosukeKamei/items/5c19819a2153b0a997b6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo python3 switch_led.py 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git clone https://github.com/RyosukeKamei/raspberrypi3.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GPIOを制御するライブラリ | |
import wiringpi | |
# タイマーのライブラリ | |
import time | |
# 引数取得 | |
import sys | |
# GPIO定義 | |
led1_pin = 23 | |
led2_pin = 24 | |
switch_pin = 17 | |
# 光らせる間隔を引数から取得 | |
param = sys.argv | |
set_interval = int(param[1]) | |
# GPIO初期化 | |
wiringpi.wiringPiSetupGpio() | |
wiringpi.pinMode( led1_pin, 1 ) | |
wiringpi.pinMode( led2_pin, 1 ) | |
wiringpi.pinMode( switch_pin, 0 ) | |
# どっちのLEDがついているか | |
led = 0 | |
# この回路は永続的なので、止めるまで繰り返す | |
while True: | |
# LEDを消灯 | |
wiringpi.digitalWrite( led1_pin, 0 ) | |
wiringpi.digitalWrite( led2_pin, 0 ) | |
# スライドスイッチを検出 | |
while ( wiringpi.digitalRead(switch_pin) == 1 ): | |
# スイッチオン | |
print("スイッチオン") | |
if ( led == 0 ): | |
# LED1を光らせる | |
wiringpi.digitalWrite( led1_pin, 1 ) | |
wiringpi.digitalWrite( led2_pin, 0 ) | |
led = 1 | |
print("LED1") | |
else: | |
# LED2を光らせる | |
wiringpi.digitalWrite( led1_pin, 0 ) | |
wiringpi.digitalWrite( led2_pin, 1 ) | |
led = 0 | |
print("LED2") | |
# 引数で指定した秒数待機 | |
print(set_interval, "秒待機") | |
time.sleep(set_interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment