Hi All,
I made the in-out action as a stepper motor using the Encoder library and Accelstepper.
I'm a newbie in coding, so the code is just using Exsample, and it's very simple.
The encoder works fine, but the motor gets malfunctioning when saving and recall in-out positions.
It seems to lose its position after returning to the loop statement.
Another concern is that the Easybutton library uses the function name "read", which is same with the Encoder library.
Could you help me or some advice for good coding?
I made the in-out action as a stepper motor using the Encoder library and Accelstepper.
I'm a newbie in coding, so the code is just using Exsample, and it's very simple.
The encoder works fine, but the motor gets malfunctioning when saving and recall in-out positions.
It seems to lose its position after returning to the loop statement.
Another concern is that the Easybutton library uses the function name "read", which is same with the Encoder library.
Could you help me or some advice for good coding?
- Coding Environment: Arduino IDE
- Encoder used: 200cpl rotary encoder.
- Board: SAMD21mini (to be moved to Teensy 3.2 or LC)
- Motor: Nema17 Bipolar 1.7 degree
Code:
#include <Encoder.h>
#include <EasyButton.h>
#include <AccelStepper.h>
#define BTN1 5
#define BTN2 6
Encoder Enc(12, 13);
AccelStepper stepper1(1, 10, 11);
EasyButton button1(BTN1);
EasyButton button2(BTN2);
long XInPoint = 0;
long XOutPoint = 0;
short scale = 4;
void setup() {
button1.begin();
button2.begin();
button1.onPressed(onButton1Pressed);
button2.onPressed(onButton2Pressed);
button1.onPressedFor(1000, onPressed1ForDuration);
button2.onPressedFor(1000, onPressed2ForDuration);
stepper1.setMaxSpeed(3000);
stepper1.setAcceleration(6000);
}
long oP = -999;
void loop() {
button1.read();
button2.read();
///////////////////////////////////////////////////////////////////// Encoder
long nP = Enc.read();
if (nP != oP) {
oP = nP;
stepper1.moveTo(nP * scale);
}
stepper1.run();
}
////////////////////////////////////////////////////////////// Button1.(Pressed)
void onButton1Pressed()
{
stepper1.moveTo(XInPoint);
stepper1.runToPosition();
}
////////////////////////////////////////////////////////////// Button2.(Pressed)
void onButton2Pressed()
{
stepper1.moveTo(XOutPoint);
stepper1.runToPosition();
}
///////////////////////////////////////////////////////////// Button1.(Duration)
void onPressed1ForDuration()
{
long xi = Enc.read();
XInPoint = xi;
}
//////////////////////////////////////////////////////////// Button2.(Duration)
void onPressed2ForDuration()
{
long xo = Enc.read();
XOutPoint = xo;
}