-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBlink_pwm_Dragonfly.ino
121 lines (101 loc) · 2.88 KB
/
Blink_pwm_Dragonfly.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Blink_pwm_Dragonfly
Runs through the full 255 color spectrum for three rgb leds using pwm pins3,4,5 and 6,8,9, and 20, 21, 22
This example code is in the public domain.
*/
// Set up the rgb led names
int led1R = 4;
int led1G = 5;
int led1B = 3;
int led2R = 9;
int led2G = 8;
int led2B = 6;
int led3R = 31;
int led3G = 39;
int led3B = 30;
uint8_t ledArray[9] = {led1R, led1G, led1B, led2R, led2G, led2B, led3R, led3G, led3B};
const boolean invert = false; // set true if common anode, false if common cathode
uint8_t color = 0; // a value from 0 to 255 representing the hue
uint8_t R, G, B; // the Red Green and Blue color components
uint8_t brightness = 1023; // 1023 is maximum brightness, but can be changed
// the setup routine runs once when you press reset:
void setup()
{
analogWriteResolution(10); // default is 8 which means 256 colors
for(int i=0; i<10; i++) {
analogWrite(ledArray[i], 1023); // test high output of all leds in sequence
delay(1000);
analogWrite(ledArray[i], 0);
}
}
// void loop runs over and over again
void loop()
{
for (color = 0; color < 255; color++) { // Slew through the color spectrum
hueToRGB(color, brightness); // call function to convert hue to RGB
// write the RGB values to the pins
analogWrite(led1B, B);
analogWrite(led1R, R);
analogWrite(led1G, G);
analogWrite(led2B, B);
analogWrite(led2R, R);
analogWrite(led2G, G);
analogWrite(led3B, B);
analogWrite(led3R, R);
analogWrite(led3G, G);
delay(100);
}
}
// Courtesy http://www.instructables.com/id/How-to-Use-an-RGB-LED/?ALLSTEPS
// function to convert a color to its Red, Green, and Blue components.
void hueToRGB(uint8_t hue, uint8_t brightness)
{
uint16_t scaledHue = (hue * 6);
uint8_t segment = scaledHue / 256; // segment 0 to 5 around the
// color wheel
uint16_t segmentOffset =
scaledHue - (segment * 256); // position within the segment
uint8_t complement = 0;
uint16_t prev = (brightness * ( 255 - segmentOffset)) / 256;
uint16_t next = (brightness * segmentOffset) / 256;
if(invert)
{
brightness = 255 - brightness;
complement = 255;
prev = 255 - prev;
next = 255 - next;
}
switch(segment ) {
case 0: // red
R = brightness;
G = next;
B = complement;
break;
case 1: // yellow
R = prev;
G = brightness;
B = complement;
break;
case 2: // green
R = complement;
G = brightness;
B = next;
break;
case 3: // cyan
R = complement;
G = prev;
B = brightness;
break;
case 4: // blue
R = next;
G = complement;
B = brightness;
break;
case 5: // magenta
default:
R = brightness;
G = complement;
B = prev;
break;
}
}