SV328 get_ticks() returning negative number

Hello,

I am running a orangutan sv328 and using atmel studio 6.1 I am trying to run a continuous while loop in order to run a small wheeled robot using all pololu gear. I have included sample time in my control system implementation and have found that new ticks in the code below will randomly return a negative number. The number is counting up, however, randomly the number will go negative, as it counts the number gets smaller indicating that it is still counting up.

Can anyone help me with why it is turning negative. My code is over 300 lines long so don’t really want to copy the whole thing here.

last_ticks = new_ticks;
new_ticks = get_ticks();
sample_time = (ticks_to_microseconds(new_ticks - last_ticks))/1000;//time in milliseconds

Thanks in advance!

Check for incorrect declaration of integer variable “signedness”.

Thanks for that Jim, I changed the initialisation from int to unsigned int and it seems to have sorted itself.

Thanks heaps!!