forked from jherland/arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb_led.ino
83 lines (73 loc) · 2.09 KB
/
rgb_led.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
/*
* Switch and RGB LED test program
*
* Wiring
* ------
* Connect a switch and an RGB LED to the Arduino as follows:
* 1. Connect one side of the switch to ground.
* 2. Connect the other side of the switch to Arduino digital pin 2
* through a 100Ω resistor, and to +5V through a 10kΩ pull-up resistor.
* 3. Connect the longest pin (pin 2) of RGB LED to ground (0V).
* 4. Connect the other pins of the RGB LED as follows:
* - LED pin 1 (red) through 1kΩ to Arduino digital pin 11 (PWM)
* - LED pin 3 (green) through 3.3kΩ to Arduino digital pin 10 (PWM)
* - LED pin 4 (blue) through 1kΩ to Arduino digital pin 9 (PWM)
*
* Behavior
* --------
* 0. Initially, the LED will be dark.
* 1. Press the switch once to start repeatedly fading the red component
* up and down.
* 2. Press the switch again to lock the red component, and the green
* component will start to fade up and down.
* 3. Press the switch to lock the green component, and the blue component
* will start to fade up and down.
* 4. Press the switch to lock the blue component. The LED will now
* display the three locked components.
* 5. Press again to return to step #1.
*
* Johan Herland <johan@herland.net>
*/
const int switchPin = 2;
const int numLeds = 3;
const int ledPin[numLeds] = {11, 10, 9};
const int numstates = numLeds + 1;
int val, pval;
unsigned long bounceTimeout;
int state;
byte lumi[numLeds];
boolean up;
void setup()
{
pinMode(switchPin, INPUT);
for (int i = 0; i < numLeds; i++) {
pinMode(ledPin[i], OUTPUT);
lumi[i] = 0;
}
bounceTimeout = 0;
state = numstates - 1;
up = true;
}
void loop()
{
val = digitalRead(switchPin);
if (val != pval && millis() > bounceTimeout) {
if (val == LOW)
state = (state + 1) % numstates;
pval = val;
bounceTimeout = millis() + 100; // set new bounce timeout
}
if (up && state < numLeds) {
lumi[state] += 5;
if (lumi[state] == 255)
up = false;
}
else if (state < numLeds) {
lumi[state] -= 5;
if (lumi[state] == 0)
up = true;
}
for (int i = 0; i < numLeds; i++)
analogWrite(ledPin[i], lumi[i]);
delay(20);
}