I have been having difficulty in calculating
a speed of a projectile between two light-gates.
The main reason is that multiplication by high numbers
and division with doubles is not working as it should.
The relevant snippet of code is as follows:
#define F_CPU 8000000UL // 8Mhz clock #define PRESCALER 1024 #define TIMERFREQUENCY F_CPU/PRESCALER // (7812 Hz) #define GATE_SEPARATION 0.078 // in meters ........... ........... int timer0 = TCNT0; int timer1 = TCNT1; double projectileTime = timer0/TIMERFREQUENCY; // In Seconds double projectileVelocity = GATE_SEPARATION/projectileTime; // v = d/t : v = (0.078m / t-seconds) : in m*S^-1 TransmitUART(timer0); // timer0 TransmitUART(timer1); // timer1 TransmitUART(projectileTime); TransmitUART(projectileVelocity);
The result is always that the projectile velocity is 0. Or it sometimes gives me some garbage like -2 or -4.
With the UART library i'm using I have confirmed that
transmitting doubles works fine and just gets rounded of to the nearest int.
I've also tried using ints , casting to ints before the transfer etc.
Basically i've spend about 4 hours of my time just on those 4-5 lines of code yesterday and I've tried everything I could think of but it's not working.
I found that the problem is specifically at the
double projectileTime = timer0/TIMERFREQUENCY;
timer0 is in the range of single - double digit integer.
Could someone help me out please?
Probably not relevant:
Also , could someone enlighten me as to why something like this gives a false result ? :
int varr = 93; int(or double) tmp = (varr*1000)/4553; // false result int(or double) tmp = (varr*10)/4553; // Works fine when it is times 10 , but 1000 produces a false result.
Is there overflow happening somewhere?