Works really good, I’m also able to use the MPG at the same time,
The code below is modified with dual “different” encoders, edit this code from now on👌
also added 3 more buttons, now its just a potentiometer to control the speed
// Lathe003
// Change float encoder counts to long
// Add joystick test with 4 buttons
// Added support for 2 Diffrent Encoders 100p/r and 400p/r (steps_per_pulse_z)
#include <AccelStepper.h>
#define encoder_pin_Ay 2
#define encoder_pin_By 3
#define encoder_pin_Az 4
#define encoder_pin_Bz 5
// Define input button pins
const int LEFT_PIN = 44; // Input Pin location on the Arduino, Left Button Yellow Wire
const int RIGHT_PIN = 46;// Input Pin location on the Arduino, Right Button Pink Wire
const int UP_PIN = 48; // Input Pin location on the Arduino, Left Button Yellow Wire
const int DOWN_PIN = 50;// Input Pin location on the Arduino, Right Button Pink Wire
const int ENA_PIN = 52;// ENABLE PIN
int encoder_pin_Ay_last = LOW;
int encoder_pin_Az_last = LOW;
long encoder_pos_y = 0;
long encoder_pos_z = 0;
int ny = LOW;
int nz = LOW;
/////////////////////////////////////////////////////////////////////
const int buttonPin = 4; // the number of the pushbutton pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
/////////////////////////////////////////////////////////////////////
#define stepper_pin_step_y 8
#define stepper_pin_dir_y 9
#define step_pin_step_z 10
#define step_pin_dir_z 11
// Stepper Driver p/r x1=200 x2=400 x4=800 x8=1600 x16=3200
// Enc have ? steps per revolution. 100/400 Pulses per rev (p/r)
// The motor have 200 steps per revolution at 1x currently set at 2x=400p/r
// Want: 1 encoder rev = 1 stepper rev on channel Z and Y
// 400 / 100 = 4... 100P/R Encoder
// 400 / 400 = 1... 400P/R Encoder
long steps_per_pulse_y = 1;
long steps_per_pulse_z = 1;
AccelStepper stepper_y(AccelStepper::DRIVER, stepper_pin_step_y, stepper_pin_dir_y);
AccelStepper stepper_z(AccelStepper::DRIVER, step_pin_step_z, step_pin_dir_z);
void setup()
{
stepper_y.setMaxSpeed(3000.0);
stepper_y.setAcceleration(4000.0);
stepper_z.setMaxSpeed(3000.0);
stepper_z.setAcceleration(4000.0);
pinMode(encoder_pin_Ay, INPUT_PULLUP);
pinMode(encoder_pin_By, INPUT_PULLUP);
pinMode(encoder_pin_Az, INPUT_PULLUP);
pinMode(encoder_pin_Bz, INPUT_PULLUP);
pinMode(buttonPin, INPUT);
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode(UP_PIN, INPUT_PULLUP);
pinMode(DOWN_PIN, INPUT_PULLUP);
pinMode(ENA_PIN, OUTPUT);
}
void loop()
{
// read encoder
ny = digitalRead(encoder_pin_Ay);
if ((encoder_pin_Ay_last == LOW) && (ny == HIGH))
{
if (digitalRead(encoder_pin_By) == LOW)
{
encoder_pos_y -= steps_per_pulse_y ;
}
else
{
encoder_pos_y += steps_per_pulse_y ;
}
// set stepper to the new calculated position
stepper_y.moveTo(encoder_pos_y);
}
encoder_pin_Ay_last = ny;
////////////////////added///////////////////
nz = digitalRead(encoder_pin_Az);
if ((encoder_pin_Az_last == LOW) && (nz == HIGH))
{
if (digitalRead(encoder_pin_Bz) == LOW)
{
encoder_pos_z -= steps_per_pulse_z ;
}
else
{
encoder_pos_z += steps_per_pulse_z ;
}
// set stepper to the new calculated position
stepper_z.moveTo(encoder_pos_z);
}
//////////////////////////////////////////Y-Axis LEFT Button///////////////////////////////////////////////////////
encoder_pin_Az_last = nz;
if (digitalRead(LEFT_PIN) == LOW)
{
if (stepper_y.currentPosition() == encoder_pos_y) // Stepper is at commanded position
{
encoder_pos_y += steps_per_pulse_y ;
stepper_y.moveTo(encoder_pos_y);
}
}
//////////////////////////////////////////Y-Axis RIGHT Button//////////////////////////////////////////////
encoder_pin_Az_last = nz;
if (digitalRead(RIGHT_PIN) == LOW)
{
if (stepper_y.currentPosition() == encoder_pos_y) // Stepper is at commanded position
{
encoder_pos_y -= steps_per_pulse_y ;
stepper_y.moveTo(encoder_pos_y);
}
}
///////////////////////////////////////////Z-Axis Forward Button//////////////////////////////////////////////////
encoder_pin_Az_last = nz;
if (digitalRead(UP_PIN) == LOW)
{
if (stepper_z.currentPosition() == encoder_pos_z) // Stepper is at commanded position
{
encoder_pos_z += steps_per_pulse_z ;
stepper_z.moveTo(encoder_pos_z);
}
}
//////////////////////////////////////////////Z-Axis Backward Button//////////////////////////////////////////
encoder_pin_Az_last = nz;
if (digitalRead(DOWN_PIN) == LOW)
{
if (stepper_z.currentPosition() == encoder_pos_z) // Stepper is at commanded position
{
encoder_pos_z -= steps_per_pulse_z ;
stepper_z.moveTo(encoder_pos_z);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
stepper_z.run();
stepper_y.run();
////////////////////////////////////////////////////////////////////////
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState)
{
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState)
{
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH)
{
steps_per_pulse_y = 4;
steps_per_pulse_z = 4;
}
else
{
steps_per_pulse_y = 8;
steps_per_pulse_z = 2;
}
}
}
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}