Arduino reboots once in loop

Hi Guys,

I have a program which estimates battery capacity, range as well as runs different control loops on an arduino MEGA2560 to control an eBike. I was using floating points which gave me quite a slow sampling frequency (10Hz). I now changed this to use integers by multiplying everything by at least 1000 so I can still keep a 3 decimal accuracy. However, as soon as the Arduino leaves setup and starts running loop, it crashes and restarts. What could this be due to and how could I solve it?

My code is attached to this post

post.ino (14.8 KB)

long batteryspec[6][56]={{4200,4000,3940,3900,3876,3860,3852,3844,3826,3809,3795,3783,3765,3744,3728,3710,3692,3672,3650,3631,3613,3591,3573,3556,3532,3508,3488,3463,3439,3417,3399,3380,3366,3352,3332,3312,3291,3273,3253,3237,3217,3196,3176,3150,3123,3087,3051,2994,2929,2868,2796,2727,2650,5261,2502},//5A discharge 18650GA SPEC

None of the values needs to be stored in a long. Why are they all using twice as much memory as needed?

You should add some Serial.print() statements to loop(), to find out where it crashes.

You should also use Tools + Auto Format, to fix your crappy indenting.

I get a ton of warnings when I compile your code, quite a few of them bad that can cause horrible bugs:

C:\Users\XXX\Downloads\post\post.ino: In function 'long int pidcurrent_control()':

C:\Users\XXX\Downloads\post\post.ino:85:14: warning: overflow in implicit constant conversion [-Woverflow]

      despower=2000000;

              ^

C:\Users\XXX\Downloads\post\post.ino:97:12: warning: overflow in implicit constant conversion [-Woverflow]

    despower=550000;

            ^

C:\Users\XXX\Downloads\post\post.ino: In function 'void cyclescaling()':

C:\Users\XXX\Downloads\post\post.ino:111:13: warning: comparison is always true due to limited range of data type [-Wtype-limits]

  if (ncycles<=499000) {

             ^

C:\Users\XXX\Downloads\post\post.ino: In function 'void throttlesmoothing()':

C:\Users\XXX\Downloads\post\post.ino:139:11: warning: variable 'dtsm' set but not used [-Wunused-but-set-variable]

  int i=0, dtsm;

           ^

C:\Users\XXX\Downloads\post\post.ino: In function 'void autonomy()':

C:\Users\XXX\Downloads\post\post.ino:247:10: warning: return-statement with a value, in function returning 'void' [-fpermissive]

   return 0;

          ^

C:\Users\XXX\Downloads\post\post.ino: In function 'long int pidcurrent_control()':

C:\Users\XXX\Downloads\post\post.ino:107:5: warning: control reaches end of non-void function [-Wreturn-type]

     }

     ^

C:\Users\XXX\Downloads\post\post.ino: In function 'pidspeed_control()':

C:\Users\XXX\Downloads\post\post.ino:124:6: warning: 'isp' is used uninitialized in this function [-Wuninitialized]

 isp++;

      ^

What makes you think it is restarting? Put a Serial.println(F("Beginning program.")); just after begin() and open the Serial monitor. If you don’t see that line more than once, it’s not actually resetting. Something else is going on.

Thank you for all your help! I used Serial.print and just found that it was crashing due to the use of the command myGLCD.printNumI() instead of printNumF(). However when now checking my sampling rate it is worst that it used to be. The loop takes 119ms to run when it used to be 100ms when I was using floating point... Is it because as you said my long variables are using too much more memory?

Is it because as you said my long variables are using too much more memory?

Long arithmetic takes longer that integer arithmetic, but less than floating point arithmetic. So, no, using more memory than you need to isn't the cause of the slowdown.