UDP Send/Receive Data Anomalies/Inconsistent Data

I’m currently trying to send sensor data from the core to Max/MSP using UDP. I have two sensors attached to the A0 and A1 ports on the core and am receiving the data in Max (with the “sadam.udpReceiver” object which negates the typical OSC formatting requirement of the Max UDP object). The issue I’m running into is when both sensors are active the incoming data in Max doesn’t correspond with the data that should be getting sent out from each sensor.

When I only have one sensor’s data being received at a time in Max it works fine. One sensor averages around 55 and the other around 150. However, when both sensors are active there is an incoming stream of data that sits around 105 and 200 with random bursts of data coming in at 47 and 241 for no apparent reason.

// EXAMPLE USAGE

// local port to listen on
unsigned int localPort = 7400; 
int bend = 0;
int bend2 = 0;
 

IPAddress ip(192, 168, 1, 103);

// An UDP instance to let us send and receive packets over UDP
UDP Udp;

void setup() 
{
  // start the UDP
  Udp.begin(localPort);
}

void loop() 
{
    bend = analogRead(A0)/4;
    bend2 = analogRead(A1)/4;
    const uint8_t sensors[3] = {bend, bend2, 0};
    Udp.beginPacket(ip, localPort);
    Udp.write(sensors, 3);
    //Udp.write(bend2);
    Udp.endPacket();
    delay(100);
}

Could your trouble be related to the ADC? Perhaps dumping bend and bend2 out the serial port would you debug.

There is some recent data in the this thread:

that seems to show that reading two ADC pins is causing problems with the data. There were a lot of changes in the ADC handling in most recent update from :spark: and it is not completely clear right now what is going on.

1 Like

Hi @braden_s

I wrote a test program for two ADC channels and I am getting strange results. I put that discussion over in the “Odd analog readings (part 2)” thread.

1 Like

You are declaring values as int. Then when sending udp packet you are saving it as uint8_t. Is it right? Also analogRead() has size of 4096 and 4096/4 is 1024 not 256. When sending udp byte, max value is 255 (0-255 = 256 values).

I would either decrease resolution by trying analogRead(A0)/16 and add condition if (bend == 256) bend = 255. Or send one bend variable over udp as byte array. Higher level languages (Java, C#) can easily decode byte array into 32-bit unsigned integer for example.

1 Like

That should be correct. Also, thanks for pointing out the analog read size as that is definitely my bad. I’ll be changing that to /16 next time I work on it.