My project works, except my stepper motors run forward one revolution, then backward one revolution, once at the beginning of the sketch. I've included my code. It's not well written code, I know it needs revision, but can anyone help me figure out where I'm going wrong as far as the forward/backward once at startup? My intent is to have the steppers move forward one revolution when the button is pressed once, then backward when the same button is pressed again. Otherwise the steppers should be not moving and disabled.
#include <AccelStepper.h>
#include <MultiStepper.h>
const int REVOLUTIONS = 1; // number of times the wand turns
const int CYCLE = 2048;
//AccelStepper stepper(Accelblind_1::FULL4WIRE, 0, 2, 1, 3); // 0, 1, 2, 3 ATTiny85
AccelStepper blind_1(AccelStepper::FULL4WIRE, 9, 11, 10, 12); // 9, 10, 11, 12 Uno Pro Trinket
AccelStepper blind_2(AccelStepper::FULL4WIRE, A0, A2, A1, A3); // A0, A1, A2, A3 Uno Pro Trinket
AccelStepper blind_3(AccelStepper::FULL4WIRE, 8, 6, 7, 5); // 8, 6, 5, 4 Uno Pro Trinket
// Up to 10 steppers can be handled as a group by MultiStepper
MultiStepper steppers;
long positions[3]; // Array of desired stepper positions
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void checkButton() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
void Open(){
positions[0] = CYCLE*REVOLUTIONS;
positions[1] = CYCLE*REVOLUTIONS;
positions[2] = CYCLE*REVOLUTIONS;
steppers.moveTo(positions);
steppers.runSpeedToPosition(); // Blocks until all are in position
blind_1.disableOutputs();
blind_2.disableOutputs();
blind_3.disableOutputs();
}
void Close(){
positions[0] = 0;
positions[1] = 0;
positions[2] = 0;
steppers.moveTo(positions);
steppers.runSpeedToPosition(); // Blocks until all are in position
blind_1.disableOutputs();
blind_2.disableOutputs();
blind_3.disableOutputs();
}
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
// Configure each stepper
blind_1.setMaxSpeed(500);
blind_2.setMaxSpeed(500);
blind_3.setMaxSpeed(500);
// Then give them to MultiStepper to manage
steppers.addStepper(blind_1);
steppers.addStepper(blind_2);
steppers.addStepper(blind_3);
}
void loop() {
checkButton();
switch (buttonPushCounter) {
case 0:
// do nothing
break;
case 1:
digitalWrite(ledPin, HIGH);
Serial.println("Opening Blinds");
Open();
buttonPushCounter = 2;
break;
case 2:
// do nothing
break;
case 3:
digitalWrite(ledPin, LOW);
Serial.println("Closing Blinds");
Close();
//reset buttonPushCounter
buttonPushCounter = 0;
break;
}
}