Hi All
I have been struggling with this for days now and can't solve it, I need some help.
I have working LCD sketch and part of it uses a while (1) to block the sketch while the user adjusts either the time or date. I have rewritten this to a state machine
i want to make this non-blocking purely for my own learning to write non-blocking code. However I cannot work out how to use flags to prevent the original calling function continuing to call and therefore overwrite the number the user is trying to change. I have tried hundreds or iterations and google seems to be no help.
The first part of the code is called directly by the state machine (state changed by button push)
void set_time() {
// set up initial LCD display
if(inputTimeFlag == false){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set Time ");
show_time(SECOND_LINE);
setHoursFlag = true;
setMinutesFlag = true;
setSecondsFlag = true;
hoursTmp = tm.Hour;
minutesTmp = tm.Minute;
secondsTmp = tm.Second;
inputTimeFlag = true;
previousMillis = millis();
} // end initial LCD set if
else if (inputTimeFlag == true){
//get_button_input(initial value, x_position, min value, max value)
that sets up flags to set hrs, min secs and sets the temporary variables so the RTC doesn't keep overwriting them.
then each part of the time (hr.min.sec) or date (dd.MM.YYYY) is passed, using hours as an example
if(setHoursFlag == true){
hours = get_button_input(hoursTmp, 8, 0, 23); //hour ranged from 0 to 23
// need to add code here to set hoursflag to false
setHoursFlag = btnUPDATE // button update set to false in the get_button_input
}
This is passed to the get_button_input function. This is where my problem is. There use to be a while statement at the beginning
previousMillis = millis(); // reset timeout so that the timeout will not occur
while (1) { // loop forver
if (isTimeout()) { //exit loop if timeout occurred
return -1;
}
which is what I want to remove. but every time I do the calling function just keeps updating the data plus a few other issues such as my local variables keep being reinitialized to 0 unless I make them global and then the case btnRIGHT: which should confirm the value isn't setting val or returning the value to the original calling function. I think my preblem is things are looping back through and I'm not catching them in the right place.
Should I be looking a for statements to do counts ? Any other suggestions are welcome.
//////////////////////////////////////////////////////////////////////////
// read input from button switches
//////////////////////////////////////////////////////////////////////////
int get_button_input(int val, byte pos_x, int min, int max) { // Takes for example int hourstmp = tm.hours and the LCD position plus min and max allowed
// eg 13 9 0 23 for 13:00hrs 9th column min 0hrs max 23 hrs
int tmpVal; // initialises variables to use within this function - Issue is they keep being reinitialised back to 0 unless they are global.
int result; // as the original call function keeps calling
previousMillis = millis(); // reset timeout so that the timeout will not occur
if(getInputFlag == true){ // flag for the first read through
tmpVal = val; // sets the tmpVal to match the val passed
getInputFlag = false; // sets the flag so the next run to allow function to continue
}
if (getInputFlag == false){
lcd_key = key_press(); // read the buttons
switch (lcd_key) {
case btnUP:
tmpVal++; // if we increase tmpVal to 15 for 1500 hrs
if (tmpVal > max) tmpVal = min;
break;
case btnDOWN:
tmpVal--;
if (tmpVal < min) tmpVal = max;
break;
case btnRIGHT:
val = tmpVal; // this should set val to tmp value ie val now equals 15
lcd.setCursor(pos_x, SECOND_LINE); // set curser to the hours position
lcd.print(int2str(val)); // writes the new value ie 15
getInputFlag = true; // resets the flag
btnUPDATE = false // passed back to original calling function
result = val; // unnessary but there
return result; // should retun 15 to the original passing function
break;
}// end switch
blink_text(pos_x, int2str(tmpVal)); // makes the value being edit blink
if (lcd_key != btnNONE) { //if a button is pressed
previousMillis = millis(); // reset timeout so that the timeout will not occur
}
}
} // end get_button_input
Cheers
Al