-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSC__with_two_Encoders_SDsetup.ino
410 lines (309 loc) · 9.89 KB
/
OSC__with_two_Encoders_SDsetup.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//THESE ARE THE LIBRARIES THAT NEED TO BE INSTALLED FOR THIS TO WORK
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <OSCMessage.h>
#include <SD.h>
#define ENCODER_DO_NOT_USE_INTERRUPTS
#include <Encoder.h>
Encoder myEnc1(6, 7); // the number of the encoder 1 pins
Encoder myEnc2(8, 9); // the number of the encoder 2 pins
const int buttonPin = A0; // the number of the pushbutton pin
const int buttonPin1 = A1; // the number of the pushbutton pin
const int ledPin = 3;
const int ledPin1 = 5;
long oldPosition1 = -999;
long oldPosition2 = -999;
int lastButtonState = LOW; // the previous reading from the input pin
int lastButtonState1 = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
long lastDebounceTime1 = 0; // the last time the output pin was toggled
long debounceDelay1 = 50; // the debounce time; increase if the output flickers
int buttonState = 0; // variable for reading the pushbutton status
int buttonState1 = 0; // variable for reading the pushbutton status
char oscMsg1[32];
char oscMsg2[32];
char oscMsg3[32];
char oscMsg4[32];
byte myMac[6] = {
};
byte myNM[4] = {
};
byte myIP[4] = {
};
byte myGW[4] = {
};
byte RemIP[4] = {
};
int RemPort = 0; // remote port to transmit to
EthernetUDP Udp;
//SETUP FOR PIN DEFINITION
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin1, INPUT_PULLUP);
//LEDs on BUTTONS
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 67);
pinMode(ledPin1, OUTPUT);
analogWrite(ledPin1, 67);
//ETHERNET ENABLE
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
ShieldSetup ();//setup ethernet shield
Udp.begin(RemPort);
}
//THIS SECTION LOADS SETTINGS.TXT FILE INTO LOCAL MEMORY AND SETS UP ETHERNET
void ShieldSetup()
{
Serial.begin(9600);
while (!Serial) ;
if (!SD.begin(4)) Serial.println(F("SD fail"));
else Serial.println(F("SD ok"));
File fh = SD.open("settings.txt", FILE_READ);
char netBuffer[32];
if (!fh)
{
Serial.println(F("SD open fail"));
return;
}
int chPos = 0;
int lineNo = 0;
while (fh.available())
{
char ch = fh.read();
if (ch == '\n') {
chPos = 0;
//DIAG INFO FROM SD CARD
switch (lineNo) {
case 0:
if (getMAC(netBuffer, myMac)) Serial.println(F("mac ok"));
break;
case 2:
if (getIP(netBuffer, myIP)) Serial.println(F("ip ok"));
break;
case 4:
if (getIP(netBuffer, myNM)) Serial.println(F("NM ok"));
break;
case 6:
if (getIP(netBuffer, myGW)) Serial.println(F("GW ok"));
break;
case 8:
if (getIP(netBuffer, RemIP)) Serial.println(F("CNSL ok"));
break;
case 10:
RemPort = atoi(&netBuffer[0]);
Serial.print(F("Port "));
Serial.println(RemPort);
break;
case 12:
strcpy( oscMsg1, netBuffer );
Serial.print(F("OSC Command1: "));
Serial.println(oscMsg1);
break;
case 14:
strcpy( oscMsg2, netBuffer );
Serial.print(F("OSC Command2: "));
Serial.println(oscMsg2);
break;
case 16:
strcpy( oscMsg3, netBuffer );
Serial.print(F("OSC Command3: "));
Serial.println(oscMsg3);
break;
case 18:
strcpy( oscMsg4, netBuffer );
Serial.print(F("OSC Command4: "));
Serial.println(oscMsg4);
break;
}
lineNo++;
}
else if (ch == '\r') {
// do nothing
}
else if (chPos < 32) {
netBuffer[chPos] = ch;
chPos++;
netBuffer[chPos] = 0;
}
}
fh.close();
int x;
//THIS PRINTS OUT DIAGNOSTIC INFO IF YOU ARE CONNECTED TO THE ARDUINO IDE
Serial.print("\r\nmac ");
for (x = 0; x < 6; x++) {
Serial.print(myMac[x], HEX);
if (x < 5) Serial.print(":");
}
Serial.print("\r\nip ");
for (x = 0; x < 4; x++) {
Serial.print(myIP[x], DEC);
if (x < 3) Serial.print(".");
}
Serial.print("\r\nnetmask ");
for (x = 0; x < 4; x++) {
Serial.print(myNM[x], DEC);
if (x < 3) Serial.print(".");
}
Serial.print("\r\ngateway ");
for (x = 0; x < 4; x++) {
Serial.print(myGW[x], DEC);
if (x < 3) Serial.print(".");
}
Serial.print("\r\nconsole ");
for (x = 0; x < 4; x++) {
Serial.print(RemIP[x], DEC);
if (x < 3) Serial.print(".");
}
Serial.println(F("\r\nStarting ethernet"));
Ethernet.begin(myMac, myIP, myGW, myGW, myNM);
Serial.println(Ethernet.localIP());
}
//MAIN LOOP OF PROGRAM THIS IS WHERE THE MAGIC HAPPENS
void loop() {
long newPosition1 = myEnc1.read();
long newPosition2 = myEnc2.read();
int reading = digitalRead(buttonPin);
int reading1 = digitalRead(buttonPin1);
//ENCODER ONE OSCMSG1
if (newPosition1 > oldPosition1)
{ //the message wants an OSC address as first argument
OSCMessage msg(oscMsg1);
msg.add("1");
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
oldPosition1 = newPosition1;
}
if (newPosition1 < oldPosition1)
{ //the message wants an OSC address as first argument
OSCMessage msg(oscMsg1);
msg.add("-1");
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
oldPosition1 = newPosition1;
}
//ENCODER 2 OSCMSG2
if (newPosition2 > oldPosition2)
{ //the message wants an OSC address as first argument
OSCMessage msg(oscMsg2);
msg.add("1");
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
oldPosition2 = newPosition2;
}
if (newPosition2 < oldPosition2)
{ //the message wants an OSC address as first argument
OSCMessage msg(oscMsg2);
msg.add("-1");
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
oldPosition2 = newPosition2;
}
//BUTTON 1 OSCMSG3
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is LOW
if (buttonState == LOW) {
//the message wants an OSC address as first argument
OSCMessage msg(oscMsg3);
msg.add("1");
analogWrite(ledPin, 255);
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
}
if (buttonState == HIGH) {
//the message wants an OSC address as first argument
OSCMessage msg(oscMsg3);
msg.add("0");
analogWrite(ledPin, 67);
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
}
}
}
//BUTTON 2 OSCMSG4
if (reading1 != lastButtonState1) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay1) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading1 != buttonState1) {
buttonState1 = reading1;
// only toggle the LED if the new button state is LOW
if (buttonState1 == LOW) {
OSCMessage msg(oscMsg4);
msg.add("1");
analogWrite(ledPin1, 255);
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
}
if (buttonState1 == HIGH) {
//the message wants an OSC address as first argument
OSCMessage msg(oscMsg4);
msg.add("0");
analogWrite(ledPin1, 67);
Udp.beginPacket(RemIP, RemPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
}
}
}
lastButtonState = reading;
lastButtonState1 = reading1;
}
//THIS IS PART OF THE SETUP THAT CONVERTS TEXT FROM THE SD CARD INTO NUMBERS FOR THE MAC ADDRESS AND IP ADDRESS
byte getMAC(char* macBuf, byte* thisMAC) {
byte thisLen = strlen(macBuf);
byte thisOctet = 1;
thisMAC[0] = strtol(&macBuf[0], NULL, 16);
for (int x = 0; x < thisLen; x++) {
if (macBuf[x] == ':') {
thisMAC[thisOctet] = strtol(&macBuf[x + 1], NULL, 16);
thisOctet++;
}
}
if (thisOctet == 6) return (1);
else return (0);
}
byte getIP(char* ipBuf, byte* thisIP) {
byte thisLen = strlen(ipBuf);
byte thisOctet = 1;
thisIP[0] = atoi(&ipBuf[0]);
for (int x = 0; x < thisLen; x++) {
if (ipBuf[x] == '.') {
thisIP[thisOctet] = atoi(&ipBuf[x + 1]);
thisOctet++;
}
}
if (thisOctet == 4) return (1);
else return (0);
}