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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
| #include <EEPROM.h>
//RGB LED pins - These pins must be PWM
//here: 9 = redPin, 10 = greenPin, 11 = bluePin
int ledAnalogOne[] = {
9, 10, 11};
// Push button, any free digital pin will do
int myButton = 7;
// I will fix pin 13 at HIGH at setup and use it as another voltage pin.
// It also lights the onboard led.
int my13 = 13;
// Reading the previous selection from the eeprom memory
int myOption = EEPROM.read(0);
int options = 15;
// these following are useful for debouncing
int mydelay = 10;
int reading = 0;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 300; // the debounce time; increase if the output flickers
//Defined Colors (different RGB (red, green, blue) values for colors
const byte RED[] = {255, 0, 0};
const byte ORANGE[] = {83, 4, 0};
const byte YELLOW[] = {255, 255, 0};
const byte GREEN[] = {0, 255, 0};
const byte BLUE[] = {0, 0, 255};
const byte WHITE[] = {255, 255, 255};
const byte BLACK[] = {0, 0, 0};
const byte PINK[] = {158, 4, 79};
const byte B2[] = {0, 128, 255};
const byte G2[] = {128, 255, 0};
const byte R2[] = {255, 128, 0};
const byte B3[] = {128, 0, 255};
const byte G3[] = {0, 255, 128};
const byte R3[] = {255, 0, 128};
byte myOldC[] = {255, 255, 255};
void setup(){
for(int i = 0; i < 3; i++){pinMode(ledAnalogOne[i], OUTPUT);}
setColor(ledAnalogOne, BLACK); //Turn off led 1
pinMode(my13, OUTPUT);
digitalWrite(my13, HIGH);
Serial.begin (9600);
}
void loop(){
readButton();
if(myOption==0){
mydelay = 15;
randomC();
}
if(myOption==1){
mydelay = 0;
setColor(ledAnalogOne, RED);
}
if(myOption==2){
mydelay = 0;
setColor(ledAnalogOne, BLUE);
}
if(myOption==3){
mydelay = 0;
setColor(ledAnalogOne, GREEN);
}
if(myOption==4){
mydelay = 0;
setColor(ledAnalogOne, YELLOW);
}
if(myOption==5){
mydelay = 0;
setColor(ledAnalogOne, PINK);
}
if(myOption==6){
mydelay = 0;
setColor(ledAnalogOne, ORANGE);
}
if(myOption==7){
mydelay = 0;
setColor(ledAnalogOne, B2);
}
if(myOption==8){
mydelay = 0;
setColor(ledAnalogOne, G2);
}
if(myOption==9){
mydelay = 0;
setColor(ledAnalogOne, R2);
}
if(myOption==10){
mydelay = 0;
setColor(ledAnalogOne, B3);
}
if(myOption==11){
mydelay = 0;
setColor(ledAnalogOne, G3);
}
if(myOption==12){
mydelay = 0;
setColor(ledAnalogOne, R3);
}
if(myOption==13){
mydelay = 0;
setColor(ledAnalogOne, WHITE);
}
if(myOption==14){
mydelay = 0;
setColor(ledAnalogOne, BLACK);
}
}
//////////////////////////////////////////////////////////////
void readButton(){
reading = digitalRead(myButton);
if ((millis() - lastDebounceTime) > debounceDelay && digitalRead(myButton)== HIGH) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
lastDebounceTime = millis();
myOption = (myOption + 1) % options;
EEPROM.write(0, myOption);
// Serial.print ("PRESSED - ");
// Serial.println (myOption);
}
}
//////////////////////////////////////////////////////////////
void randomC(){
int tmp = int(random(0,12));
byte myRandomC[] = {0,0,0};
if(tmp == 0){for(int i = 0; i < 3; i++){myRandomC[i] = RED[i];}}
if(tmp == 1){for(int i = 0; i < 3; i++){myRandomC[i] = ORANGE[i];}}
if(tmp == 2){for(int i = 0; i < 3; i++){myRandomC[i] = YELLOW[i];}}
if(tmp == 3){for(int i = 0; i < 3; i++){myRandomC[i] = GREEN[i];}}
if(tmp == 4){for(int i = 0; i < 3; i++){myRandomC[i] = BLUE[i];}}
if(tmp == 5){for(int i = 0; i < 3; i++){myRandomC[i] = B2[i];}}
if(tmp == 6){for(int i = 0; i < 3; i++){myRandomC[i] = G2[i];}}
if(tmp == 7){for(int i = 0; i < 3; i++){myRandomC[i] = R2[i];}}
if(tmp == 8){for(int i = 0; i < 3; i++){myRandomC[i] = B3[i];}}
if(tmp == 9){for(int i = 0; i < 3; i++){myRandomC[i] = G3[i];}}
if(tmp ==10){for(int i = 0; i < 3; i++){myRandomC[i] = R3[i];}}
if(tmp ==11){for(int i = 0; i < 3; i++){myRandomC[i] = WHITE[i];}}
if(tmp ==12){for(int i = 0; i < 3; i++){myRandomC[i] = PINK[i];}}
fadeToColor(ledAnalogOne, myOldC, myRandomC, mydelay );
myOldC[0]= myRandomC[0];
myOldC[1]= myRandomC[1];
myOldC[2]= myRandomC[2];
}
//////////////////////////////////////////////////////////////
////////////// Functions from oomlout.co.uk.... //////////////
//////////////////////////////////////////////////////////////
/* Sets the color of the LED to any RGB Value
led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin))
color - (byte array of three values defing an RGB color to display (color[0] = new Red value, color[1] = new Green value, color[2] = new Red value
*/
void setColor(int* led, byte* color){
for(int i = 0; i < 3; i++){ //iterate through each of the three pins (red green blue)
analogWrite(led[i], 255 - color[i]); //set the analog output value of each pin to the input value (ie led[0] (red pin) to 255- color[0] (red input color)
//we use 255 - the value because our RGB LED is common anode, this means a color is full on when we output analogWrite(pin, 0)
//and off when we output analogWrite(pin, 255).
}
}
/* A version of setColor that takes a predefined color (neccesary to allow const int pre-defined colors */
void setColor(int* led, const byte* color){
byte tempByte[] = {color[0], color[1], color[2]};
setColor(led, tempByte);
}
/* Fades the LED from a start color to an end color at fadeSpeed
led - (int array of three values defining the LEDs pins (led[0] = redPin, led[1] = greenPin, led[2] = bluePin))
startCcolor - (byte array of three values defing the start RGB color (startColor[0] = start Red value, startColor[1] = start Green value, startColor[2] = start Red value
endCcolor - (byte array of three values defing the finished RGB color (endColor[0] = end Red value, endColor[1] = end Green value, endColor[2] = end Red value
fadeSpeed - this is the delay in milliseconds between steps, defines the speed of the fade
*/
void fadeToColor(int* led, byte* startColor, byte* endColor, int fadeSpeed){
int changeRed = endColor[0] - startColor[0]; //the difference in the two colors for the red channel
int changeGreen = endColor[1] - startColor[1]; //the difference in the two colors for the green channel
int changeBlue = endColor[2] - startColor[2]; //the difference in the two colors for the blue channel
int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); //make the number of change steps the maximum channel change
for(int i = 0 ; i < steps; i++){ //iterate for the channel with the maximum change
byte newRed = startColor[0] + (i * changeRed / steps); //the newRed intensity dependant on the start intensity and the change determined above
byte newGreen = startColor[1] + (i * changeGreen / steps); //the newGreen intensity
byte newBlue = startColor[2] + (i * changeBlue / steps); //the newBlue intensity
byte newColor[] = {newRed, newGreen, newBlue}; //Define an RGB color array for the new color
setColor(led, newColor); //Set the LED to the calculated value
readButton();
if(myOption==0){delay(fadeSpeed);}
}
setColor(led, endColor); //The LED should be at the endColor but set to endColor to avoid rounding errors
}
/* A version of fadeToColor that takes predefined colors (neccesary to allow const int pre-defined colors */
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){
byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
}
////////////////////////////////////////////////////////////// |