Translate

Sunday, 20 April 2014

Writing a Menu for the Room Management System Part 1


While banging my head on how to keep the Room Management System a stand alone unit and while I am still waiting for some parts to arrive to connect the LCD to the Atmega 328 using only 3 digital pins, I started to write a menu system to implement.
Not even half way through the menu, I realised, that the menu will be again over a 1000 lines of code and even squeezing as much as any possible in functions uses already 450 byte of dynamic memory. Cleaning up the code from the room management system itself might help a bit but by all calculations I made, it will go above what the Atmega 328 can handle.

For everybody who would like to convert the whole system to a Arduino Mega, I share what I have done so far.
As said, I am still waiting for parts to connect the LCD to the room management system, so I used my Arduino board with standard connections for testing. Please check my last post LCD discovery.

While also running out of breadboard space, I soldered quickly a button shield with 3 buttons together and connected it to the Arduino digital pins 8, 9 and 10.



To save some more breadboard space and a few wires, I also built a simple shield for the LCD.



To clear up a little confusion, here a image with the LCD display attached.



Please make sure you solder the 10K potentiometer, the red marked jumper wire and the 8 pin female header for the jumpers connecting the Arduino board to the soldering side of the board, while the rest goes as usual on the parts side of the board. The connections DI 2 to DI 12 depending on your Arduino set up and demonstrating only to which pins I have connected them.
Finally a image showing everything connected together:



The set up:
If you have been looking around a little, you may notice that most of the examples and open source projects use a 1602 LCD Keypad shield which is very handy but it still needs more Atmega pins than we have left. Going back to the Room Management System project, we have 3 input pins on the third CD4021 input shift register left and we use one more of the Atmega's DI pins to implement a switch activating the setup mode. The goal was to do it with the least resource possible. That's why I went down to 3 separate buttons.

Lets start and declare some variables:


#include <LiquidCrystal.h> //include the liquid crystal library
#include <SPI.h> //not needed with the default lcd library

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Create an instance of Liquid Crystal

const int btnMenu = 8;                //constant for Menu button
const int btnSearch = 9;              //constant for Search button
const int btnSelect = 10;              //constant for Select button
const int btnNone = 0;                 //assignment for no button pressed
const byte arrowRight = B01111110; //defining a special char to be printed
                                                     //on the lcd (right arrow)
                                                     //a list with special characters and it's
                                                     //binary definitions can be found in the
                                                     //Arduino Cookbook

int menuOption = 0;                        //menuOption counter
int menuOptions = 19;                     //available options

void setup() {
lcd.begin(16, 2);                             //start lcd with 16 col and 2 row
pinMode(btnMenu, INPUT);            //setting the pin modes
pinMode(btnSearch, INPUT);
pinMode(btnSelect, INPUT);
}

void loop(){
//The function button_Loop() is the menu entry point, checking if a
//menu related button was pressed
//in a real implementation the button_loop() function should be in a
//conditional statement only to be called when needed

button_loop();
}

//Function button_loop()
void button_loop(){
int button = read_buttons();          //function call to give back button pressed
if(button = btnMenu){                 //If btn Menu was pressed
selectMenu();                             //Function call to select from Menu
}
}

//The following function is the heart of the menu
//it goes through the main menu points where can be
//selected from

void selectMenu(){
int button = 0;                            //resetting the button var
menuOption = 1;                        //set menu option to 1
lcd.clear();                                //clear the lcd screen
lcd.print("Sensitivity:");              //print the first Menu point
while(menuOption <= menuOptions){ //loop through the available menu points
button = read_buttons();            //check if a button was pressed
if(button == btnMenu){             //check if the menu button was pressed
menuOption++;                         //if the menu button was pressed
                                               //add 1 to menuOption
if(menuOption == 2){                //if menuOption is 2
lcd.clear();                                //clear lcd screen
lcd.print("Room photo cut:");      //print second menu point
}
if(menuOption == 3){                //if menuOption is 3
lcd.clear();                                //clear lcd screen
lcd.print("Room photo limit:");      //print third menu point
lcd.setCursor(0, 1);                     //set cursor to second row
lcd.print("NOT USED");              //print note that this menu point is not used

//it carries on the same way up to menuOption 19
//check if menuOption is number n
//clear the display
//print the menu point
//if not in use set cursor to second row
//if not in use print message

}
if(menuOption == 4){
lcd.clear();
lcd.print("OS photo cut:");
}
if(menuOption == 5){
lcd.clear();
lcd.print("OS photo Limit:");
lcd.setCursor(0, 1);
lcd.print("NOT USED");
}
if(menuOption == 6){
lcd.clear();
lcd.print("Room 1:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 7){
lcd.clear();
lcd.print("Room 2:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 8){
lcd.clear();
lcd.print("Room 3:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 9){
lcd.clear();
lcd.print("Room 4:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 10){
lcd.clear();
lcd.print("Room 5:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 11){
lcd.clear();
lcd.print("Room 6:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 12){
lcd.clear();
lcd.print("Room 7:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 13){
lcd.clear();
lcd.print("Room 8:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 14){
lcd.clear();
lcd.print("Room 9:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 15){
lcd.clear();
lcd.print("Room 10:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 16){
lcd.clear();
lcd.print("AC 1:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 17){
lcd.clear();
lcd.print("AC 2:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 18){
lcd.clear();
lcd.print("AC 3:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
if(menuOption == 19){
lcd.clear();
lcd.print("AC 4:");
lcd.setCursor(15, 0);
lcd.write(arrowRight);
}
}
}
}


//function to give back the button which was pressed by it's assigned
//input pin number
int read_buttons(){
if(digitalRead(btnMenu) == HIGH){         //digital read the Menu button pin
delay(250);                                            //button denounce delay
return btnMenu;                                     //if activated return btnMenu
}

//the same conditions apply for the Search and Select button
//in the next two if-statements

else if(digitalRead(btnSearch) == HIGH){
delay(250);
return btnSearch;
}
else if(digitalRead(btnSelect) == HIGH){
delay(250);
return btnSelect;
}
else {
return btnNone;                                   //return 0 if no button was pressed
}
}


No comments:

Post a Comment