I'm still learning C to be used in microprocessors. In the beginning I used lots of globals. Now I'm trying to avoid it as much as a can, but for me it's not always clear to see how to do this.
For example a battery monitor, in this case there are 4 functions that need to read or modify a variable. I have these functions all using the variable LowVoltage.
void Check_Voltage(){
checks current voltage against LowVoltage
}
void Menu_Voltage(){
a menu on the LCD screen to set the value of LowVoltage
}
void Save_LowVoltage(){
runs after the settings menu is finished to save LowVoltage to EEPROM
}
void Load_LowVoltage(){
reads EEPROM and sets LowVoltage at startup
}
- Check_Voltage() and Save_LowVoltage() need to read LowVoltage.
- Load_LowVoltage() need to write LowVoltage.
- Menu_Voltage() needs to read and write LowVoltage.
How can I make this work without making LowVoltage global?? Would I need to make another function to read or write LowVoltage? Something like this:
unsigned int Low_Voltage(short Get, unsigned int Value){
static unsigned int LowVoltage;
if(Get) return LowVoltage;
else LowVoltage= Value;
}
Or are there better ways to do this? I guess there must be :) I've been reading about structures lately, but to be honest I don't fully understand them and I'm not even sure it would help me in cases like this?