As the code is somehow time critical, It would be awesome to omat those delay calls.
Any ideas?
void sendMessage(char message[])
{
#ifdef DEBUG
Serial.print("Sending data: ");
Serial.write(message, 5);
Serial.println();
#endif
udp.beginPacket(gameServer, gamePort);
udp.write(message, 5);
udp.endPacket();
}
void sendButtonsChanged(int newButtonStates)
{
char message[] = {
CMD_BUTTONS_CHANGED,
char(newButtonStates & 0xFF),
char((newButtonStates ^ buttonStates) & 0xFF),
0x00,
CMD_BUTTONS_CHANGED
};
sendMessage(message);
}
void sendKeepAlive()
{
char message[] = {
CMD_KEEP_ALIVE,
char(buttonStates & 0xFF),
0x00,
0x00,
CMD_KEEP_ALIVE
};
sendMessage(message);
}
void setup()
{
#ifdef DEBUG
Serial.begin(115200);
#endif
Cytrill.begin();
setupWifi();
}
void loop()
{
static int keepAliveCounter = 0;
int newButtonStates = Cytrill.getButtons();
if (newButtonStates != buttonStates)
{
sendButtonsChanged(newButtonStates);
buttonStates = newButtonStates;
}
if (keepAliveCounter == 2000)
{
sendKeepAlive();
keepAliveCounter = 0;
//delay(20); // not helping at all
}
else
{
keepAliveCounter++;
}
delay(2); // helps
Cytrill.loop();
}
Thanks in advance,
misterdanb