Python Code 27.2.1 LightWater03
First observe the project result, and then analyze the code.
1. Use cd command to enter 27.2.1_LightWater03 directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/27.2.1_LightWater03
2. Use python command to execute python code “LightWater03.py”.
python LightWater03.py
After the program is executed, the LEDs will light up in the form of flowing water and carry a long tail.
The following is the program code:
im port RPi. GPIO as GPIO
im port time
LSBFIRST = 1
MSBFIRST = 2
#define the pins connect to 74HC595
dataPin = 11 #DS Pin of 74HC595(Pin14)
latchPin = 13 #ST_CP Pin of 74HC595(Pin12)
clockPin = 15 #SH_CP Pin of 74HC595(Pin11)
#Define an array to save the pulse width of LED. Output the signal to the 8 adjacent LEDs
in order.
pluseWidth = [ 0, 0,0,0,0,0,0,0,64,32,16,8,4,2,1,0,0,0,0,0,0,0,0]
def setup():
GPIO. setmode(GPIO.BOARD) # Number GPIOs by its physical location
GPIO. setup(dataPin, GPIO. OUT)
GPIO. setup(latchPin, GPIO. OUT)
GPIO. setup(clockPin, GPIO. OUT)
def shiftOut(dPin,cPin,order,val):
for i in range(0,8):
GPIO.output(cPin,GPIO.LOW);
if(order == LSBFIRST):
GPIO.output(dPin,(0x01&(val>>i)==0x01) and GPIO.HIGH or GPIO.LOW)
elif(order == MSBFIRST):
GPIO.output(dPin,(0x80&(val<<i)==0x80) and GPIO.HIGH or GPIO.LOW)
GPIO.output(cPin,GPIO.HIGH);
def outData(data):
GPIO. output(latchPin,GPIO.LOW)
shiftOut(dataPin,clockPin,LSBFIRST,data)
GPIO. output(latchPin,GPIO.HIGH)
def loop():
moveSpeed = 0.1 #move speed delay, the larger, the slower
index = 0 #Starting from the array index 0