forked from BadPractice/2_button_light_switch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
2_button_lightswitch.ino
283 lines (237 loc) · 6.43 KB
/
2_button_lightswitch.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
This example will transmit a universe via Art-Net into the Network.
This example may be copied under the terms of the MIT license, see the LICENSE file for details
*/
bool isButton0Down = false;
bool isButton1Down = false;
bool isButton2Down = false;
int currentLight = 0;
int currentMode = 9;
int currentSpeed = 75;
int discoCounter = 0;
byte colors[] = {0,0,0};
int lookupR[] = {1,0,0,1,1,0,1,0,0,0,1};
int lookupG[] = {0,1,0,1,0,1,1,0,0,0,1};
int lookupB[] = {0,0,1,0,1,1,1,0,0,0,1};
int lookupA[] = {0,0,0,0,0,0,0,1,0,1,0};
int lookupW[] = {0,0,0,0,0,0,0,0,1,1,0};
#define BUTTON0 4
//D2 on the ESP8266 D1 mini
#define BUTTON1 5
//D1
#define BUTTON2 0
//D3
//fourth button is reset
#if defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include <WiFiUdp.h>
#include <ArtnetWifi.h>
//Wifi settings
const char* ssid = "metalights"; // CHANGE FOR YOUR SETUP
const char* password = "metalights"; // CHANGE FOR YOUR SETUP
// Artnet settings
ArtnetWifi artnet;
const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as 0.
const char host[] = "10.20.255.255"; // CHANGE FOR YOUR SETUP your destination
/*IPAddress ip(10,20,30,69);
//IPAddress broadcastIp(10,20,255,255);
IPAddress broadcastIp(255,255,255,255);
IPAddress netmask(255, 255, 0, 0);
char packet[]="Art-Net\0\0\x50\0\x0d\0\0\0\x03\0\x3caaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
//char packet[]="Art-Net\0\0\x50\0\x0d\0\0\x03\0\0\x3caaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
WiFiUDP Udp;
unsigned int localUdpPort = 4211; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
*/
int lastPot = 0;
// connect to wifi – returns true if successful or false if not
boolean ConnectWifi(void)
{
boolean state = true;
int i = 0;
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 20){
state = false;
break;
}
i++;
}
if (state){
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
void setup()
{
Serial.begin(115200);
ConnectWifi();
artnet.begin(host);
artnet.setLength(60);
artnet.setUniverse(3);
//Udp.begin(localUdpPort);
{
pinMode(BUTTON0, INPUT_PULLUP);
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
}
randomSeed(analogRead(0));
}
void loop()
{
uint8_t i, j;
if(digitalRead(BUTTON0) == 0)
{
isButton0Down = true;
}
else if(isButton0Down == true)
{
isButton0Down = false;
if (currentMode == 10) changeSpeed(10);
else changeLight(25);
}
if(digitalRead(BUTTON1) == 0)
{
isButton1Down = true;
}
else if(isButton1Down == true)
{
isButton1Down = false;
if (currentMode == 10) changeSpeed(-10);
else changeLight(-25);
}
if(digitalRead(BUTTON2) == 0)
{
isButton2Down = true;
}
else if(isButton2Down == true)
{
isButton2Down = false;
changeMode();
}
if (currentMode == 10)
{
discoCounter++;
if (discoCounter > currentSpeed)
{
discoCounter = 0;
sendOut();
}
}
delay(20);
}
void changeLight(int amount)
{
Serial.println("changeLight");
currentLight += amount;
if(currentLight > 250)
currentLight = 250;
if(currentLight < 0)
currentLight = 0;
sendOut();
}
void changeSpeed(int amount)
{
Serial.println("changeLight");
currentSpeed += amount;
if(currentSpeed > 150)
currentSpeed = 150;
if(currentSpeed < 5)
currentSpeed = 5;
}
void changeMode(){
Serial.println("changeMode");
currentMode = (currentMode + 1) % 11;
sendOut();
}
void sendOut(){
Serial.println("currentLight:");
Serial.println(currentLight);
Serial.println("currentMode:");
Serial.println(currentMode);
int j,i;
/*for(i = 18; i < 78; i = i + 5 ){
packet[ i ] = char(currentLight * lookupR[currentMode]);
packet[i+1] = char(currentLight * lookupG[currentMode]);
packet[i+2] = char(currentLight * lookupB[currentMode]);
packet[i+3] = char(currentLight * lookupA[currentMode]);
packet[i+4] = char(currentLight * lookupW[currentMode]);
}*/
if (currentMode == 10)
{
for (i = 0; i < 12; i++)
{
Wheel((byte)random(255));
for (j = 0; j < 6; j++)
{
artnet.setByte(0 + (i*5), (byte)(colors[0]));
artnet.setByte(1 + (i*5), (byte)(colors[1]));
artnet.setByte(2 + (i*5), (byte)(colors[2]));
artnet.setByte(3 + (i*5), (byte)(0));
artnet.setByte(4 + (i*5), (byte)(0));
artnet.write();
}
}
}
else
{
for(j = 0; j < 6; j++)
{
for (i = 0; i < 60; i++)
{
artnet.setByte(0 + (i*5), (byte)(currentLight * lookupR[currentMode]));
artnet.setByte(1 + (i*5), (byte)(currentLight * lookupG[currentMode]));
artnet.setByte(2 + (i*5), (byte)(currentLight * lookupB[currentMode]));
artnet.setByte(3 + (i*5), (byte)(currentLight * lookupA[currentMode]));
artnet.setByte(4 + (i*5), (byte)(currentLight * lookupW[currentMode]));
artnet.write();
//Udp.beginPacket(broadcastIp, 6454);
//Udp.write(packet);
//Udp.endPacket();
}
delay(20);
}
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
void Wheel(byte colIndex)
{
//int colIndex = WheelPos[0];
if(colIndex < 85)
{
colors[0] = colIndex * 3;
colors[1] = 255 - colIndex * 3;
colors[2] = 0;
}
else if(colIndex < 170)
{
colIndex -= 85;
colors[0] = 255 - colIndex * 3;
colors[1] = 0;
colors[2] = colIndex * 3;
}
else
{
colIndex -= 170;
colors[0] = 0;
colors[1] = colIndex * 3;
colors[2] = 255 - colIndex * 3;
}
}