Hi, I'm trying to implement MENWIZ with 3 buttons instead of 4.
I have 3 buttons on an analog pin, when any button is pressed an interrupt is triggered to read the pin, set a flag to say button processing required and the button that was pressed.
I defined my own button function as per the Tour pdf (very useful by the way!!) which I guess MENWIZ polls and added submenu items that call my exit function, this just sets a couple of flags like the interrupt would but to emulate an ESCAPE press. Seems to work but I get this confirm to run message too. is it possible to disable that? or do you have recommendations on implementing MENWIZ with 3 buttons rather than 4?
Not sure if this is something I did but the "Node4" MW_AUTO_INT looses it's label in the menu sometimes (pssibly related to using the List option first.). If you select it it no longer runs instead returning to the root menu. Also I can't seem to access the freeMem() funct, has it been removed?
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_SR.h>
#include <buttons.h>
#include <MENWIZ.h>
#include <MemoryFree.h>
//INTERRUPT BUTTONS
const int pin = 13; // test for interrupt
const int buttonPin = A0; // buttons can be read here
volatile byte buttonAct = 0; // flag, follow button press
volatile int buttonValue = 0; // button pressed analogue value
volatile int buttonPressed = 0; // which button was pressed
volatile byte lastButtonPressed = 0; // prev button pressed
volatile byte state = LOW; // state of led on pin13
volatile byte lastState = HIGH; // unused?
/*
// DEFINE ARDUINO PINS FOR THE NAVIGATION BUTTONS
#define UP_BOTTON_PIN 9
#define DOWN_BOTTON_PIN 5
#define LEFT_BOTTON_PIN 7
#define RIGHT_BOTTON_PIN 8
#define CONFIRM_BOTTON_PIN 3
#define ESCAPE_BOTTON_PIN 4
*/
menwiz tree;
// create lcd obj using LiquidCrystal lib
LiquidCrystal_SR lcd(10, 11, 12); //Data,Clk,Enable.
int list,sp=110;
void setup(){
Serial.begin(19200);
Serial.println(F("STARTED"));
pinMode(pin, OUTPUT);
attachInterrupt(0, readButtons, RISING);
_menu *r,*s1,*s2;
tree.addUsrNav(navMenu);
tree.begin(&lcd,20,4); //declare lcd object and screen size to menwiz lib
r=tree.addMenu(MW_ROOT,NULL,"Root");
s1=tree.addMenu(MW_SUBMENU,r,"SUBMENU");
s2=tree.addMenu(MW_VAR,s1,"Node3");
s2->addVar(MW_LIST,&list);
s2->addItem(MW_LIST,"Option1");
s2->addItem(MW_LIST,"Option2");
s2->addItem(MW_LIST,"Option3");
s2=tree.addMenu(MW_VAR,s1,"Node4");
s2->addVar(MW_AUTO_INT,&sp,0,120,10);
s2=tree.addMenu(MW_VAR,s1,"Exit");
s2->addVar(MW_ACTION,exitFunct);
// tree.navButtons(UP_BOTTON_PIN,DOWN_BOTTON_PIN,LEFT_BOTTON_PIN,RIGHT_BOTTON_PIN,ESCAPE_BOTTON_PIN,CONFIRM_BOTTON_PIN);
}
int i = 0;
void loop(){
if(i > 1000){
Serial.print(F("freeMemory() reports "));
Serial.println( freeMemory() );
i = 0;
}else{
i++;
}
tree.draw();
}
void myfunc(){
Serial.println("ACTION FIRED!");
}
void readButtons(){
state = !state;
digitalWrite(pin, state);
/* 511 : none | 614 : up | 768 : down | 1023 : select */
lastButtonPressed = buttonPressed;
buttonValue = analogRead(buttonPin);
if(buttonValue >= 900) {
buttonPressed = 3; // select
}
else if(buttonValue >= 700){
buttonPressed = 2; // down
}
else if(buttonValue >= 600){
buttonPressed = 1; // up
}
buttonAct = 1;
Serial.print(buttonPressed);
Serial.print(" ");
Serial.println(buttonValue);
}
void exitFunct(){
buttonAct = 1;
buttonPressed = 4;
}
int navMenu(){
//MenuItem currentMenu=menu.getCurrent();
if(buttonAct){
buttonAct = 0;
switch (buttonPressed){
case 4: //simulated escape
return MW_BTE;
break;
case 3: //select
return MW_BTC;
/*
if (menu.getCurrent().getRight() != 0){ //The current item has an element right, it's a sub menu so nav right.
menu.moveRight();
Serial.print(menu.getCurrent().getName());
Serial.println(F("has menu right"));
}
else{ //otherwise, menu has no child and has been pressed. enter the current menu
menu.use();
}
*/
break;
case 2: //down
return MW_BTD;
break;
case 1: //up
return MW_BTU;
break;
}
}
else{
return MW_BTNULL;
}
}