-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgb.cc
82 lines (60 loc) · 1.33 KB
/
rgb.cc
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
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
int led = 6;
int brightness = 0;
int fadeAmount = 5;
struct LED {
LED(uint8_t pin, int br) : pin(pin), brightness(br) {}
void setup() {
pinMode(pin, OUTPUT);
}
void write() {
analogWrite(pin, brightness);
}
uint8_t pin;
int brightness;
};
struct Pulsor {
Pulsor(const LED &l, int step=5, int max_br = 255, int min_br = 0)
: led(l), min_br(min_br), max_br(max_br), step(step) { }
void pulse() {
int &brightness = led.brightness;
analogWrite(led.pin, led.brightness);
brightness += step;
if (brightness >= max_br) {
brightness = max_br;
step *= -1;
} else if (brightness <= min_br) {
brightness = min_br;
step *= -1;
}
}
int min_br;
int max_br;
int step;
LED led;
};
LED blue(6, 0);
LED green(3, 125);
LED red(5, 250);
LED all(9, 255);
Pulsor p_blue(blue, 1, 100);
Pulsor p_green(green, 2);
Pulsor p_red(red, 3);
void setup() {
blue.setup();
green.setup();
red.setup();
all.setup();
}
void loop() {
p_red.pulse();
p_blue.pulse();
p_green.pulse();
all.write();
// wait for 30 milliseconds to see the dimming effect
delay(30);
}