-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSC_Eos_Sub_37_with_Feedback.ino
235 lines (183 loc) · 5.71 KB
/
OSC_Eos_Sub_37_with_Feedback.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
#include <SPI.h> // needed for Arduino versions later than 0018
//#include <Ethernet.h>
//#include <EthernetUdp.h> // UDP library from: [email protected] 12/30/2008
#include <EthernetV2_0.h>
#include <EthernetUdpV2_0.h> // UDP library from: [email protected] 12/30/2008
#include <EEPROM.h>
//#include <OSCMessage.h>
#include <OSCBundle.h>
//#include <OSCBoards.h>
#include <AFMotor.h>
// DC motor on M2
AF_DCMotor motor(1);
const int wiper = A0; //Position of fader relative to GND (Analog 0)
double faderMax = 0; //Value read by fader's maximum position (0-1023)
double faderMin = 0; //Value read by fader's minimum position (0-1023)
int ledState = LOW;
int ledPin = 5; //pin 13 on Arduino Uno. Pin 6 on a Teensy++2
int state = 0;
int sensorPin = A0;
//int sensorPin1 = A1;
double sensorValue = 0;
double outputValue = 0;
double sensorVal = 0;
int sensorValue1 = 0;
int outputValue1 = 0;
int sensorVal1 = 0;
double sensorValstring = 0;
int THRESHOLD = 2; //define a threshold amount
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[6] = {
0x90, 0xA2, 0xDA, 0x0E, 0xDA, 0x60 };
byte subnet[] = {
255, 255, 0, 0 };
byte gateway[] = {
10, 101, 95, 115 };
IPAddress ip(10, 101, 95, 115);
unsigned int localPort = 8000; // local port to listen on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
IPAddress RemIP(10, 101, 95, 101);
unsigned int RemPort = 3501; // remote port to listen on
char macstr[18];
const byte ID = 0x92; //used to identify if valid data in EEPROM the "know" bit,
// if this is written in EEPROM the sketch has ran before
// We use this, so that the very first time you'll run this sketch it will use
// the values written above.
// defining which EEPROM address we are using for what data
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
#define W5200_CS 10
#define SDCARD_CS 4
void setup()
{
ShieldSetup ();//setup ethernet shield
// start the Ethernet and UDP:
// Ethernet.begin(mac, ip);
motor.setSpeed(200);
motor.run(RELEASE);
calibrateFader();
}
void ShieldSetup()
{ // Random MAC address stored in EEPROM
if (EEPROM.read(1) == '#') {
for (int i = 2; i < 6; i++) {
mac[i] = EEPROM.read(i);
}
}
else {
randomSeed(analogRead(0));
for (int i = 2; i < 6; i++) {
mac[i] = random(0, 255);
EEPROM.write(i, mac[i]);
}
EEPROM.write(1, '#');
}
int idcheck = EEPROM.read(0);
if (idcheck != ID){
//ifcheck id is not the value as const byte ID,
//it means this sketch has NOT been used to setup the shield before
//just use the values written in the beginning of the sketch
}
if (idcheck == ID){
//if id is the same value as const byte ID,
//it means this sketch has been used to setup the shield.
//So we will read the values out of EERPOM and use them
//to setup the shield.
for (int i = 2; i < 6; i++) {
mac[i] = EEPROM.read(i);
}
}
snprintf(macstr, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
// start the Ethernet and UDP:
pinMode(SDCARD_CS,OUTPUT);
digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
Ethernet.begin(mac, ip, gateway, gateway, subnet);
Udp.begin(localPort);
}
void loop() {
OSCMsgReceive();
Subrun();
}
void Subrun(){
sensorValue = analogRead(sensorPin);
outputValue = sensorValue;
outputValue = constrain(outputValue, 0, 1000);
sensorValstring = outputValue/10;
sensorValstring = sensorValstring -2;
//reads and dispatches the incoming message
if (sensorVal == sensorValue) {
// Do Nothing
}
else
{
OSCMessage msg1("/eos/sub/1");
msg1.add(sensorValstring);
Udp.beginPacket(RemIP, RemPort);
msg1.send(Udp);
Udp.endPacket();
msg1.empty();
}
delay(100);
sensorVal = sensorValue;
}
void OSCMsgReceive(){
OSCMessage msgIN;
int size;
if((size = Udp.parsePacket())>0){
while(size--)
msgIN.fill(Udp.read());
if(!msgIN.hasError()){
msgIN.route("/fader/value1",moveFader);
msgIN.route("/OnOff/toggle1",toggleOnOff);
}
}
}
void toggleOnOff(OSCMessage &msg, int addrOffset){
ledState = (boolean) msg.getFloat(0);
OSCMessage msgOUT("/OnOff/toggle2");
motor.run(BACKWARD);
delay(500);
motor.run(RELEASE);
ledState = !ledState; // toggle the state from HIGH to LOW to HIGH to LOW ...
Udp.beginPacket(RemIP, RemPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
}
void moveFader(OSCMessage &msg, int addrOffset){
state = (boolean) msg.getFloat(0);
// state = state * 10;
OSCMessage msgOUT("/Fader/Mov");
msgOUT.add(state);
Udp.beginPacket(RemIP, RemPort);
msgOUT.send(Udp); // send the bytes
Udp.endPacket(); // mark the end of the OSC Packet
msgOUT.empty(); // free space occupied by message
if (state < sensorValue - 10 && state > faderMin) {
motor.run(BACKWARD);
while (state < sensorValue - 10) {
}; //Loops until motor is done moving
motor.run(RELEASE);
}
else if (state > sensorValue + 10 && state < faderMax) {
motor.run(FORWARD);
while (state > sensorValue + 10) {
}; //Loops until motor is done moving
motor.run(RELEASE);
}
motor.run(RELEASE);
delay(100);
}
void calibrateFader() {
//Send fader to the top and read max position
motor.run(FORWARD);
delay(500);
motor.run(RELEASE);
faderMax = analogRead(sensorPin);
//Send fader to the bottom and read max position
motor.run(BACKWARD);
delay(500);
motor.run(RELEASE);
faderMin = analogRead(sensorPin);
}