Must be getting rusty with C
Beavis3215
Posts: 229
in Propeller 1
#include "simpletools.h"
#include "adcDCpropab.h"
int main() // Main function
{
adc_init(21, 20, 19, 18);
set_directions(15, 0, 0b1111111111111111); // pins 15 through 0 are outputs
int d[] = {0b1110111, 0b1000100, 0b1101011, 0b1101110, 0b1011100, //digits 0 through 9
0b0111110, 0b0011111, 0b1100100, 0b1111111, 0b1111100};
while(1)
{
for (int sample = 0; sample <= 1000; sample++)
{
float i = adc_volts(2);
float j = adc_volts(2);
if (j > i)
{
i = j;
}
}
int a = (int)i; // intger part
float b = (10 * (i - a));
int c = (int)b; // frac part
//print("%f, %d, %d\n", i, a, c);
set_outputs(15, 9, d[c]); // c is the decimal value
set_output(1,1); // pin 1 is the decimal digit driver
pause(8);
set_output(1,0);
set_outputs(15, 9, d[a]); // a is the integer value
set_output(2,1); // pin 2 is the integer digit driver
pause(8);
set_output(2,0);
}
}
I Cant figure out why this won't compile. When attempting, I becomes undeclared after I = j;
any Ideas?

Comments
Solution: move "float i;" above the for-loop (right underneath the opening brace for the while-loop).
int main(I)
This would be passing an argument to main and would have local scope to the main function and not global to the entire file and other functions in the file. But, would it compile as is?
To make it global to the entire file and any functions in the file, you would declare it above the main under the includes. To ensure the compiler does not optimize the value out, you could add volatile to the variable.
int fully_global; /* Visible to the whole program, any source file */ static int locally_global; /* Visible to everything inside the same source file */ void my_function(void) { int only_this_function; /* Only visible inside my_function */ } int main (int argc, char *argv[]) { int visible; /* Visible everywhere in the main() function */ while (something()) { int loop_var; /* Visible inside the while() loop only */ for (int k = 0; k < 10; k++) /* k only exists inside this for loop */ { ; } { int very_local; /* Creating a block scope purely to isolate very_local from the rest of main() */ very_local = 0; } } from_here = 0; /* This will fail */ int from_here; /* Only visible in main() from here and below, not above */ from_here = 0; /* This will work */ return (0); }That speeds up the code, especially on a ARM as it's a load-and-store type mcu.
Registers in C are temporary and will be trashed on exit of function.