-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodlamp.ino
186 lines (150 loc) · 6.93 KB
/
moodlamp.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
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
/*------------------------------------------------------------------------------------------------------------------------
Open Hardware Mood Lamp
By: Daniel Spillere Andrade - www.DanielAndrade.net - daniel@danielandrade.net - https://github.com/dansku
https://github.com/dansku/Open-Hardware-Mood-Lamp
Based on the code from Arkadian: http://www.arkadian.eu/pages/219/arduino-controlled-ikea-lamp
This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.
Code version: 0.9
------------------------------------------------------------------------------------------------------------------------*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13); // RX, TX
int leds[] = {11,10,9}; // Goes for colors RED, GREEN, BLUE (you may have to change to get in this order)
int buttonPin = 8;
int buttonState = 0;
int states = 13; // number or color states
int currentState = 0;
int mydelay = 15;
long lastDebounceTime = 0; // button debounce
long debounceDelay = 300;
//-----[ Colors ]-------------------
const byte RED[] = {255, 0, 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 YELLOW[] = {255, 255, 0};
const byte PINK[] = {158, 4, 79};
const byte ORANGE[] = {255, 128, 0};
const byte BLUE2[] = {0, 128, 255};
const byte GREEN2[] = {128, 255, 0};
const byte BLUE3[] = {128, 0, 255};
const byte GREEN3[] = {0, 255, 128};
byte myOldC[] = {255, 255, 255};
//-----[ Setup & Loop ]--------------
void setup() {
for(int i = 0; i < 3; i++){ pinMode(leds[i], OUTPUT); }
pinMode(buttonPin,INPUT);
Serial.begin(9600); //for debugging
mySerial.begin(9600);
}
void loop() {
if (mySerial.available()) {
String line = mySerial.readString();
// print(">>"+line);
//(255,255,255)
int color1 = line.substring(1, 4).toInt();
int color2 = line.substring(5, 8).toInt();
int color3 = line.substring(9, 12).toInt();
if (line == "ON") {
print("ON");
setColor(leds, RED);
} else if (line == "OFF") {
print("OFF");
setColor(leds, BLACK);
} else {
byte COLOR[] = {color1, color2, color3};
print("COLOR received: {"+(String)color1+","+(String)color2+","+(String)color3+"}");
setColor(leds, COLOR);
}
}
// buttonRead();
// Serial.print("State: ");
// Serial.println(currentState,DEC); //for debugging
// if(currentState==12){ //faster
// randomC();
// mydelay = 10;
// }
//
// if(currentState==0){ //slow
// randomC();
// mydelay = 30;
// }
//
// if(currentState==1){ setColor(leds, RED); }
// if(currentState==2){ setColor(leds, GREEN); }
// if(currentState==3){ setColor(leds, BLUE); }
// if(currentState==4){ setColor(leds, WHITE); }
// if(currentState==5){ setColor(leds, YELLOW); }
// if(currentState==6){ setColor(leds, PINK); }
// if(currentState==7){ setColor(leds, ORANGE); }
// if(currentState==8){ setColor(leds, BLUE2); }
// if(currentState==9){ setColor(leds, GREEN2); }
// if(currentState==10){ setColor(leds, BLUE3); }
// if(currentState==11){ setColor(leds, GREEN3); }
//// if(currentState==13){ setColor(leds, BLACK); }
}
void print(String string) {
Serial.print(string);
mySerial.print(string);
}
//-----[ Functions ]-----------
void buttonRead(){
buttonState = digitalRead(buttonPin);
if ((millis() - lastDebounceTime) > debounceDelay && digitalRead(buttonPin) == 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();
currentState = (currentState + 1) % states;
}
}
void setColor(int* led, byte* color){
for(int i = 0; i < 3; i++){
analogWrite(led[i], color[i]);
}
}
void setColor(int* led, const byte* color){
byte tempByte[] = {color[0], color[1], color[2]};
setColor(led, tempByte);
}
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
buttonRead();
if(currentState==0){delay(fadeSpeed);}
if(currentState==12){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);
}
void randomC(){
int tmp = int(random(0,10));
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] = GREEN[i];}}
if(tmp == 2){for(int i = 0; i < 3; i++){myRandomC[i] = BLUE[i];}}
if(tmp == 3){for(int i = 0; i < 3; i++){myRandomC[i] = WHITE[i];}}
if(tmp == 4){for(int i = 0; i < 3; i++){myRandomC[i] = YELLOW[i];}}
if(tmp == 5){for(int i = 0; i < 3; i++){myRandomC[i] = PINK[i];}}
if(tmp == 6){for(int i = 0; i < 3; i++){myRandomC[i] = ORANGE[i];}}
if(tmp == 7){for(int i = 0; i < 3; i++){myRandomC[i] = BLUE2[i];}}
if(tmp == 8){for(int i = 0; i < 3; i++){myRandomC[i] = GREEN2[i];}}
if(tmp == 9){for(int i = 0; i < 3; i++){myRandomC[i] = BLUE3[i];}}
if(tmp == 10){for(int i = 0; i < 3; i++){myRandomC[i] = GREEN3 [i];}}
fadeToColor(leds, myOldC, myRandomC, mydelay );
myOldC[0]= myRandomC[0];
myOldC[1]= myRandomC[1];
myOldC[2]= myRandomC[2];
}