User Manual

56
softPwmWrite(LedPinBlue, b_val);
}
ledColorSet(0xff,0x00,0x00); // red calls the function defined before. Write oxff into
LedPinRed and ox00 into LedPinGreen and LedPinBlue. Only the Red LED lights up after
running this code. If you want to light up LEDs in other colors, just modify the
parameters.
For Python users:
Step 2: Open the code file
cd /home/pi/SunFounder_Super_Kit_V3.0_for_Raspberry_Pi/Python
Step 3: Run
sudo python 05_rgb.py
Code Explanation
# Set up a color table in Hexadecimal
COLOR = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
# Set pins' channels with dictionary
pins = {'Red':17, 'Green':18, 'Blue':27}
p_R = GPIO.PWM(pins['Red'], 2000) # the same as the last lesson, here we configure
the channels and frequencies of the 3 PWM.
p_G = GPIO.PWM(pins['Green'], 2000)
p_B = GPIO.PWM(pins['Blue’], 2000)
p_R.start(0) # the same as the last lesson, the PWM of the 3 LEDs begin with 0.
p_G.start(0)
p_B.start(0)
# Define a MAP function for mapping values. Like from 0~255 to 0~100
def MAP(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def setColor(color): # configures the three LEDs’ luminance with the inputted
color value .
R_val = (color & 0xFF0000) >> 16 # these three lines are used for analyzing the
col variables
G_val = (color & 0x00FF00) >> 8 # assign the first two values of the
hexadecimal to R, the middle two assigned to G
B_val = (color & 0x0000FF) >> 0 # assign the last two values to B, please
refer to the shift operation of the hexadecimal for details.
SunFounder