XBee causing arduino to hang

Hi all

I have a setup on a Mega 2560 that reads 3 pot values, sending them when they exceeds a certain value. Also it's coded to send the current value every 5 seconds if there hasn't been any change in that time.

The values are sent via an XBee Pro S1 module using the Serial.print command. Everything seems to work fine apart from the arduino hangs after about 3 minutes. The reset buttom stops responding and the only way to restart it seems to be to disconnect the power and reconnect it. I've tried adding a watchdog timer to no avail, but I did read that the watchdog timer cannot work with the Mega 2560 because the timeout gets changed to 15ms when the sketch is loaded.

This problem only seems to be evident when the XBee is connected. When I take the shield off, the pot values work non stop.

Does anyone know why this might be happening, or if there's a way to reset the arduino to get around this?

I'm currently in the desert at the Burning Man festival so I have very limited resources, but I do have a spare arduino duemilianove and some FETs that I was thinking I could use to turn off the power going into the Mega at 3 minute intervals. This is a real hack solution though and I'd rather not go down that route.

Anyway, here's my code. Excuse any bad coding, there's probably much better ways of doing most of the things but it seems to work fine apart from the hanging issue.

Thanks

Laurence

int pot1pin = A8;
int pot2pin = A9;
int pot3pin = A10;

int potpos = 33;

char pot1_char;
char pot2_char;
char pot3_char;

int readpot1 = 500;
int readpot2 = 500;
int readpot3 = 500;

int newpotval1 = 0;
int newpotval2 = 0;
int newpotval3 = 0;

int hyster = 3;
int deadzone = 3;

int segment1 = 0;
int segment2 = 0;

int segment1old;
int segment2old;

int cutoff1[17]={0, 48, 72, 96, 116, 132, 157, 181, 205, 241, 275, 323, 378, 459, 565, 780, 1000};
int cutoff2[17]={0, 22, 57, 88, 110, 132, 151, 177, 204, 234, 267, 310, 355, 430, 550, 700, 1000};

long previoustimer = 0;
long sendinterval = 5000;

unsigned long currenttimer;

void setup(){
  
  Serial.begin(9600);
  Serial.println("Start");
 
  pinMode(pot1pin, INPUT);
  pinMode(pot2pin, INPUT);
  pinMode(pot3pin, INPUT);
  
  pinMode(potpos, OUTPUT);
 
  digitalWrite(potpos, HIGH);
 
}

void loop(){
  
   getpot1();
   getpot2();
   getpot3();
   intervalsend();
   delay(20);
   
}


void getpot1(){
  
  readpot1 = analogRead(pot1pin);
  
  if (readpot1 > newpotval1 + hyster || readpot1 < newpotval1 - hyster){
    newpotval1 = readpot1;
  }
  for(int i=0;i<16;i++){
  if (newpotval1 > cutoff1[i] + deadzone && newpotval1 < cutoff1[i+1] - deadzone){
    segment1 = i;
  }
  }
  if (segment1 != segment1old){
    if (segment1old == 15 && (segment1 != 14 || segment1 != 0)){
      segment1=0;
    }
    pot1_char = segment1;
    Serial.print("p");
    Serial.print("1");
    Serial.print(pot1_char);
    previoustimer=millis();
  segment1old=segment1;
  }
}

void getpot2(){
  
  readpot2 = analogRead(pot2pin);
  
  //check to see if the pots have moved at all
  if (readpot2 > newpotval2 + hyster || readpot2 < newpotval2 - hyster){ 
    newpotval2 = readpot2;
  }
  
  //if there's movement, run the pots through a lookup table to divide the analogue value into 16 equal segments
  for(int i=0;i<16;i++){
  if (newpotval2 > cutoff2[i] + deadzone && newpotval2 < cutoff2[i+1] - deadzone){
    segment2 = i;
  }
  }
  
  //if there was a change, do a quick cleanup then send it over
  if (segment2 != segment2old){
    
   //try and limit the amount of jumping around at the deadspot
    if (segment2old == 15 && (segment2 != 14 || segment2 != 0)){
      segment2=0;
    }
    pot2_char = segment2;
    Serial.print("p");
    Serial.print("2");
    Serial.print(pot2_char);
    previoustimer=millis();
  segment2old=segment2;
  }
}

