Nick & Mike,
Thanks, I understand that the full sketch is needed, I cutted all the unnecessary part to make it smaller.
the involved part is quite short but I wanted to post a compiling sketch, so the buttons control is in there.
#include <Wire.h>
#include <LiquidCrystal.h>
#include <buttons.h>
#include <MENWIZ.h>
#include <EEPROM.h>
#include <AccelStepper.h>
#include <AFMotor.h> //not needed if not using motor shield
// Create global object for menu and lcd
menwiz menu;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// Stepper Motor Parameters
AF_Stepper motor1(200, 2);
const int stepsPerRevolution = 200;
void forwardstep1() {
motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() {
motor1.onestep(BACKWARD, SINGLE);
}
boolean wr=0; // Instantiate global variables to bind to menu
const int buttonPin = A0; // 4 button switch circuit input connected to analog pin 0
const int led13Pin = 13; // the number of the LED output pin
int stepCount = 0; // number of steps the motor has taken
int HeightZ; //ABS Height Variable = mm
int MoveREL; //Rel Move Variable = mm
boolean buttonBlock = 0;
boolean buttonAct = 0; // flag 1
boolean stopMenu = 0; // flag 2
byte buttonPressed = 0; // which button was pressed
byte lastButtonPressed = 0; // prev button pressed
int buttonValue = 0; // read value from analog port for the 4 navigation pushbuttons
long menuOffTime = 0; //used for timer
extern byte MW_navbtn; //needed to run the software key navigation in Menwiz
AccelStepper stepper1(forwardstep1, backwardstep1);
//AccelStepper stepper2(forwardstep2, backwardstep2);
void setup()
{
Serial.begin(9600); // Output to serial writing
digitalWrite((A0), HIGH); // enable the 20k internal pullup for UNO based board for analog port A0
// Motor Speed & Accel
stepper1.setMaxSpeed(400);
stepper1.setAcceleration(50);
//++++++++++++++++++++Menu and LCD
char b[84];
_menu *r,*s1,*s2,*s3;
// initialize the menu object ( 4 x rows x 20 colums LCD)
menu.begin(&lcd,20,4); //initialize the menwiz menu object passing the lcd object and the colums and rows params
menu.addUsrNav(navMenu,4);
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)
//------ Sega Squadratrice ---------
s1=menu.addMenu(MW_SUBMENU,r,F("SAW")); //add a submenu node 2 to the root menu (control the heigh of my Saw)
s2=menu.addMenu(MW_SUBMENU,s1,F("Saw Height"));
s3=menu.addMenu(MW_VAR,s2,F("Absolute Height")); //add a terminal node in the menu tree (that is "variable"); (Move AT a certain mm height)
s3->addVar(MW_AUTO_INT,&HeightZ,0,100,1); //int type, fd binded variable, rage 0-100, step 1
s3=menu.addMenu(MW_VAR,s2,F("ABS_Height")); //Call the Action ABS_Height to move the Stepper in ABS mode
s3->addVar(MW_ACTION,ABS_Height); //Fire the Ation Above
s2=menu.addMenu(MW_SUBMENU,s1,F("Saw Move")); //add a terminal node in the menu tree (that is "variable"); (Move OF a certain mm)
s3=menu.addMenu(MW_VAR,s2,F("Relative Move"));
s3->addVar(MW_AUTO_INT,&MoveREL,-100,100,1); //int type, fd binded variable, rage -100 +100, step 1
s3=menu.addMenu(MW_VAR,s2,F("MoveSAW")); //Call the Action moveSAW to move the Stepper in REL mode
s3->addVar(MW_ACTION,MoveSAW); //Fire the Ation Above
pinMode(led13Pin, OUTPUT); // initialize the led as a output control pin
}
void loop()
{
// NAVIGATION MANAGEMENT & DRAWING ON LCD. NOT BLOCKING has to be the first in the void loop
menu.draw();
readButtons();
}
//**** HERE IS THE PROBLEMATIC PART *******
//int HeightZ = 0;
int LastHeightZ = 0;
//int NewHeightZ = 0;
void ABS_Height(){ //This is the Absolute Move Action to be implemented
HeightZ = 0;
if (HeightZ > 0) HeightZ = LastHeightZ;
stepper1.moveTo((LastHeightZ - HeightZ) * 50); // move Height1 mm's
lcd.print ("Height mm ");
lcd.print (HeightZ);
delay (5000);
}
//**** FROM HERE EVERITHING IS WORKING ******
void MoveSAW(){ //This is the Relative Movement of the SAW. WORKING ! :-)
stepper1.moveTo(MoveREL * 50); // move the SAW X mm's
lcd.print ("Move ");
lcd.print (MoveREL);
lcd.print (" mm");
delay (5000);
}
// Here is the working coce to use MENWIZ with 4 Analog Pushbottons.
// ++++++ functions +++++++
void readButtons()
// ++++ Control 4 buttons ++++
{
lastButtonPressed = buttonPressed;
// Analog values representing the pushbutton value are depending on the resitor value used.
buttonValue = analogRead(buttonPin); //analog value to simulate the pushed buttons
if(buttonValue >= 850)
{
buttonPressed = 0;
noButtonPressed(); // is calling an extra fucntion
}
else if(buttonValue >= 380 & buttonValue <= 450)
{
buttonPressed = 4;
buttonAct = 1; // set the menu flag1
}
else if(buttonValue >= 200 & buttonValue <=300)
{
buttonPressed = 3;
buttonAct = 1; // set the menu flag1
}
else if(buttonValue >= 0 & buttonValue <=50)
{
buttonPressed = 2;
buttonAct = 1; // set the menu flag1
}
else if(buttonValue >= 80 & buttonValue <=150)
{
buttonPressed = 1;
buttonAct = 1; // set the menu flag1
}
}
int noButtonPressed()
{
return MW_BTNULL;
}
int navMenu() // called from menu.draw
{
/*
As soon as a button is pushed the first time flag 1 is set to HIGH and if the buttonnumber is not 0 then a timer is started.
The menu action then should only be performed once.
After 400 msecs the flag will be set to LOW and a new buttonpush can be processed.
Menu action is blocked for 400 msec even if the same button is being kept pressed for 2000 msecs by flag2.
*/
long menublockTime = millis();
if (buttonAct == 1 && buttonPressed != 0 && stopMenu == 0) // we have a state were we process a menu navigation step once and block it for 2 secs
{
digitalWrite(led13Pin,HIGH); // set timer led ON
menuOffTime = menublockTime + 400; //start the timer. You can change this blocking time to your convenience but do not make it lower aa 200 msecs
stopMenu = 1;
switch (buttonPressed)
{
case 1: // Up
return MW_BTU;
break;
case 2: // Confirm
return MW_BTC;
break;
case 3: // Down
return MW_BTD;
break;
case 4: // Escape
return MW_BTE;
break;
}
}
if (menuOffTime != 0 && menublockTime > menuOffTime) // Reset of the timer so a new menu action can be processed
{
buttonAct = 0; // resets the flag 1
buttonPressed = 0;
menuOffTime = 0; // resets timer to zero
stopMenu = 0;
digitalWrite(led13Pin,LOW); // set timer led OFF
}
}