Hi everyone, new to this forum and to the Arduino platform. I would really appreciate some help with a little code.
I want to keep this simple so I can learn but I am not sure how to format the code in order to add additional functionality.
As it sits, I have an adafruit stepper which is controlled by a rotary encoder... this functions great and the sketch seems to be working great. The rotary encoder allows me to jog 1 step at a time on the stepper... so far so good.
I wanted to add a momentary switch on a digital input so I can have the stepper run continuously at a faster speed until it is depressed so I don't have to jog a large distance with the rotary encoder.
Can someone help me with the formatting of the code... I gave it a couple tries and both broke my sketch every time. Not sure where to add it in the void setup and loop, what how it needs to be defined so it does not cause conflict with the existing function.
Here is what it looks like so far...
#include <AccelStepper.h>
#include <AFMotor.h>
#define encoderPinA 2
#define encoderPinB 3
int encoderPos = 0;
int encoderPinALast = LOW;
int encoderPinBLast = LOW;
int n = LOW;
AF_Stepper motor2(200, 2);
// wrappers for the motor! SINGLE is default, can also use INTERLEAVE DOUBLE MICROSTEP
void forwardstep2() {
motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {
motor2.onestep(BACKWARD, SINGLE);
}
// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper2(forwardstep2, backwardstep2);
void setup()
{
stepper2.setMaxSpeed(500.0);
stepper2.setAcceleration(100.0);
stepper2.moveTo(0);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
}
void loop()
{
n = digitalRead(encoderPinA);
if ((encoderPinALast != n )) {
if (((n == HIGH) && (digitalRead(encoderPinB) == LOW)) ||
((n == LOW) && (digitalRead(encoderPinB) == HIGH))) {
motor2.step(1, BACKWARD, INTERLEAVE);
} else {
motor2.step(1, FORWARD, INTERLEAVE);
}
} else {
n = digitalRead(encoderPinB);
if ((encoderPinBLast != n )) {
if (((n == HIGH) && (digitalRead(encoderPinA) == LOW)) ||
((n == LOW) && (digitalRead(encoderPinA) == HIGH))) {
motor2.step(1, FORWARD, INTERLEAVE);
} else {
motor2.step(1, BACKWARD, INTERLEAVE);
}
}
}
encoderPinALast = digitalRead(encoderPinA);
encoderPinBLast = digitalRead(encoderPinB);
}