Unusual reboot in serial receive loop

Hi everyone, I’m having an unusual issue on an ATTiny85 I am programming with the Arduino IDE.

I am trying to receive a serial stream from an APM flight controller using the Mavlink protocol and I’ve got everything I need to working including debug via an FTDI but I have noticed that the Attiny seems to be periodically rebooting sometimes a few times a second.

I have confirmed the reboot with a loop counter on the main loop that gets transmitted each time and despite it being a byte size variable it resets on reaching only about 10-20 (so not an overflow). Adding a startup debug string to the setup routine also shows a reboot every second or so.

I’m a bit stumped as to why this is happening - I can’t see any major faults in the code but I am hoping someone here can shed some light on the situation. I am very confident it is a software issue as if I run a program that does nothing but increment a uint32_t and debug it runs continuously as long as I have watched it which tells me the hardware is staying online fine.

Have attached the code in question - thanks in advance to anyone who can help out.

Mavlink_Attiny85_Native.ino (3.08 KB)

Could it be this buffer overflow?

 for (byte i = 1; i <= 8; i++)

and in that loop:

RC_In[i] = RC_Val;

But RC_In doesn’t have an element 8, only 0 through 7 are legal.

uint16_t RC_In[8];

You’re also writing off the end of the Servo_Out array in the same loop.

Ah wow ok now I feel a bit silly. Os course declaring an array length includes the variable[0] in the count!

Thanks so much for the quick response that looks like it has nailed it! No sign of reboot when I amend both arrays to be length of 9 to allow a variable[8] position.