Hi guys. I made this code to send UDP via ethernet with an ethernet shield. to send a packet to a to a cue software to tell it which cue to to "go" depending on what distance the viewer is at. I'm using an ultrasonic sensor and since the ultrasonic sensor is constantly sending data when the viewer is in the same position the arduino will constantly be sending a packet to the software which is not what I want. I used delays to stop the arduino from sending too many packets but I basically just want the arduino to send a packet once until the distance is changed. So even if the viewer stays at a distance, only one packet will be sent. This is my code:
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCMessage.h>
EthernetUDP Udp;
//the Arduino's IP
IPAddress ip(192, 168, 1, 21 );
//destination IP
IPAddress outIp(192, 168, 1, 20);
const unsigned int outPort = 53000;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields
const int trig = 9;
const int echo = 8;
int duration = 0;
int distance = 0;
void setup()
{
Ethernet.begin(mac,ip);
Udp.begin(53000);
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);
duration = pulseIn(echo , HIGH);
distance = (duration/2) / 28.5 ;
Serial.println(distance);
if ( distance <= 7 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/1/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
delay(2600);
}
else if ( distance <= 15 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/2/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
delay(3000);
}
else if ( distance <= 23 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/3/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
delay(6600);
}
else if ( distance <= 31 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/4/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
delay(7000);
}
}
@keganmiven, please edit your post, select all code and click the </> button to apply code tags; next save your post. That way we don't have to look at things like byte mac[] but it will be properly rendered as byte mac[] and indentations will be preserved. It also makes it easier to read and copy.
Hey Stefan, Sorry about the long wait. Haven't had any time to touch my arduino up till now. I just managed to do it. Thank you for your help!
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCMessage.h>
EthernetUDP Udp;
//the Arduino's IP
IPAddress ip(192, 168, 1, 21 );
//destination IP
IPAddress outIp(192, 168, 10, 118);
const unsigned int outPort = 53000;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
}; // you can find this written on the board of some Arduino Ethernets or shields
const int trig = 9;
const int echo = 8;
int duration = 0;
int distance = 0;
int cueNumber = 0;
void setup()
{
Ethernet.begin(mac, ip);
Udp.begin(53000);
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);
duration = pulseIn(echo , HIGH);
distance = (duration / 2) / 28.5 ;
Serial.println(distance);
if ( distance <= 7 )
{ if ( cueNumber != 1 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/1/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
cueNumber = 1;
delay(2600);
}
}
else if ( distance <= 15 )
{ if ( cueNumber != 2 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/2/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
cueNumber = 2;
delay(3000);
}
}
else if ( distance <= 23 )
{ if ( cueNumber != 3 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/3/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
cueNumber = 3;
delay(6600);
}
}
else if ( distance <= 31 )
{ if ( cueNumber != 4 )
{
delay(1);
//insert message here
OSCMessage msg("/cue/4/start");
msg.add((int32_t)analogRead(0));
Udp.beginPacket(outIp, outPort);
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
cueNumber = 4;
delay(7000);
}
}
else ( cueNumber = 0 );
}