I've got this code divided in two tabs.
TAB 1
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCBundle.h>
EthernetUDP Udp;
OSCBundle usend;
const unsigned int udpinPort = 9000;
const unsigned int udpoutPort = 10000;
//the Arduino's IP
IPAddress ip(192, 168, 1, 220);
IPAddress outip(192, 168, 1, 72);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // you can find this written on the board of some Arduino Ethernets or shields
void setup()
{
Serial.begin(115200);
i2c_setup();
setup_sonar();
Ethernet.begin(mac,ip);
Udp.begin(udpinPort);
}
void loop()
{
usrange();
sendBundle();
}
/*Send out our bundle */
void sendBundle()
{
/* Client's IP is automatically resolved with remote ip function */
if(usend.size())
{
Udp.beginPacket(outip, udpoutPort);
usend.send(Udp);
Udp.endPacket();
}
}
TAB 2
#include <NewPing.h>
#define SONAR_NUM 3 // Number or sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
int ind;
int threshold=15;
int previouson[SONAR_NUM]; //check the previous status
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(22, 23, MAX_DISTANCE),
NewPing(24, 25, MAX_DISTANCE),
NewPing(26, 27, MAX_DISTANCE),
};
void setup_sonar(void)
{
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t ind = 1; ind < SONAR_NUM; ind++) // Set the starting time for each sensor.
pingTimer[ind] = pingTimer[ind - 1] + PING_INTERVAL;
//initialize zero the array o previous stuff
for(ind=0;ind<SONAR_NUM;ind++)
{
previouson[ind]=0;
}
}
void usrange ()
{
for (uint8_t ind = 0; ind < SONAR_NUM; ind++) { // Loop through all the sensors.
if (millis() >= pingTimer[ind]) { // Is it this sensor's time to ping?
pingTimer[ind] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor will be pinged.
if (ind == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop(); // Make sure previous timer is canceled before starting a new ping (insurance).
currentSensor = ind; // Sensor being accessed.
cm[currentSensor] = 0; // Make distance zero in case there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
}
}
// The rest of your code would go here.
}
void echoCheck() { // If ping received, set the sensor distance to array.
if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle()
{ // Sensor ping cycle complete, send over osc.
for (ind = 0; ind < SONAR_NUM; ind++)
{
/*send only if included in the range*/
if((cm[ind]>2) && (cm[ind]<15))
{
usend.add("/sonar").add((int32_t)ind).add((int32_t)cm[ind]);
}
}
}
It should send out three OSC messages containing the sensor number and its distance value
like: /sonar 0 23
/sonar 1 120
/sonar 2 50
etc..
I check the incoming OSC bundle messages with Reaktor.
The code works but it hangs after 4 seconds and I need to reset the arduino to make it send again.
I'm using an arduino mega 2560 with Ethernet shield Rev 3