void getpot3(){
  
  readpot3 = analogRead(pot3pin);
  
  if (readpot3 > newpotval3 + hyster*2 || readpot3 < newpotval3 - hyster*2){
    newpotval3 = readpot3;
    pot3_char = newpotval3/4; //divide the integer by 4 so that the ascii is happy
    Serial.print("p");
    Serial.print("3");
    Serial.print(pot3_char);
    previoustimer=millis();
  }
}
    
    
    void intervalsend(){
         
    currenttimer = millis();
    
    if (currenttimer-previoustimer>sendinterval){
      
      previoustimer=currenttimer;
     
      Serial.print("p");
      Serial.print("1");
      Serial.print(pot1_char);
    
      Serial.print("p");
      Serial.print("2");
      Serial.print(pot2_char);
    
      Serial.print("p");
      Serial.print("3");
      Serial.print(pot3_char);
      
    }
      
    }
  pinMode(pot1pin, INPUT);
  pinMode(pot2pin, INPUT);
  pinMode(pot3pin, INPUT);

pinMode does nothing to analog pins. The are input only, so there is no reason to set the mode.

Everything seems to work fine apart from the arduino hangs after about 3 minutes.

How do you know it hangs? The only evidence that I see is that the other end stops receiving serial data, but there could be any number of reasons for that.

Hi PaulS

Thanks for the reply, and thanks for telling me about the analog pinMode, I didn't realise that. But I didn't think they were input only, I have used them as digital outputs before.

You may be right about the arduino not hanging but I'm not sure how to test it. The two symptoms I have are that I stop receiving data, both at the receiving XBee and through the serial port on the PC, and the on board reset button stops responding.

I'm working on having a seperate watchdog board to do the same as the watchdog timer, but it'd be much simpler to fix this XBee problem.

Thanks very much

Laurence

But I didn't think they were input only, I have used them as digital outputs before.

The analog pins can be used as digital pins, but, then they have different numbers. The analog pins, used as analog pins, are input only.

The two symptoms I have are that I stop receiving data, both at the receiving XBee and through the serial port on the PC, and the on board reset button stops responding.

I'm curious how you know that the reset button stops responding. There are generally no visual indications on the Arduino that it is being reset. That it does not restore serial communications says more about the receiving end, I think, not working any more.

Are you using an XBee shield on the Arduino? If so, which one?

Does the TX light on the Arduino quit flashing? Or, is it strictly the receiving end that quits?

Hi PaulS

Thanks again for the reply.

That's helpful what you said about the analog pins being only inputs if you define them as analog pins. I was wondering just earlier why it stopped letting me define an integer for a pin, A8. I changed it to 61 easily enough but I hadn't realised that calling it A8 is implicitly defining it as an input. Thanks for that.

As for the reset, I'm not sure why it's toggled on, but the LED connected to pin13 is normally on, apart from at a reset, when I guess all outputs are held low during startup. I haven't coded that LED to be on, but it is and it has allowed me to see whether or not I can reset the arduino.

I am using the libellium XBee shield. The only thing I can think of is something in the XBee settings that is causing something strange to happen.

Anyway, I've curmudgeonly got around the problem by using another arduino next to it as an external watchdog. I've got my XBee sender board outputting a pulse at about 5Hz, then the WD board listens for it and if it stops, it switches the power to the XBee board via a pnp transistor driven with a npn. This works around the problem pretty well.

The project is a control panel with 3 pots on the ground, then a lighting setup in a big balloon up in the air. Currently with the WD board fix, the only problem is that during a reset operation, there is about 3 seconds where no data is transmitted, but the lighting board just updates via the XBee, so visually this is not too much of a problem. There is very little data transmitted, maybe a maximum of 8 bytes a second, but only when someone is turning a knob. It's not ideal but it's the best we can do while we're in the desert. There's some info on the project at http://blog.luneproject.com/lighting-electronics-design-and-testing/ although that design is slightly out of date. I changed the multiplexing setup in favour of 3 TLC5940s, with a slightly complex system of a decade counter feeding into an AND gate as a way of multiplexing via hardware for the sake of limiting current.

When I get back to the real world, I'll try and get the board working perfectly.

Thanks loads

Laurence

Try changing your delay up a bit from 20 to 200 and see if there are any changes. I had a weird problem where my changing an analog signal would delay for over 13 seconds until i got a larger delay in loop.