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(" ");
}
}