Stepper Motors 28BYJ-48, ULN2003a driver, Arduino Nano

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;
  }
}

Also, I am powering the steppers/drivers from a 9VDC wall wort.

I figured it out. I was using an esp-01 as the button input (buttonPin) to an arduino nano. The esp-01 pin I used was also the TX pin, So when I was using the esp-01 to start wifi communication I was toggling that pin, which caused the steppers to move on startup. Sometimes, I just have to say "DUH", and slow down.

Consider this post closed.

Maybe you can mark it as solved?

I think I did it right.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.