忍者ブログ
[263]  [262]  [261]  [260]  [259]  [258]  [257]  [256]  [255]  [254]  [253
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

関数定義でロジックを分解し構造化でコーディング


import RPi.GPIO as GPIO
import time
LedPin = 11    # pin11 --- led
BtnPin = 12    # pin12 --- button

#セットアップ
def setup():
GPIO.setmode(GPIO.BOARD)       # ピンの番号を使用
GPIO.setup(LedPin, GPIO.OUT)   # 11を出力
GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    
            # 12を入力, プルアップで使用
   GPIO.output(LedPin, GPIO.HIGH)

#繰り返し
def loop():
while True:
if GPIO.input(BtnPin) == GPIO.LOW:  
print '...led on'
time.sleep(0.1)
GPIO.output(LedPin, GPIO.LOW)  # led on
else:
print 'led off...'
time.sleep(0.1)
GPIO.output(LedPin, GPIO.HIGH) # led off

#後始末
def destroy():
GPIO.output(LedPin, GPIO.HIGH)     # led off
   GPIO.cleanup()                     

#スタートロジック
if __name__ == '__main__':     # Program start 
setup()
try:
loop()
except KeyboardInterrupt:  # 'Ctrl+C' is pressed
destroy()

拍手

PR
カレンダー
03 2025/04 05
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
フリーエリア
最新CM
[03/10 DORA]
最新TB
プロフィール
HN:
dorabu
性別:
非公開
バーコード
ブログ内検索
P R
Copyright © ドラブーのアンドロイドとIoTなブログ All rights reserved. / Template by 四季. / Material by てんせん.

忍者ブログ [PR]