# Arduino device (PanStamp) hangs when I change the data that is sent through serial

I have a PanStamp (arduino compatible) and everything was working fine until I change the amount of data I'm sending through serial.

send_altitude is called every second using timer interruption (TimerOne library).

This is the original function that sends data (and does not hang):

void send_altitude()
{
cli();

float_byte altitude;
byte buffer[6];

// Do one trash read to avoid fluctuations
delay(5);
altitude.asFloat = getAltitude(ALT_SENSOR);

buffer[0] = 0x05; /* Size */
buffer[1] = HUB_DATA;
buffer[2] = altitude.asByte[0];
buffer[3] = altitude.asByte[1];
buffer[4] = altitude.asByte[2];
buffer[5] = altitude.asByte[3];

send_through_serial(6, buffer);

sei();
}


But when I changed it to this one (basically I'm now sending a packet count): Following PeterJ suggestion I moved the buffer and variable outside the function and also removed the sei()/cli(), however it didn't work... One thing that I noticed is that it looks to take longer to hang without sei/cli.

float_byte altitude;
unsigned char buffer[10];
void send_altitude()
{
// Increments the counter;
packet_count.asULong = packet_count.asULong + 1;

// Do one trash read to avoid fluctuations
delay(5);
altitude.asFloat = getAltitude(ALT_SENSOR);

buffer[0] = 0x09; /* Size */
buffer[1] = HUB_DATA;
buffer[2] = altitude.asByte[0];
buffer[3] = altitude.asByte[1];
buffer[4] = altitude.asByte[2];
buffer[5] = altitude.asByte[3];

buffer[6] = packet_count.asByte[0];
buffer[7] = packet_count.asByte[1];
buffer[8] = packet_count.asByte[2];
buffer[9] = packet_count.asByte[3];

send_through_serial(10, buffer);
}


The PanStamp started to hang without reason.... packet_count is an ulong_byte.

The others functions and data structs:

void send_through_serial(int lenght, unsigned char *data)
{
unsigned int i;

/* Send srync word */
Serial.write(0xBA);
Serial.write(0xBA);

/* Send data */
for(i = 0; i < lenght; i++)
{
Serial.write(data[i]);
}
}

/**
* @brief getAltitude
*
* Return the altitude (Voltage) of a
* analogue pressure sensor.
*
* Maybe later on it will return the
* altitude
*
* @param ain Analogue pin
* @return Altitude (Voltage)
*/
inline float getAltitude(int ain)
{
}

typedef union _float_byte
{
byte asByte[4];
float asFloat;
} float_byte;

typedef union _ulong_byte
{
unsigned long asULong;
byte asByte[4];
} ulong_byte;


Timer initialization code:

Timer1.initialize(1000000);
Timer1.attachInterrupt(send_altitude);


The only "solution" I found is use the WDT to reset after a hang, but it's not good because I have some counters that can not be rested.

Thanks to @JRobert it is working now!! I also made a few more changes:

• Timer interrupt only setting a boolean flag;
• Buffers inside the function;
• Using sei/cli;
• getAltitude() is not inline any more.
• I can't see any problem above but how do you call send_altitude? Just thinking you may have a problem elsewhere and the additional delay of sending a few extra characters causes a problem. Maybe try doubling the baud rate if possible and see if that makes a difference. – PeterJ Aug 2 '13 at 10:12
• I'm calling it every second using the TimerOne library (playground.arduino.cc/code/timer1). So I don't think that the problem is with the timing... I'll add this part of the code to the post – Tiago Queiroz Aug 2 '13 at 10:45
• Try removing the cli / sei from your routine. Another thing to try is moving the buffer outside send_altitude so it's not on the stack. The extra space and the possibility of ending up with another nested interrupt being called might be overflowing the stack. – PeterJ Aug 2 '13 at 11:22
• PeterJ, it didn't work... I tried with and without the sei/cli.... – Tiago Queiroz Aug 2 '13 at 14:47
• Good work. I'm glad you fixed it. – JRobert Aug 6 '13 at 1:06