Translate

Thursday 24 April 2014

Writing a menu for the Room Management System - Part 4


The final part of this approach will add the missing function to update the timers a dirty way of printing the updated values and a explanation how to carry on in the menu structure.

Let's start with showing the updated values on the lcd. Es just mentioned, it is not the most elegant way of doing it but I just wanted a quick possibility to check the response of the functions and the updated values. There fore we go straight down to the main loop.

void loop() {
while(digitalRead(btnMenu) != HIGH){      //keep on rolling over the values
                                                              //until we press the menu button
                                                              //with all the delays for readability
                                                              //you got to press the menu button
                                                             //for about 8 seconds to get out of it
lcd.clear();                                              //empty the screen
lcd.print("Sensitivity");                             //print the variable name
lcd.setCursor(0, 1);                                 //set the cursor to the next row
lcd.print(sensitivity);                                //print the value
delay(1000);                                           //delay for readability
lcd.clear();                                             //empty the screen
lcd.print("Room photo cut");                    //print the variable name
lcd.setCursor(0, 1);                                 //set the cursor to the next row
lcd.print(photoCellCutOff);                      //print the value
delay(1000);                                          //same thing for all the variables
lcd.clear();
lcd.print("Out photo cut");
lcd.setCursor(0, 1);
lcd.print(photoOutsideOff);
delay(1000);
lcd.clear(); //until here
lcd.print("R1 PIR delay");                           //print the variable name
lcd.setCursor(0,1);                                     //set the cursor to the next row
lcd.print(dBed1/60000);                              //print the value / 60000
                                                               //since the value is held in milliseconds
                                                               //we need to divide it through
                                                               //milliseconds contained in 1 minute
                                                               //(60000) to get the value displayed
                                                              //in minutes
lcd.setCursor(5, 1);                                  //set cursor to pos 5 in the second row
lcd.print("min");                                       //print min for minutes
delay(1000);                                            //delay for readability
lcd.clear();                                              //clear the screen
lcd.print("R1 HT On/Off");                       //print the variable name
lcd.setCursor(0, 1);                                 //set cursor to next row
if(room1MActive == 1){                          //if the timer is used (value 1)
lcd.print("Active");                                  //print “Active”
}
else {                                                     //if anything else is held in the var
lcd.print("Off");                                      //print “off”
}
delay(1000);                                          //delay for readability
lcd.clear();                                            //clear screen
lcd.print("R1 T1 Set time");                    //print var name
lcd.setCursor(0, 1);                               //set cursor to next row
lcd.print(room1OnM[0]);                       //print hour the timer activates
lcd.print(":");                                         //print separator
if(room1OnM[1] < 10) lcd.print("0");      //if the minute part is less then 10
                                                           //we add a leading 0
lcd.print(room1OnM[1]);                       //print minute the timer activates
lcd.setCursor(8, 1);                               //set cursor to second half of the row
lcd.print(room1OffM[0]);                     //print hour the timer deactivates
lcd.print(":");                                       //print separator
if(room1OffM[1] < 10) lcd.print("0");   //if the minute part is less than 10
                                                         //we add a leading 0
lcd.print(room1OffM[1]);                    //print minute the timer deactivates
delay(1000);                                       //delay for readability
}
delay(1000);                                       //delay for reaction time
lcd.clear();                                         //clear screen
lcd.print("Setup Mode");                      //now press menu button again to
                                                        //get into the setup mode
delay(1000);                                      //compensation for reaction time
button_loop();
}


As said, not an elegant way of doing it but I only needed a feedback to what is happening and it's nice to see that the values are changing.

For real implementation in the room management system a elegant way would be to add a switch to a free atmega pin and loop button_loop() function while the switch is activated like:

while(digitalRead(setupSwitch) == HIGH) button_loop();

This will keep the menu buttons accessible as long as the implemented switch is active.

Next we look at the function updating the timer. We can add it again right after the selectMenu() function. Variables the function needs is again a describing text what we are changing, the hour to switch on, the minutes to switch on, hour to switch off, minutes to switch off and the menu point where the call comes from.

