UDP Message getting in the way of other functions

Hello All

I'm working on a project that has an encoder connected to an Uno on pins 18 and 19.

The Uno reads the encoder value and then prints it to a 16x2 LCD screen as well as sending that same value over UDP via an Ethernet Shield to a specific IP address.

The problem I'm having is that if the uno boots without being connected to an external machine with the correct IP address then it doesn't record the encoder count correctly. The count increments/decrements very slowly. Almost like it's getting stuck.

What's interesting is if it boots with the network connection in place and then I remove the connection it continue to run perfectly. I assume this means it's something in my setup code that's the problem but I can't figure out where the issue lies.

Code is below. Any help is most welcome.


```cpp
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCMessage.h>
#include <LiquidCrystal.h>
#include <Encoder.h>

EthernetUDP Udp;

//the Arduino's IP
IPAddress ip(192, 168, 1, 177);
//destination IP
IPAddress outIp(192, 168, 1, 101);
const unsigned int outPort = 8000;

const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Encoder myEnc(18, 19);

long oldPosition = -999;
long posSend = 0;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  Serial.begin(9600);
  lcd.setCursor(0, 0);
  lcd.print("Encoder Pos:");

  byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  };  // you can find this written on the board of some Arduino Ethernets or shields

  Ethernet.begin(mac, ip);
  Udp.begin(8001);
}

void loop() {
  long newPosition = myEnc.read() / 5.771;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    lcd.setCursor(0, 1);
    lcd.print(newPosition);


    posSend = newPosition;
    //the message wants an OSC address as first argument
    String path = "/eos/sc/encoder/pos/" + String(posSend);
    OSCMessage msg(path.c_str());
    //msg.add(posSend);
    //msg.add("#");

    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
    Serial.println(newPosition);
  }

  if (newPosition < 100 && newPosition > 9) {
    lcd.setCursor(2, 1);
    lcd.print("        ");
  }
  if (newPosition < 10 && newPosition > -1) {
    lcd.setCursor(1, 1);
    lcd.print("         ");
  }
  if (newPosition < 1000 && newPosition > 99) {
    lcd.setCursor(3, 1);
    lcd.print("          ");
  }
  if (newPosition < 10000 && newPosition > 999) {
    lcd.setCursor(4, 1);
    lcd.print("          ");
  }
  if (newPosition < 0 && newPosition > -10) {
    lcd.setCursor(2, 1);
    lcd.print("           ");
  }
  if (newPosition < -9 && newPosition > -100) {
    lcd.setCursor(3, 1);
    lcd.print("           ");
  }
  if (newPosition < -99 && newPosition > -1000) {
    lcd.setCursor(4, 1);
    lcd.print("           ");
  }
  if (newPosition < -999 && newPosition > -10000) {
    lcd.setCursor(5, 1);
    lcd.print("            ");
  }
}

Start by making the encoder work.
Then check up the network parts.
On UNOs we usually use the pin names like D0 to D13, A0 to A5. No idea which ones You use. No UNO in the pocket...
I'm not sure what You say when writing 'booting". Is it applying power, pressing the reset button or what?

My hunch is there is some init in Ethernet.begin() which relies on a network present. It could be looking for a gateway address for example.

I suspect when you call the UDP Send, it is blocking, then timing out. I am digging into the Ethernet library to see how to avoid that.

ETA this thread seems to be relevant UDP delays my Sketch by 2 seconds if there is a network problem. SOLVED - #6 by darrylp

The Encoder does work. As I said everything works perfectly as long as there's a valid network connection.

Pins are A4 and A5

I suspected it might be in the Ethernet.begin(). It's just a bit beyond my knowledge to figure what exactly...

Does the udp resp. Eathernet.begin return any staus? If so, use that status to control the next action.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.