Sixteen times per second I call my function Mastercontroller().
Within that function, I want to manage with the buttons LEFT, UP, DOWN and RIGHT a menu structure (I use intMenu, intMenu1, intMenu2 and intMen3 with the library Clickbutton.h ).
Question: How can I hold the values of the variables when I leave and return to the function Mastercontroller?
int intMenu=0, intMenu0=1, intMenu1=1, intMenu2=1, intMenu3=1;
//=======================================================================================================
void setup()
{
Serial.begin(9600); // open the serial port at 9600 bps:
lcd.begin(); // set the LCD address to 0x20 for a 16 chars and 2 line display
//Prepare button functions
for (int i=0; i<buttons; i++)
{
button[i].debounceTime = 20; // Debounce timer in ms
button[i].multiclickTime = 250; // Time limit for multi clicks
button[i].longClickTime = 1000; // Time until long clicks register
}
//Initialize Timer1, preset to 16 Hz timetick
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
OCR1A = 15624; // set compare match register to desired timer count:
TCCR1B |= (1 << WGM12); // turn on CTC mode:
TCCR1B |= (1 << CS10); // Set CS10 and CS11 bits for 64 prescaler:
TCCR1B |= (1 << CS11);
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt:
//=======================================================================================================
void loop(){
if (cnt1!=cnt1_previous){ //updates 16 times per second the mastercontroller; if not: the Mastercontroller is called too frequently
cnt1_previous = cnt1;
Mastercontroller();
} //End if
} //End loop
//=======================================================================================================
//Mastercontrol function
void Mastercontroller() {
//Update button control, request pushbutton position
button[0].Update(); button[1].Update(); button[2].Update(); button[3].Update();
//1. Check to switch to mainmenu (No setting or Logging active) =========================
if (intMenu==0) {
//Serial.println("Backtoets"); blnSetting=false;
//boolean blnUp, blnDown, blnRight;
strLine1 = "SUNLOGGER v16";
strLine2 = "1. Logging";
strLine3 = "2. Setting menu";
strLine4 = "";
lcd.setCursor(0,intMenu0 ); lcd.blink();
if (blnUpdateDisplay==true) {
Update_display();
blnUpdateDisplay= false;
} //end if
//if previous state changed, move cursor, update display
if (button[1].clicks== 1 || button[2].clicks== 1) {
if (intMenu0==1) {intMenu0=2;} else {intMenu0=1;}
//strLine4= String(intMenu0);
lcd.setCursor(0,intMenu0-1);
blnUpdateDisplay = true; //Display wil be updated next turn
} //End if, button Up or Down pressed
//Switch to logging or settingmenu (pushbutton Right)
if (button[3].clicks==1 && intMenu0==1) {intMenu=10; Serial.println("Logging geactiveerd"); blnUpdateDisplay=true; }
if (button[3].clicks==1 && intMenu0==2) {intMenu=20; Serial.println("Setting geactiveerd"); blnUpdateDisplay=true; }
} //End 1.Mainmenu control (No setting or Logging)
//2. Check to switch in Loggingmenu ==========================
//Toggle between first and last measurements in Loggingmenu
if (intMenu==10){
//Action for logging
} //End 2.Logging menu
//3. Check to switch in Settingmenu ==========================
//Toggle between first and last measurements in Loggingmenu
if (intMenu==20){
if (button[0].clicks==1) {intMenu=0; blnUpdateDisplay=true; } //Button Left: back to mainmenu
if (button[1].clicks==1) {intMenu2++; blnUpdateDisplay=true; if (intMenu2>6){intMenu2=1;} }
if (button[2].clicks==1) {intMenu2--; blnUpdateDisplay=true; if (intMenu2<1){intMenu2=6;} }
if (button[3].clicks==-1) {
intMenu = 20 + intMenu2;
//Do something
}
if (blnUpdateDisplay==true) {
Serial.print ("intMenu = "); Serial.println(intMenu); tab(); Serial.print("intMenu2 = "); Serial.println(intMenu2);
Update_display();
blnUpdateDisplay= false;
} //end if
} //End of setting menu control
Question.ino (4.37 KB)