コレは何?

RPi.GPIO のインストール

API 仕様

サンプルプログラム

ランダムに点灯するLEDと同じ色のスイッチを押せ!

RasPi.gif
#!/usr/bin/env python3
# coding: utf-8

import RPi.GPIO as GPIO
import time
import random

LED_RED = 4
LED_YELLOW = 3
LED_BLUE = 2

SW_RED = 11
SW_YELLOW = 10
SW_BLUE = 9

def main() :

  # mode
  # GPIO.BOARD : pin number
  # GPIO.BCM   : gpio number
  GPIO.setmode(GPIO.BCM)

  GPIO.setup(LED_RED, GPIO.OUT)
  GPIO.setup(LED_YELLOW, GPIO.OUT)
  GPIO.setup(LED_BLUE, GPIO.OUT)

  GPIO.setup(SW_RED, GPIO.IN)
  GPIO.setup(SW_YELLOW, GPIO.IN)
  GPIO.setup(SW_BLUE, GPIO.IN)

  for _ in range(10) :
    led = random.randint(LED_BLUE, LED_RED)
    GPIO.output(led, True)

    sw = SW_RED if led == LED_RED else (SW_YELLOW if led == LED_YELLOW else SW_BLUE)
    GPIO.wait_for_edge(sw, GPIO.RISING, timeout=2000)

    if GPIO.input(sw) == False :
      GPIO.output(LED_RED, True)
      GPIO.output(LED_YELLOW, True)
      GPIO.output(LED_BLUE, True)
      time.sleep(1.0)

    GPIO.output(LED_RED, False)
    GPIO.output(LED_YELLOW, False)
    GPIO.output(LED_BLUE, False)
    time.sleep(1.0)

  GPIO.cleanup()

###
# If this script was called as shell command, __name__ is '__main__'.
if __name__ == '__main__':
    main()

サービス化


Raspberry Pi


添付ファイル: filebounce.gif 1952件 [詳細] fileRasPi.gif 2458件 [詳細] filecircuit.png 2143件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2016-05-06 (金) 18:27:41 (2905d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=6487cf4d47
ISBN10
ISBN13
9784061426061