Tuxduino,
Thanks for your suggestion in your boss time.
The key point IMHO is don't mix different levels of functionality, e.g. detecting a key based on the analogRead value and deciding which menu page is the next one. Try to write functions that do one thing, and do it independently of all the other code.
I worked on the pseudo code with my knowledge to see if I ccould get it running to debug first and then take the next step.
Code below is what I made of it sofar.
It will not compile due to two lines telling me there is else or else if with no previous if.
Lines are marekd with @@@@@@@@ in the end.
I cant get my finger on it.
So I commented those two line and then it compiles but that is not good.
Paco
// +++++++ Libaries included
#include <Wire.h>
//INSERT ALL THE LIBARIES AFTER INCLUDING WIRE LIB (MENWIZ request)
#include <LiquidCrystal_I2C.h>
#include <buttons.h>
#include <MENWIZ.h>
#include <EEPROM.h>
#include <Boards.h>
// Create global object for menu and lcd
menwiz menu;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlightpin, polarity
// Instantiate global variables to bind to menu
boolean wr=0;
boolean ButtonState= 0;
const int analogPin = A0; // 10 button switch circuit input connected to analog pin 0
int buttonValue; // read value from analog port 0 for 6 QAB
extern byte MW_navbtn;
int _currKey = 0;
int _prevKey = 0;
#define KEY_UP 1
#define KEY_CONFIRM 2
#define KEY_DOWN 3
#define KEY_ESCAPE 4
#define KEY_NONE 11
void setup()
{
Serial.begin(9600); // Output to serial writing or BT
digitalWrite((54), HIGH); // enable the 20k internal pullup for MEGA board
//++++++++++++++++++++Menu and LCD
char b[84];
_menu *r,*s1,*s2;
// initialize the menu object (20 colums x 4 rows LCD)
menu.begin(&lcd,20,4);
//menu.addUsrNav(navMenu);
MW_navbtn=4; // force 4 or 6 buttons mode for menu navigation -> MW_navbtn=4; or MW_navbtn=6;
//create the menu tree
r=menu.addMenu(MW_ROOT,NULL,F("MAIN MENU")); //create a root menu at first (required)
//---------------
s1=menu.addMenu(MW_SUBMENU,r,F("SET1")); //add a submenu node 1 to the root menu
s2=menu.addMenu(MW_VAR,s1,F("MD")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_AUTO_BYTE,&modelNumber,1,10,1); //Set value
s2=menu.addMenu(MW_VAR,s1,F("BR")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_AUTO_BYTE,&brakeRValue,1,255,stepsMValue); //Set value
s2=menu.addMenu(MW_VAR,s1,F("RH")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_AUTO_BYTE,®enHyValue,1,25,1); //Set value
//---------------
s1=menu.addMenu(MW_SUBMENU,r,F("SET2")); //add a submenu node 2 to the root menu
s2=menu.addMenu(MW_VAR,s1,F("CP")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_AUTO_INT,&endCurvePower,1,50,1); //Set value
s2=menu.addMenu(MW_VAR,s1,F("DB")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_AUTO_BYTE,&deadbandXvalue,1,25,1); //Set value
s2=menu.addMenu(MW_VAR,s1,F("SQ")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_AUTO_BYTE,&stepsQValue,0,10,1); //Set value
s2=menu.addMenu(MW_VAR,s1,F("SM")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_AUTO_BYTE,&stepsMValue,0,10,1); //Set value
//---------------
s1=menu.addMenu(MW_SUBMENU,r,F("SET3")); //add a submenu node 3 to the root menu
s2=menu.addMenu(MW_VAR,s1,F("RS")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_BOOLEAN,®brakeState); //Set value
s2=menu.addMenu(MW_VAR,s1,F("RCALIB")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_ACTION,resetCalibration); //Set value
s2=menu.addMenu(MW_VAR,s1,F("SCALIB")); //add a terminal node in the menu tree (that is "variable");
//s2->addVar(MW_ACTION,saveCalibration); //Set value
}
void loop()
{
// NAVIGATION MANAGEMENT & DRAWING ON LCD. NOT BLOCKING has to be the first in the void loop
menu.draw();
}
//++++ functions +++++++++++++++
int analogToKey()
{
buttonValue = analogRead(0); // read the input pin 0
// internal 20K ohm pullup is enabled on analog pin 0 (54).
if(buttonValue >= 920 and buttonValue <= 930) // @analog value = 910
{
return KEY_UP; // Up
}
else if(buttonValue >= 820 and buttonValue <= 835) // @analog value = 827
{
return KEY_CONFIRM; // Confirm
}
else if(buttonValue >= 725 and buttonValue <= 740) // @analog value = 731
{
return KEY_DOWN; // Down
}
else if(buttonValue >= 630 and buttonValue <= 645) // @analog value = 637
{
return KEY_ESCAPE; // Escape
}
else
{
return KEY_NONE; // no button pushed @analog value = 1021
}
}
int readButton()
{
_currKey = analogToKey();
if (_prevKey != _currKey); // if a new key is pressed we go further
{
if (_prevKey == KEY_NONE); // no key was pressed and now a key is pressed
{
keyPressed(_currKey); // _currKey has been pressed
}
//else if (_currKey == KEY_NONE); //@@@@@@@@@@@@@@@@@@
{
keyReleased(_prevKey); // _prevKey was pressed and now no key is pressed, thus _prevKey has been released
}
//else // directly from one key to another //@@@@@@@@@@@@@
{
keyReleased(_prevKey);
keyPressed(_currKey);
}
}
_prevKey = _currKey;
}
void keyPressed(int key)
{
Serial.print("Pressed: ");
Serial.println(key, DEC);
}
void keyReleased(int key)
{
Serial.print("Released: ");
Serial.println(key, DEC);
}