I am using a keypad to choose between different modes in my program, one mode is to read data using UDP from a computer which is continuously sending me time values. The issue I am having is that it seems to be buffering my data if when I am not in that mode.
For example, if I go into this mode and receive:
1:55:47.5
1:55:47.6
1:55:47.7
1:55:47.8
1:55:47.9
1:55:48.0
Then click the button to change modes, play around in another mode, then eventually switch back to this time mode, it will start counting like this:
1:55:48.1
1:55:48.2
1:55:48.3
1:55:48.4
1:55:48.5
1:55:48.6
That is sequentially in order... but since i spent time in another mode, the time that is being read in is not actually what is being sent to me anymore, that was sent maybe a few minutes ago.
So it seems like this data is being buffered somehow.
I have tried clearing the packetBuffer but all this does is clear out just one of the time values, not the entire "buffer".
Since I am using UDP I was expecting that if I wasn't actively listening/capturing the packets that they would just be ignored, but that does not seem to be the case. They are being captured somehow.
Alternatively, if I change my delay(100) to delay(2000), it will only print out one time value every two seconds as expected. The issue is that is still prints out the time in tenths of a second like shown above. With the two second delay this quickly gets me way out of sync with the computer sending me the time. I would expect to only print out the accurate time every two seconds and ignore all values in between.
Maybe I am completely wrong about how UDP works and maybe this is impossible to do, but I hope there is a way to solve my problem.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Keypad.h>
#include <FlexiTimer2.h>
String mode = "";
char keys[4][4] =
{
{'0','1','2','3'},
{'4','5','6','7'},
{'8','9','A','B'},
{'C','D','E','F'},
};
byte rowPins[4] = {2,3,4,5};
byte colPins[4] = {6,7,8,9};
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, 4, 4);
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(172, 16, 0, 72);
unsigned int localPort = 8888;
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char myString[6] = {' ',' ',' ',' ',' ',' '};
void setup()
{
Ethernet.begin(mac,ip);
Udp.begin(localPort);
FlexiTimer2::set(100, checkKey);
FlexiTimer2::start();
Serial.begin(9600);
}
void checkKey()
{
char key = kpd.getKey();
if(key)
{
if(key == 'C') // C is the SELECT MODE button
{
mode = "";
}
if(mode == "lynx")
{
}
else // select mode
{
if(key == '0')
{
mode = "lynx";
}
}
}
}
void loop()
{
Serial.println(packetBuffer);
if(mode == "lynx")
{
int packetSize = Udp.parsePacket();
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
//Serial.println(packetBuffer);
delay(100);
}
else // select mode
{
delay(1000);
}
}