总体
一、arduino
接线
代码
/******************************************************************************
*name:Rainbow LED
*Function:RGB LED flash red, green and blue first, and then change to red, orange, yellow, green, blue, indio and purple
*Note: because the RGB module is common anode, so when you want to turn on a color, you need to set it as 0
*******************************************************************************/
const int redPin = 11; //R petal on RGB LED module connect to digital pin 11
const int greenPin = 10; //G petal on RGB LED module connect to digital pin 10
const int bluePin = 9; //B petal on RGB LED module connect to digital pin 9
/******************************************************************************/
void setup()
{
pinMode(redPin, OUTPUT); //set the redPin to be an output
pinMode(greenPin, OUTPUT); // set the greenPin to be an output
pinMode(bluePin, OUTPUT); // set the bluePin to be an output
}
/***************************************************************************/
void loop()
{
//Basic colors;
color(0,255,255); //turn the RGB LED red
delay(1000); //delay for 1s
color(255,0,255); //turn the RGB LED green
delay(1000);
color(255,255,0); //turn the RGB LED blue
delay(1000);
//Example blended colors:
color(0,255,255); //turn the RGB LED red
delay(1000); //delay for 1s
color(0,128,255); //orange
delay(1000);
color(0,0,255); //yellow
delay(1000);
color(255,0,255); //green
delay(1000);
color(255,255,0); //blue
delay(1000);
color(255,0,0); //indio
delay(1000);
color(128,255,0); //purple
delay(1000);
}
/**************************************************************************/
void color(unsigned char red, unsigned char green, unsigned char blue) //the color generatating function
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
/*******************************************************************************/
二、树莓派
接线
代码
C语言版
1 #include <wiringPi.h>
2 #include <softPwm.h>
3 #include <stdio.h>
4 #define uchar unsigned char
5 #define LedPinRed 0
6 #define LedPinGreen 1
7 #define LedPinBlue 2
8 void ledInit(void)
9 {
10 softPwmCreate(LedPinRed, 0, 100);
11 softPwmCreate(LedPinGreen, 0, 100);
12 softPwmCreate(LedPinBlue, 0, 100);
13 }
14
15 void ledColorSet(uchar r_val, uchar g_val, uchar b_val)
16 {
17 softPwmWrite(LedPinRed, r_val);
18 softPwmWrite(LedPinGreen, g_val);
19 softPwmWrite(LedPinBlue, b_val);
20 }
21
22 int main(void)
23 {
24 int i;
25 if(wiringPiSetup() == -1){ //when initialize wiring failed, print massage to screen
26 printf("setup wiringPi failed !");
27 return 1;
28 }
29 printf("linker LedPin: GPIO %d(wiringPi pin)\n", LedPinRed); //when initialize wiring successfully, print message to screen
30
31 ledInit();
32 while(1){
33 ledColorSet(0xff,0x00,0x00); //red
34 delay(500);
35 ledColorSet(0x00,0xff,0x00); //green
36 delay(500);
37 ledColorSet(0x00,0x00,0xff); //blue
38 delay(500);
39 ledColorSet(0xff,0xff,0x00); //yellow
40 delay(500);
41 ledColorSet(0xff,0x00,0xff); //pick
42 delay(500);
43 ledColorSet(0xc0,0xff,0x3e);
44 delay(500);
45 ledColorSet(0x94,0x00,0xd3);
46 delay(500);
47 ledColorSet(0x76,0xee,0x00);
48 delay(500);
49 ledColorSet(0x00,0xc5,0xcd);
50 delay(500);
51 }
52 return 0;
53 }
Python版本
1 #!/usr/bin/env python
2 import RPi.GPIO as GPIO
3 import time
4
5 colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
6 R = 11
7 G = 12
8 B = 13
9
10 def setup(Rpin, Gpin, Bpin):
11 global bins
12 global p_R, p_G, p_B
13 pins = {'pin_R':Rpin, 'pin_G':Gpin, 'pin_B':Bpin}
14 GPIO.setmode(GPIO.BOARD) #Numbers GPIOs by phisical location
15 for i in pins:
16 GPIO.setup(pins[i], GPIO.OUT) #Set pin's mode is output
17 GPIO.output(pins[i], GPIO.HIGH) #Set pins to high(+3.3V) to off led
18
19 p_R = GPIO.PWM(pins['pin_R'], 2000) #set Frequency to 2KHz
20 p_G = GPIO.PWM(pins['pin_G'], 1999)
21 p_B = GPIO.PWM(pins['pin_B'], 5000)
22
23 p_R.start(100) #Initial duty Cycle = 0 (leds off)
24 p_G.start(100)
25 p_B.start(100)
26
27 def map_led(x, in_min, in_max, out_min, out_max):
28 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min #这步应该是做归一化
29
30 def off():
31 for i in pins:
32 GPIO.output(pins[i], GPIO.HIGH) #Turn off all leds
33
34 def setColor(col): #For example: col = 0x112233 #位操作
35 R_val = (col & 0xff0000) >> 16
36 G_val = (col & 0x00ff00) >> 8
37 B_val = (col & 0x0000ff) >> 0
38
39 R_val = map_led(R_val, 0, 255, 0, 100)
40 G_val = map_led(G_val, 0, 255, 0, 100)
41 B_val = map_led(B_val, 0, 255, 0, 100)
42
43 p_R.ChangeDutyCycle(100-R_val)
44 p_G.ChangeDutyCycle(100-G_val)
45 p_B.ChangeDutyCycle(100-B_val)
46
47 def loop():
48 while True:
49 for col in colors:
50 setColor(col)
51 time.sleep(1)
52
53 def destroy():
54 p_R.stop()
55 p_G.stop()
56 p_B.stop()
57 off()
58 GPIO.cleanup()
59
60
61 if __name__ == "__main__":
62 try:
63 setup(R, G, B)
64 loop()
65 except KeyboardInterrupt:
66 destroy()
网友评论