Hi,
I can't focus the proble very well, the same commands do the correct calculation but the stepper does not execute the corect movement.
Let's start with the complete code:
#include <Wire.h>
#include <LiquidCrystal.h>
#include <buttons.h>
#include <MENWIZ.h>
#include <EEPROM.h>
#include <AccelStepper.h> //instead of Stepper.h
// Create global object for menu and lcd
menwiz menu;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//***** Stepper Motor Parameters *****
AccelStepper stepper(4, 2, 3, 4, 5);
const int stepsPerRevolution = 200;
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 = 0; //ABS Height Variable = mm
int ActualHeightZ = 0;
int NewHeightZ;
int MoveREL; //Rel Move Variable = mm
int Height2;
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
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 ******
stepper.setMaxSpeed(200);
// stepper.setSpeed(110);
stepper.setAcceleration(50);
//********** Menu and LCD *********
char b[84];
_menu *r,*s1,*s2,*s3;
// initialize the menu object (16x2 LCD)
menu.begin(&lcd,16,2); //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)
//****** SAW ******
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")); // (Move AT a certain mm height)
s3=menu.addMenu(MW_VAR,s2,F("Absolute Height")); // add a terminal node in the menu tree (that is "variable")
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("Saw 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")
s3=menu.addMenu(MW_VAR,s2,F("Relative Move")); // (Move OF a certain mm)
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
s2=menu.addMenu(MW_VAR,s1,F("Zero")); //add a terminal node in the menu tree (that is "variable"); (should set the zero)
s2=menu.addMenu(MW_VAR,s1,F("Setup")); //add a terminal node in the menu tree (that is "variable"); (set the parameters)
//******* Planner *******
s1=menu.addMenu(MW_SUBMENU,r,F("PLANNER"));
//******* Shaper *******
s1=menu.addMenu(MW_SUBMENU,r,F("SHAPER"));
}
void loop()
{
// NAVIGATION MANAGEMENT & DRAWING ON LCD. NOT BLOCKING has to be the first in the void loop
menu.draw();
ReadButtons();
stepper.run();
}
void ABS_Height() // Absolute Height Function
{
NewHeightZ = (HeightZ - ActualHeightZ);
stepper.moveTo (NewHeightZ* 200); // move Height1 mm's
Serial.print ("Move mm ");
Serial.print (NewHeightZ);
Serial.print ("Height mm ");
Serial.print (HeightZ);
ActualHeightZ = HeightZ;
}
void MoveSAW() // This is the Relative Movement of the SAW. WORKING ! :-)
{
stepper.moveTo (MoveREL * 200); // move the SAW X mm's
Serial.print ("Move mm ");
Serial.print (MoveREL);
HeightZ = (HeightZ+MoveREL);
ActualHeightZ = HeightZ;
MoveREL = 0;
Serial.print ("Height mm ");
Serial.print (HeightZ);
}
void ReadButtons()
// ++++ Control 4 buttons ++++
{
lastButtonPressed = buttonPressed;
buttonValue = analogRead(buttonPin); //analog value to simulate the pushed buttons
if(buttonValue >= 850)
{
buttonPressed = 0;
noButtonPressed(); // ? calling an extra fucntion
}
else if(buttonValue >= 480 & buttonValue <= 550)
{
buttonPressed = 4;
buttonAct = 1; // set the menu flag1
}
else if(buttonValue >= 280 & buttonValue <=380)
{
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 >= 100 & buttonValue <=200)
{
buttonPressed = 1;
buttonAct = 1; // set the menu flag1
}
}
int noButtonPressed()
{
return MW_BTNULL;
}
int navMenu() // called from menu.draw
{
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 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
}
}
This is the Calculation "core":
void ABS_Height() // Absolute Height Function
{
NewHeightZ = (HeightZ - ActualHeightZ);
stepper.moveTo (NewHeightZ* 200); // move Height1 mm's
Serial.print ("Move mm ");
Serial.print (NewHeightZ);
Serial.print ("Height mm ");
Serial.print (HeightZ);
ActualHeightZ = HeightZ;
}
void MoveSAW() // This is the Relative Movement of the SAW. WORKING ! :-)
{
stepper.moveTo (MoveREL * 200); // move the SAW X mm's
Serial.print ("Move mm ");
Serial.print (MoveREL);
HeightZ = (HeightZ+MoveREL);
ActualHeightZ = HeightZ;
MoveREL = 0;
Serial.print ("Height mm ");
Serial.print (HeightZ);
}
The serial Monitor display always the correct calculation. The MoveSAW funtion called to execute +1 mm = 1 turn (200 step) perfecly works, but then if I make -1 mm = -1 turn (200 step backwards) the motors turns 400 steps backwards (-2 turns) but the serial monitor display move 1mm (heigh 0 mm) and that is correct. The motor understood "go to -1 mm so it moves 2 turns backwards (from +1 to -1)
Another example: execute +1 mm (1 turn) ok,move 200 steps ! then execute another +1 mm (1 turn) ko, no move !
now some example just with numbers:
reset arduino
+1mm = +1 turn; +2mm= +1 turn; +4mm=+2 turn; +5mm=+1 turn; +10mm=5 turns; -10mm=-20 turns
+1=1 +2=1 +4=2 +5=1 +10=5 (the display correctly print 22mm ) then +1=-9 turns (display +23)
Any Hint ? I'm going crazy !