Hope someone can help.
I’m trying to write some code that uses a Nano and a ENC28J60 shield to send data to a computer via UDP. The software works for a while, anywhere from a minute to 30 minutes before it stops, and I have to reset the nano for it to start working again. The stoppage is completely random and I can see an obvious cause. Can anyone see any issues with this code?
#include <EthernetENC.h>
//Device IP & MAC (Variable)
byte mac[] = { 0x2A, 0x01, 0x22, 0x22, 0x20, 0x20 };
IPAddress ip(192, 168, 1, 20);
char remote_IP1[] = "192.168.1.183";
int remote_Port1 = 43041;
EthernetUDP Udp; //Class variable (Initiates process of UDP)
int EB = 4;
int BS1 = 5;
int BS2 = 6;
int BS4 = 7;
void remote_send(int data)
{
Udp.beginPacket(remote_IP1, remote_Port1);
Udp.write(data);
Udp.endPacket();
delay(250);
}
void setup()
{
pinMode(EB, INPUT_PULLUP);
pinMode(BS1, INPUT_PULLUP);
pinMode(BS2, INPUT_PULLUP);
pinMode(BS4, INPUT_PULLUP);
Ethernet.begin(mac, ip); // Set up the Ethernet Shield
Udp.begin(remote_Port1); // Open a socket for this port
//Serial.begin(9600); // Set up serial monitor with PC4
delay(500);
}
void loop() {
//Serial.println("");
if (digitalRead(EB) == 0)
{
remote_send(9);
}
else
{
remote_send(8);
}
if (digitalRead(BS1) == 0 && digitalRead(BS2) == 1 && digitalRead(BS4) == 1 )
{
remote_send(0);
}
else if (digitalRead(BS1) == 0 && digitalRead(BS2) == 1 && digitalRead(BS4) == 0 )
{
remote_send(1);
}
else if (digitalRead(BS1) == 0 && digitalRead(BS2) == 0 && digitalRead(BS4) == 0 )
{
remote_send(2);
}
else if (digitalRead(BS1) == 0 && digitalRead(BS2) == 0 && digitalRead(BS4) == 1 )
{
remote_send(3);
}
else if (digitalRead(BS1) == 1 && digitalRead(BS2) == 0 && digitalRead(BS4) == 1 )
{
remote_send(4);
}
else if (digitalRead(BS1) == 1 && digitalRead(BS2) == 0 && digitalRead(BS4) == 0 )
{
remote_send(5);
}
else if (digitalRead(BS1) == 1 && digitalRead(BS2) == 1 && digitalRead(BS4) == 0 )
{
remote_send(6);
}
else if (digitalRead(BS1) == 1 && digitalRead(BS2) == 1 && digitalRead(BS4) == 1 )
{
remote_send(7);
}
}