View previous topic :: View next topic |
Author |
Message |
uni_student
Joined: 01 Aug 2007 Posts: 38 Location: AUckland, NEW ZEALAND
|
Simple division not working |
Posted: Wed May 12, 2010 3:55 am |
|
|
Hi there, getting unrealistic values for average velocity(avspd) on display. No problems getting current speed (tpspd) from gps and displaying it, but calculating average speed is not working. Am i missing something here or do i have to use some modulus method?? It is really bugging me, help please.
Code: |
//intialise
int tpspd=0;
int avcnt=0;
int cycles=1;
//while(1) loop
tpspd=atoi(temp); //temp is velocity string from gps
avcnt+= tpspd;
avspd = avcnt/cycles;
cycles++;
fprintf(LCD, "Current Speed %i", tpspd);
fprintf(LCD, "Average Speed %i", avspd);
//
|
|
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed May 12, 2010 4:19 am |
|
|
it is most likely to do with the fact all vars are 8 bits.
What are values for tpspd (typical, min and max) ?
Does it work for x loops ?
After avcnt > 255 it will go horribly wrong.
After 255 cycles it will go horribly wrong. |
|
![](templates/subSilver/images/spacer.gif) |
uni_student
Joined: 01 Aug 2007 Posts: 38 Location: AUckland, NEW ZEALAND
|
|
Posted: Wed May 12, 2010 4:46 pm |
|
|
Cheers, dude.
int16 made it work, knew it was something simple:) |
|
![](templates/subSilver/images/spacer.gif) |
|