unsigned int get_setTime(char timeText[], unsigned int onTimeH,
unsigned int onTimeM, unsigned int offTimeH,
unsigned int offTimeM, int menuPoint){
int subButton = 0;                            //resetting the subButton var
unsigned int onHourTS = onTimeH;   //passing the hour var for processing

//next we call the timer function we have already used to change some values.
//This comes in pretty handy since the entry is again a describing text, the actual
//value of the variable we need to change, the minimum and maximum set points
//here we pass the hour part of the switch on time of the timer

unsigned int onHourValue = get_Timer("Hour On", onHourTS, 0, 23);
if(onHourValue >= 0 && onHourValue < 24){              //if the returned value is
                                                                              //within the limits
unsigned int onMinuteTS = onTimeM;                        //we move on to the next
//next part. This time we pass the minutes of the switch on time to the get_Timer
//function
unsigned int onMinuteValue = get_Timer("Minutes On", onMinuteTS, 0, 59);
if(onMinuteValue < 60){                                            //if the returned value is
                                                                              //within the limits
unsigned int offHourTS = offTimeH;                          //we move on to the next part.
unsigned int offHourValue = get_Timer("Hour off", offHourTS, 0, 23);
if(offHourValue >= 0 && offHourValue < 24){
unsigned int offMinuteTS = offTimeM;
unsigned int offMinuteValue = get_Timer("Minute off", offMinuteTS, 0, 59);
if(offMinuteValue < 60){                                            //after processing all the parts
                                                                               //of the timer
lcd.clear();                                                               //clear the screen
lcd.setCursor(0, 1);                                                   //and print the updated results
lcd.print(onHourValue);
lcd.print(":");
if(onMinuteValue < 10)lcd.print("0");
lcd.print(onMinuteValue);
lcd.setCursor(8, 1);
lcd.print(offHourValue);
lcd.print(":");
if(offMinuteValue < 10) lcd.print("0");
lcd.print(offMinuteValue);
while(subButton != btnSelect){                                   //if we are happy with the results
subButton = read_buttons();                                       //we keep them
if(subButton == btnSelect){
lcd.clear();
lcd.print("Saving..... ");
delay(1000);
if(menuPoint == 3){                                                 //and pass them back to the original
room1OnM[0] = onHourValue;                                  //variables
room1OnM[1] = onMinuteValue;
room1OffM[0] = offHourValue;
room1OffM[1] = offMinuteValue;
}
}
}
return 0;
}
}
}
}
}

Looking through, we have all the functions we need to change all values in the main setup part of the sketch. Now lets have a quick look how to carry on in the menu structure. To carry on with the sub menus, we just go to the end of the selectMenu() function. And keep on adding:

if(room1SubMenu == 2){
get_offon("R1 T1 On Off", room1MActive);
return;
}
if(room1SubMenu == 3){
get_setTime("R1 T1 On/Off", room1OnM[0], room1OnM[1],
room1OffM[0], room1OffM[1], room1SubMenu);
return;
}
//>>>>>>>>>>>>>Keep on adding from here<<<<<<<<<<<<<<
//To finish the already initialised sub menus, we adding from here
if(room1SubMenu == 4){
get_offon(“R1 T2 On/Off”, room101Active);
return;
}
}
return;
}
}
}
}

And so on. Check the next sub menu point, call the appropriate function with the corresponding variables and we are done. Please don't forget to declare the variables in the top part of the sketch.
To carry on on the next main menu point, we have to pay a little attention. Our last main menu point was menuOption 6. We find exactly the part where we process the last main menu point (current case 6). We go to the opening bracket and find the corresponding closing bracket
if(menuOption == 4){
get_Timer("Set photo cut O", photoOutsideOff, 0, 1024);
return;
}
if(menuOption == 5){
return;
}
if(menuOption == 6){ //<<<<<<<<find the corresponding closing bracket
int subButton = 0;
room1SubMenu = 1;
lcd.clear();
lcd.print("R 1 PIR delay");
while(room1SubMenu < room1SubMenus){
subButton = read_buttons();
if(subButton == btnMenu){
|
|
|
if(room1SubMenu == 2){
get_offon("R1 T1 On Off", room1MActive);
return;
}
if(room1SubMenu == 3){
get_setTime("R1 T1 On/Off", room1OnM[0], room1OnM[1],
room1OffM[0], room1OffM[1], room1SubMenu);
return;
}
}
}
return;
} //<<<<<<<<<<<<<<after this bracket we have to carry on with main menu options
if(menuOption == 7){
int subButton = 0;
room2SubMenu = 1;
lcd.clear();
lcd.print("R 2 PIR delay");
while(room2SubMenu < room2SubMenus){
subButton = read_buttons();
if(subButton == btnMenu){
room2SubMenu++;
if(room2SubMenu == 2){

//to be carried on same like menu point 6.
//again, please do not forget to declare your new
//variables accordingly and close the opened brackets!!
//you also need to go back into the corresponding functions
//and add the statement to passing the updated value back to the 
//original variable
}
}

That's it for this approach. As mentioned, my next task is to get the 3 wire version of the lcd working and have a look in to working with the program memory not to use up to much RAM. If you have any questions about this one, please feel free to post it on the end and I will try to answer it as good as I can.

No comments:

Post a Comment