54
softPwmWrite(LedPinGreen, g_val);
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
Содержание Super Kit 3.0
Страница 7: ...4 22 RGB LED 1 23 LED red 8 24 LED white 4 25 LED green 4 26 LED yellow 4 27 NPN Transistor S8050 2 ...
Страница 30: ...27 Also we have drawn a table of the corresponding BCM wiringPi and Name of each pins ...
Страница 78: ...75 Now pull the slide and you can see the two LEDs light up alternately ...