Log in

View Full Version : Simple problem with variables


sear_yoda
04-02-2004, 14:55
I'm trying not to be a total idiot here by checking all over the forums first, and reading all of the documentation, but I CANNOT figure out how to initilize a simple integer variable 'pressure' that equals the value from the pressure reader on analog input 1.

static unsigned int pressure = Get_Analog_Value(rc_ana_in01);

No matter where I put that, I can't get it to compile. It will compile fine in a few certain places in user_routines.c if it's just pressure = 0, but not this line. EVen if I split it up, it still doesn't work. All I get is syntax errors.

Does anyone else wish this compiler was a tiny bit more descriptive in its error debugging? I've been learning Java this year using the NetBeans IDE, and this MPLAB IDE makes me want to hurl my laptop across the room.

All of my coding depends on being able to initialize variables (well, duh) so this is really a stopping point for me. My entire team is getting sort of pissed off, becuase I just sit there looking angry every meeting trying to get the code to work.

Thanks in advance for any replies I recieve...I must be doing some simple thing wrong.


-Chris

Ryan M.
04-02-2004, 15:00
static unsigned int pressure = Get_Analog_Value(rc_ana_in01);
If this is a global variable, you can't use a function to initialize it at its declaration. To initialize it with a function, put something like this:
pressure = Get_Analog_Value(rc_ana_in01);
in user_initialazation(). (in the file user_routines.c)

Hope that helps.

Random Dude
04-02-2004, 17:07
I'm trying not to be a total idiot here by checking all over the forums first, and reading all of the documentation, but I CANNOT figure out how to initilize a simple integer variable 'pressure' that equals the value from the pressure reader on analog input 1.

static unsigned int pressure = Get_Analog_Value(rc_ana_in01);

No matter where I put that, I can't get it to compile. It will compile fine in a few certain places in user_routines.c if it's just pressure = 0, but not this line. EVen if I split it up, it still doesn't work. All I get is syntax errors.

Does anyone else wish this compiler was a tiny bit more descriptive in its error debugging? I've been learning Java this year using the NetBeans IDE, and this MPLAB IDE makes me want to hurl my laptop across the room.

All of my coding depends on being able to initialize variables (well, duh) so this is really a stopping point for me. My entire team is getting sort of pissed off, becuase I just sit there looking angry every meeting trying to get the code to work.

Thanks in advance for any replies I recieve...I must be doing some simple thing wrong.


-Chris

What I supect your problem might be is that C, unlike Java or C++ is requires that all variable initializations be done at the very top of the function. You cannot have any code except for other variable initializtion before it. So, I would suggest sticking

static unsigned int pressure;

at the top of the function and then

pressure = Get_Analog_Value(rc_ana_in01);

where ever it is more appropiate.

If this isn't the problem, could you post the message and some code snippets?

sear_yoda
05-02-2004, 08:47
Well either one, or the combination of both, fixed the problem. I'm not quite sure what I ended up doing, so I'll grab some code samples when I get home and explain how I fixed it.

Thanks for the help, now I can finally get started on my code!

KenWittlief
05-02-2004, 09:12
Initializing a variable is something you normally do with a predetermined value at powerup - like setting it to zero, or 127, or 255

I dont think that going out and reading an input register would technically be considered initializing it - because to get that analog input register value, the code has to be running

maybe you are confused a little between initializing it (at powerup) and reading the input for the first time when your code runs?

If you want to read the initial value of the pressure sensor when the robot is first turned on, you should have a separate variable for that, and some way to make sure you only read the sensor input once after the code first turns on.

for example, initialize the variable to a value that it could not possible get from the sensor at power on, like maybe 254

then somewhere in your code have something like

if initial_pressure = 254 then initial_presssure = pressure_sensor