I'm using the nuelectronics 3310 LCD with this library (http://blog.thiseldo.co.uk/?p=383)
The example provided shows one main menu with each option leading to various demos.
I've changed the menu text and have my first option working fine (it leads to a simple temp display area)
But the second option i need to lead to a second menu, this is the bit i can't figure out.
here is my code so far
Part 1:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <nokia_3310_lcd.h>
// Temp/humidity display using nokia 3310 LCD display shield from nuelectronics.com
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//keypad debounce parameter
#define DEBOUNCE_MAX 15
#define DEBOUNCE_ON 10
#define DEBOUNCE_OFF 3
#define NUM_KEYS 5
#define NUM_MENU_ITEM 5
#define NUM_MENU_ITEM1 6
// joystick number
#define UP_KEY 3
#define LEFT_KEY 0
#define CENTER_KEY 1
#define DOWN_KEY 2
#define RIGHT_KEY 4
// Pin used by Backlight, so we can turn it on and off. Pin setup in LCD init function
#define BL_PIN 7
// menu starting points
#define MENU_X 5 // 0-83
#define MENU_Y 0 // 0-5
// adc preset value, represent top value,incl. noise & margin,that the adc reads, when a key is pressed
// set noise & margin = 30 (0.15V@5V)
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
// debounce counters
byte button_count[NUM_KEYS];
// button status - pressed/released
byte button_status[NUM_KEYS];
// button on flags for user program
byte button_flag[NUM_KEYS];
char* shower;
// menu definition
char menu_items[NUM_MENU_ITEM][14]={
"TEMPERATURE",
"WATER REQUEST"
};
void (*menu_funcs[NUM_MENU_ITEM])(void) = {
temperature,
waterreq
};
char menu_items1[NUM_MENU_ITEM1][5]={
"1",
"1.5",
"2",
"2.5",
"Bath",
"None"
};
void (*menu_funcs1[NUM_MENU_ITEM1])(void) = {
shower1,
shower15,
shower2,
shower25,
bath,
none
};
char current_menu_item;
int blflag = 1; // Backlight initially ON
char degree = 0x7b; // Degree symbol
Nokia_3310_lcd lcd=Nokia_3310_lcd();
void setup()
{
pinMode(3, OUTPUT);
sensors.begin();
// setup interrupt-driven keypad arrays
// reset button arrays
for(byte i=0; i<NUM_KEYS; i++){
button_count[i]=0;
button_status[i]=0;
button_flag[i]=0;
}
// Setup timer2 -- Prescaler/256
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
TCCR2B = (1<<CS22)|(1<<CS21);
ASSR |=(0<<AS2);
// Use normal mode
TCCR2A =0;
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (0<<OCIE2A);
TCNT2=0x6; // counting starts from 6;
TIMSK2 = (1<<TOIE2);
SREG|=1<<SREG_I;
lcd.init();
lcd.clear();
//menu initialization
init_MENU();
current_menu_item = 0;
}
/* loop */
void loop() {
byte i;
for(i=0; i<NUM_KEYS; i++) {
if(button_flag[i] !=0) {
button_flag[i]=0; // reset button flag
switch(i){
case UP_KEY:
// current item to normal display
lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_NORMAL );
current_menu_item -=1;
if(current_menu_item <0) current_menu_item = NUM_MENU_ITEM -1;
// next item to highlight display
lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_HIGHLIGHT );
break;
case DOWN_KEY:
// current item to normal display
lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_NORMAL );
current_menu_item +=1;
if(current_menu_item >(NUM_MENU_ITEM-1)) current_menu_item = 0;
// next item to highlight display
lcd.writeString(MENU_X, MENU_Y + current_menu_item, menu_items[current_menu_item], MENU_HIGHLIGHT );
break;
case LEFT_KEY:
init_MENU();
current_menu_item = 0;
break;
case RIGHT_KEY:
lcd.clear();
(*menu_funcs[current_menu_item])();
lcd.clear();
init_MENU();
current_menu_item = 0;
break;
}
}
}
}
/* menu functions */
void init_MENU(void) {
byte i;
lcd.clear();
lcd.writeString(MENU_X, MENU_Y, menu_items[0], MENU_HIGHLIGHT );
for (i=1; i<NUM_MENU_ITEM; i++) {
lcd.writeString(MENU_X, MENU_Y+i, menu_items[i], MENU_NORMAL);
}
}
// waiting for center key press
void waitfor_OKkey() {
byte i;
byte key = 0xFF;
while (key!= CENTER_KEY){
for(i=0; i<NUM_KEYS; i++){
if(button_flag[i] !=0){
button_flag[i]=0; // reset button flag
if(i== CENTER_KEY) key=CENTER_KEY;
}
}
}
}
// Check if joystick is moved or pressed
byte checkKeypressed() {
byte key = 0xFF;
for(int i=0; i<NUM_KEYS; i++){
if(button_flag[i] !=0){
button_flag[i]=0; // reset button flag
if(i== CENTER_KEY) key=CENTER_KEY;
}
}
return key;
}
char numStr[8];
// Format a number to 2 decimal places
void formatNum( int num ) {
// First part before decimalpoint
itoa( num / 100, numStr, 10 );
int pos = strlen( numStr );
numStr[pos++] = '.';
int decimal = num % 100;
if( decimal > 9 ) {
itoa( decimal, &numStr[pos], 10 );
} else {
numStr[pos++] = '0';
itoa( decimal, &numStr[pos], 10 );
}
}
// Display temperature in big digits, humidity in small digits underneath
void temperature() {
int temp1, temp2, temp3, temp4, temp5;
char temp[3];
char* shower;
// Display non changing text, there is a slight delay while first reading is taken
lcd.writeString(0, 0, "Number", MENU_NORMAL);
lcd.writeString(10, 1, "Of", MENU_NORMAL);
lcd.writeString(0, 2, "Showers", MENU_NORMAL);
lcd.writeString(0, 3, "avail:", MENU_NORMAL);
lcd.writeString(75, 0, "C", MENU_NORMAL);
lcd.writeString(75, 1, "C", MENU_NORMAL);
lcd.writeString(75, 2, "C", MENU_NORMAL);
lcd.writeString(75, 3, "C", MENU_NORMAL);
lcd.writeString(75, 4, "C", MENU_NORMAL);
lcd.writeString(38, 5, "OK", MENU_HIGHLIGHT );
long lastUpdate = 0; // Force update
byte i;
byte key = 0xFF;
// Loop to display temperaure/humidity with check for key press to exit
while (key!= CENTER_KEY) {
// Update temp
if( millis() > lastUpdate + 1000) {
// Read temperature and humidity
//Request and display temperatures
sensors.requestTemperatures();
temp1 = sensors.getTempCByIndex(0);
temp2 = sensors.getTempCByIndex(1);
temp3 = sensors.getTempCByIndex(2);
temp4 = sensors.getTempCByIndex(3);
temp5 = sensors.getTempCByIndex(4);
itoa(temp1, temp, 10);
lcd.writeString(60, 0, temp, MENU_NORMAL);
itoa(temp2, temp, 10);
lcd.writeString(60, 1, temp, MENU_NORMAL);
itoa(temp3, temp, 10);
lcd.writeString(60, 2, temp, MENU_NORMAL);
itoa(temp4, temp, 10);
lcd.writeString(60, 3, temp, MENU_NORMAL);
itoa(temp5, temp, 10);
lcd.writeString(60, 4, temp, MENU_NORMAL);
if (temp1 >= 50 && temp4 >= 50)
{
shower = "Bath";
}
else if (temp1 >= 50 && temp2 >= 35 && temp3 <= 30)
{
shower = "2";
}
else if (temp1 >= 50 && temp2 >= 20)
{
shower = "1";
}
else if (temp1 <=45)
{
shower = "0";
}
lcd.writeString(10, 4, shower, MENU_NORMAL);
lastUpdate = millis();
}
key = checkKeypressed();
}
}