/
whale-interface-OSC.ino
131 lines (104 loc) · 3.5 KB
/
whale-interface-OSC.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
#include <SPI.h>
//#include <OSCMessage.h>
#include <OSCBundle.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
// These constants won't change. They're used to give names to the pins used:
const int analogIn0 = A0; // Analog input pin that the potentiometer is attached to
const int analogIn1 = A1; // Analog input pin that the potentiometer is attached to
const int analogIn2 = A2; // Analog input pin that the potentiometer is attached to
const int analogIn3 = A3; // Analog input pin that the potentiometer is attached to
int buttonVal0 = 0; // value read from the buttons
int buttonVal1 = 0; // value read from the buttons
int buttonVal2 = 0; // value read from the buttons
int buttonVal3 = 0; // value read from the buttons
// constants won't change. They're used here to set pin numbers:
int status = WL_IDLE_STATUS;
#include "arduino_secrets.h"
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
//destination IP
IPAddress outIp(172, 20, 10, 2);
WiFiUDP Udp;
void setup() {
// initialize the pushbutton pin as an input:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
connectToWifi();
Udp.begin(8888);
}
void loop() {
sendSensorsOverOSC();
}
void sendSensorsOverOSC() {
// read the analog in value:
buttonVal0 = analogRead(analogIn0);
// delay(1);
buttonVal1 = analogRead(analogIn1);
// delay(1);
buttonVal2 = analogRead(analogIn2);
// delay(1);
buttonVal3 = analogRead(analogIn3);
// delay(2);
// print the results to the Serial Monitor:
// Serial.print("button0 = ");
// Serial.print(buttonVal0);
// Serial.print("\t button1 = ");
// Serial.print(buttonVal1);
// Serial.print("\t button2 = ");
// Serial.print(buttonVal2);
// Serial.print("\t button3 = ");
// Serial.println(buttonVal3);
// make a new OSC message
OSCBundle buttons;
buttons.add("/button0").add((int32_t)buttonVal0);
buttons.add("/button1").add((int32_t)buttonVal1);
buttons.add("/button2").add((int32_t)buttonVal2);
buttons.add("/button3").add((int32_t)buttonVal3);
//send the bundle together
Udp.beginPacket(outIp, 5005);
buttons.send(Udp);
Udp.endPacket();
buttons.empty();
delay(2);
}
void connectToWifi() {
Serial.println("Connecting to wifi...");
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWiFiStatus();
}
void printWiFiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}