7
0

More than 1 year has passed since last update.

Raspberry Pi Zeroでイルミネーションする

Last updated at Posted at 2021-12-02

この記事について

クリスマスなので、Raspberry Pi Zeroでイルミネーションしてみました:christmas_tree:

iOS の画像 (1).jpg

コード

import RPi.GPIO as GPIO
import time

Led_red_pin = 17
Led_green_pin = 27
Led_blue_pin = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(Led_red_pin, GPIO.OUT)
GPIO.setup(Led_green_pin, GPIO.OUT)
GPIO.setup(Led_blue_pin, GPIO.OUT)

def main():
  try:
    while True:
      blinking_led(Led_red_pin, 3)
      blinking_led(Led_green_pin, 3)
      blinking_led(Led_blue_pin, 3)
      blinking_all_led(3)

  except KeyboardInterrupt:
    GPIO.cleanup()

def blinking_led(pin, count):
  for i in range(count):
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(0.1)
    GPIO.output(pin, GPIO.LOW)
    time.sleep(0.1)

def blinking_all_led(count):
  for i in range(count):
    GPIO.output(Led_red_pin, GPIO.HIGH)
    GPIO.output(Led_green_pin, GPIO.HIGH)
    GPIO.output(Led_blue_pin, GPIO.HIGH)
    time.sleep(0.1)
    GPIO.output(Led_red_pin, GPIO.LOW)
    GPIO.output(Led_green_pin, GPIO.LOW)
    GPIO.output(Led_blue_pin, GPIO.LOW)
    time.sleep(0.1)

if __name__ == '__main__':
    main()

詳しい説明

  • GPIOを指定するおまじないです
    今回は使用するGPIOを変数で指定しています
Led_red_pin = 17
Led_green_pin = 27
Led_blue_pin = 4

GPIO.setmode(GPIO.BCM)
GPIO.setup(Led_red_pin, GPIO.OUT)
GPIO.setup(Led_green_pin, GPIO.OUT)
GPIO.setup(Led_blue_pin, GPIO.OUT)
  • main関数で点灯させる関数をループさせます
    Ctl + cでループを停止し、GPIOの設定をクリアします
def main():
  try:
    while True:
      blinking_led(Led_red_pin, 3)
      blinking_led(Led_green_pin, 3)
      blinking_led(Led_blue_pin, 3)
      blinking_all_led(3)

  except KeyboardInterrupt:
    GPIO.cleanup()
  • 指定したGPIOを点滅させる関数
def blinking_led(pin, count):
  for i in range(count):
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(0.1)
    GPIO.output(pin, GPIO.LOW)
    time.sleep(0.1)
  • すべてのGPIOを点滅させる関数
def blinking_all_led(count):
  for i in range(count):
    GPIO.output(Led_red_pin, GPIO.HIGH)
    GPIO.output(Led_green_pin, GPIO.HIGH)
    GPIO.output(Led_blue_pin, GPIO.HIGH)
    time.sleep(0.1)
    GPIO.output(Led_red_pin, GPIO.LOW)
    GPIO.output(Led_green_pin, GPIO.LOW)
    GPIO.output(Led_blue_pin, GPIO.LOW)
    time.sleep(0.1)
  • Pythonでmain関数を呼び出すおまじない
if __name__ == '__main__':
    main()

動かしてみる

IMAGE ALT TEXT HERE

7
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
0