Python Code 9.1.1 ColorfulSoftlight
First observe the project result, and then analyze the code.
1. Use cd command to enter 09.1.1_ColorfulSoftlight directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/09.1.1_ ColorfulSoftlight
2. Use python command to execute python code "ColorfulSoftlight.py".
python ColorfulSoftlight.py
After the program is executed, rotate one of potentiometers, then the color of RGBLED will change
consequently. And the terminal window will print out the ADC value of each potentiometer.
The following is the program code:
im port RPi. GPIO as GPIO
im port smbus
im port time
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
ledRedPin = 15 #define 3 pins of RGBLED
ledGreenPin = 13
ledBluePin = 11
def analogRead(chn): #read ADC value
bus. write_byte(address,cmd+chn)
value = bus.read_byte(address)
value = bus. read_byte(address) #This function always return the last data read.
#If you want return the current data, you need execute it twice.
r eturn value
def analogWrite(value):
bus. write_byte_data(address,cmd,value)
def setup():
g lobal p_Red,p_Green,p_Blue
GPIO. setmode(GPIO.BOARD)
GPIO. setup(ledRedPin,GPIO.OUT) #set 3 pins of RGBLED to output mode
GPIO. setup(ledGreenPin,GPIO.OUT)
GPIO. setup(ledBluePin,GPIO.OUT)
p_Red = GPIO.PWM(ledRedPin,1000) #configure PWM to 3 pins of RGBLED
p_Red. start(0)
p_Green = GPIO. PWM(ledGreenPin,1000)
p_Green.start(0)
p_Blue = GPIO.PWM(ledBluePin,1000)
p_Blue.start(0)