We wrote some code to do some iterative functions. They did not work as expected so I started breaking it down. It got so simple that I finally realized what was causing the problem - simple math.
Here is our simple code
StartTimer(1);
while (1);
LeftArmTimer=GetTimer(1);
PrintToScreen(“Main Loop %d\n” , LeftArmTimer);
TempA=LeftArmTimer / 350 ; // TempA is declared as int
PrintToScreen(“TempA%d” , TempA);
TempB=TempA/2;
PrintToScreen(“TempB%d” ,TempB);
wait(5);
Foster, this is EasyC, not Robotc.
Pinion, pasting code is better than handtyping misleading mistakes.
The following code compiles in EasyC as a user C code function, but I don’t have a cortex handy to run it to check output.
At least it has all the vars declared locally so we can see their types.
Note that GetTimer() returns unsigned long (32 bit pos = 4billion),
your application can likely define LAT as int, which is max 2billion,
so that all the variables are the same type.
PrintToScreen uses C printf descriptors, so fixing position count, and prepending 0 to get leading zeros should make columns line up better.
See also help page, search PrintToScreen , selection Marker
so it might help to print unsigned long in their correct marker %lu,
etc
Welcome to the strict typecasting world of C
#include "Main.h"
void timerloop ( void ) {
unsigned long LAT; int TempA; int TempB;
StartTimer ( 1 ) ;
while (1){ // repeat forever
LAT=GetTimer(1); // LAT is ulong, same as GetTimer
TempA=LAT / 350 ; // TempA is declared as int, want 0..=>0, 350..=>1
TempB=TempA/2; //TempB is int : want 0.. =>0, 2.. =>1
PrintToScreen("\n LAT:%012lu A:%04d B: %04d" , LAT,TempA,TempB);
Wait ( 5 ) ; // wait N milliseconds, maybe 50 would be better than 5, since print takes a while...
} // wend
} //timerloop
Edit: I agree with stack overflow link. you didn’t comment if TempB was int, or float, or long, or what type
Looks like you are using a double and printing the hex (well decimal) representation of it. 1072693248 is hex 0x3FF00000, if this were the upper 32 bits of a 64 bit double (0x3ff0000000000000), it would have been 1.0. (ie. 1.0 in it’s 8 byte double internal format is 0x3ff0000000000000).
So, you calculated 2/2 which is 1.0
You need to use %lf as the format string for double numbers.
As a mentor, every moment should be a teaching moment.
We started out talking about doing something on a repitive basis, like every 1/2 second. For example, turn on a motor for 1/2 second in one direction, stop, run the motor in the opposite direction for 1/2 second and then repeat.
So, I started talking about a counter, then using the counter to determine whether it was odd or even. This led to a formula for comparison where the number would be divided by 2 and compared to the int(of the number divided by 2).
Using the crazy math functions in Easy C led to interesting case for the variables and I suspect run time errors which are not reported back. Stupidly we displayed the values and jumped to conclusions without looking at the %d vs %f in the display.
SO, one last question if you all know…
When we enter a line of C code in easy C will it properly interpret the line or do we need to use the math functions built into the system?
Not sure what you mean by “math functions built into the system”. EasyC is just using the math functions in the standard library that comes with gcc, same as PROS and ConVEX. EasyC uses the CodeSourcery release of gcc, they should be correct.
I don’t have a working EasyC license in front of me to check, but I believe that if you want to type your own code (for example a math equation), you need to use the “user code” option from the menu.