#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCMessage.h>
EthernetUDP Udp;
//the Arduino's IP
IPAddress ip(192, 168, 2, 4);
//destination IP
IPAddress outIp(169, 254, 255, 255);
const unsigned int outPort = 9999;
byte mac[] = {
0x04, 0xE9, 0xE5, 0x00, 0x63, 0x54 }; // This is the current teensy's MAC address and can be found with the teensyMAC sketch.
int32_t XBEE1[12] = { 1, 2, 3, 4, 5, 6 };
int32_t XBEE2[12] = { 1, 2, 3, 4, 5, 6 };
int32_t XBEE3[12] = { 1, 2, 3, 4, 5, 6 };
byte checkArray1[] = {
126, 0, 20, 131, 0, 1, 0, 0, 1, 126, 0 };
byte checkArray2[] = {
126, 0, 20, 131, 0, 2, 0, 0, 1, 126, 0 };
byte checkArray3[] = {
126, 0, 20, 131, 0, 3, 0, 0, 1, 126, 0 };
byte bytenr1 = 0;
byte bytenr2 = 0;
byte bytenr3 = 0;
int32_t incomingByte1;
int32_t incomingByte2;
int32_t incomingByte3;
int32_t value1;
int32_t value2;
int32_t value3;
OSCMessage msg1("/XBEE1/");
OSCMessage msg2("/XBEE2/");
OSCMessage msg3("/XBEE3/");
void setup() {
Serial1.begin(115200);
Serial2.begin(115200);
Serial3.begin(115200);
Ethernet.begin(mac,ip);
Udp.begin(8888);
}
void loop() {
if (Serial1.available() > 0) {
incomingByte1 = Serial1.read();
if (bytenr1 < 11) {
if (incomingByte1 == checkArray1 [bytenr1] || bytenr1 == 6){
bytenr1 += 1;
}
else {
bytenr1 = 0;
}
}
else {
if (bytenr1 < 19) {
bytenr1 += 1;
}
else {
if (bytenr1 < 23) {
if (bytenr1 % 2) {
value1 = incomingByte1 << 8;
bytenr1 += 1;
}
else {
value1 = value1 + incomingByte1;
msg1.add(value1);
bytenr1 += 1;
}
}
if (bytenr1 == 23) {
Udp.beginPacket(outIp, outPort);
msg1.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg1.empty(); // free space occupied by message
bytenr1 = 0;
}
}
}
}
if (Serial2.available() > 0) {
incomingByte2 = Serial2.read();
if (bytenr2 < 11) {
if (incomingByte2 == checkArray2 [bytenr2] || bytenr2 == 6){
bytenr2 += 1;
}
else {
bytenr2 = 0;
}
}
else {
if (bytenr2 < 23) {
if (bytenr2 % 2) {
value2 = incomingByte2 << 8;
bytenr2 += 1;
}
else {
value2 = value2 + incomingByte2;
msg2.add(value2);
bytenr2 += 1;
}
}
if (bytenr2 == 23) {
Udp.beginPacket(outIp, outPort);
msg2.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg2.empty(); // free space occupied by message
bytenr2 = 0;
}
}
}
if (Serial3.available() > 0) {
incomingByte3 = Serial3.read();
if (bytenr3 < 11) {
if (incomingByte3 == checkArray3 [bytenr3] || bytenr3 == 6){
bytenr3 += 1;
}
else {
bytenr3 = 0;
}
}
else {
if (bytenr3 < 23) {
if (bytenr3 % 2) {
value3 = incomingByte3 << 8;
bytenr3 += 1;
}
else {
value3 = value3 + incomingByte3;
msg3.add(value3);
bytenr3 += 1;
}
}
if (bytenr3 == 23) {
Udp.beginPacket(outIp, outPort);
msg3.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg3.empty(); // free space occupied by message
bytenr3 = 0;
}
}
}
}