EthernetBoard Freezing

Hi,

I am having a strange issue with my Ethernet board freezing. Very intermittently. What happens is:

2 LED’s (connected via a relay) and a button are connected to the board. The board receives UDP messages (a, b, c, or d) which set it into a different mode. When the board receives the message ‘b’, it goes into standby mode which flashes one LED. If the button is pressed while in standby mode, then we send a UDP message to start a computer recording. The computer then sends back the message ‘d’ which puts the arduino into recording mode. For some reason the board always seems to lock in the recording mode.

The are 4 of these arduino’s working at the same time on the same network (although independently). Each has a different IP address but the port is the same (8888). Could it be that they are all sending/receiving on the same port?

I’ve seen on the forum some info about pin 4 being for SD and at the moment am using pin 4 as an output pin, I am going to try changing that to a different pin but other than that has anyone got any other ideas?

Thanks.

#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x68, 0xF5 };
IPAddress ip(192, 168, 0, 40);
unsigned int localPort = 8888; // local port to listen on
IPAddress sendIP(192, 168, 0, 30);
unsigned int sendPort= 8888;

//buffers for receiving and sending data
//buffer to hold incoming packet,
char packetBuffer[1];

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

//The buttons need this to allow for the noise
#define BOUNCE_DURATION 2500 // THIS VALUE IS TO ENSURE THAT WE DON'T HAVE MULTIPLE CLICKS
volatile unsigned long bounceTime=0; // variable to hold ms count to debounce a pressed switch


//declare CameraMode data type
enum CameraMode {
  rehearse,
  standby,
  recording,
  testCarlSwitch
};

volatile CameraMode camMode;
volatile CameraMode prevCamMode; //used to reset camMode when testCarlSwitch is switched

int lightFlashDuration;

//relay pins
unsigned char relayPin[4] = { 4, 5, 6, 7 };

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  //Serial.begin(9600);
  //Serial.print("Hello");
  camMode = rehearse;

  for(int i = 0; i < 4; i++)
  {
    pinMode(relayPin[i],OUTPUT);
  }
  
  pinMode(8, OUTPUT); //testCarlSwitch
  
  attachInterrupt(1, buttonPress, FALLING);
  lightFlashDuration = 500; //ms that the LEDs flash on and off
}



void loop() {
  char packetBuffer[] = "";

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {

    // read the packet into packetBufffer
    Udp.read(packetBuffer,1);

    if(packetBuffer[0] == 'a')
    {
      camMode = rehearse;
    }
    else if(packetBuffer[0] == 'b')
    {
      camMode = standby;
    }
    else if (packetBuffer[0] == 'c')
    {
      prevCamMode = camMode;
      camMode = testCarlSwitch;
    }
    else if (packetBuffer[0] == 'd')
    {
      camMode = recording;
    }
  }


  if(camMode == rehearse)
  {
    for(int i = 0; i < 4; i++)
    {
      digitalWrite(relayPin[i],LOW);
    }
  }
  else if(camMode == standby)
  {
    digitalWrite(relayPin[2],LOW);
    
    digitalWrite(relayPin[3],LOW);
    delay(lightFlashDuration);
    digitalWrite(relayPin[3],HIGH);
    delay(lightFlashDuration);
  }
  else if(camMode == recording)
  {
    digitalWrite(relayPin[2],HIGH);
    digitalWrite(relayPin[3],HIGH);    
  }
  else if (camMode == testCarlSwitch)
  {
    digitalWrite(8, LOW);
    delay(100);
    digitalWrite(8, HIGH);
    delay(100);
    camMode = prevCamMode;
  }  
  
  delay(10);
}


void buttonPress()
{    
  if(abs(millis() - bounceTime) > BOUNCE_DURATION)
  {
    bounceTime = millis(); 
 
    if(camMode == standby)
    {
      Udp.beginPacket(sendIP, sendPort);
      Udp.write("startRecording");
      Udp.endPacket();
    }
    else if(camMode == recording)
    {
      Udp.beginPacket(sendIP, sendPort);
      Udp.write("stopRecording");
      Udp.endPacket();
    }
  }
}

Digital pin 4? Do you have a memory card in the microSD slot?

SurferTim: Digital pin 4? Do you have a memory card in the microSD slot?

No memory card in the slot.Does that mean the pin 4 should be set the HIGH and left?

If you do not have a memory card in the slot, you should be able to use digital pin 4 as an output. However, keep that in mind if you decide to use a microSD card later.

edit: You did change the mac addresses on each of the four devices? No duplicates allowed.

Yeah, each device has a unique mac address and ip address. I was beginning to think that it might be down to the ports being the same but if the ip's are different I can't see why that would make any difference.

Same port on different ips is fine. Otherwise, web servers would have a tough time.

I don't like this declaration. It is duplicate globally set to a size of one.

  char packetBuffer[] = "";
//  I like this better
  packetBuffer[0] = 0;

Also, I would get the whole packet. Resize packetBuffer to the max packet size tho.

    Udp.read(packetBuffer,packetSize);

Just some ideas for you.

So you think it's the code? The fact that it's so intermittent make me think it's hardware. I'll try those things anyway, thanks for your help.

I'm not convinced it is software, but I try that first.

Is the sketch stopping (fails to return from a udp function), or just not getting UDP packets?

It stops receiving UDP packets. So it should respond to messages (a, b, c, d) which do different things with the LED's at any time. When it does freeze, the LED's do not change, so I think it's just not receiving the packets. It occurred to me that it could be a network issue, but that would not explain why only 1 arduino seems to fail at any time (I'd image that all 4 would stop responding if it was a network issue).