01.LED左右移動

#-*- coding:big5 -*-

#!/usr/bin/python

# ½d¨Ò: LED ¶]°¨¿O

# ±µ¸}»¡©ú: GPIO 3-24 ±µ¦Ü16 LED¼Ò²Õ´ú¸Õ

#

import RPi.GPIO as GPIO

import time #使用time.sleep

gpio_list = [3,5,7,8,10,11,12,13,15,16,18,19,21,22,23,24]

# Use physical pin numbers

GPIO.setmode(GPIO.BOARD)

for i in range(16):

GPIO.setup(gpio_list[i], GPIO.OUT) #把16支腳設定為GPIO.OUT)

for i in range(16):

GPIO.output(gpio_list[i], GPIO.LOW) #把16支腳設定為低電位(不會亮)

print ("01.16LED DEMO Starting... ")

while True:

for i in range(16): #由左到右

for j in range(16):

if i == j :

GPIO.output(gpio_list[j], True) #將 i 的LED 設定為ON

else:

GPIO.output(gpio_list[j], False) #其餘 LED 設定為OFF

time.sleep(0.1)

for i in range(16): #由右到左

for j in range(16):

if i == j :

GPIO.output(gpio_list[15-j], True)

else:

GPIO.output(gpio_list[15-j], False)

time.sleep(0.1)

GPIO.cleanup()

作業:

LED 左到右 逐漸全亮

LED 右到左 逐漸全暗