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);
}
}