non blocking code to blocking code on 28BYJ-48 Stepper Motor and AccelStepper

Hi all.
I found the code bellow 28BYJ-48 Stepper Motor with ULN2003 driver and Arduino Uno – 42 Bots
It works brilliantly with my stepper motor in terms of consistency and speed. But it’s written in non-blocking code. I’m looking to add digital buttons into the mix and for the motor to turn, I need to have blocking code. I was wondering if it’s possible to modify the bellow code?

I’ve found some other snippets that use modified version of the Arduino Stepper, but they’re really not very good with this particular motor. Stutter-y and weird.

Any help greatly appreciated

.

#include <AccelStepper.h>
#define HALFSTEP 8

// Motor pin definitions
#define motorPin1 3 // IN1 on the ULN2003 driver 1
#define motorPin2 4 // IN2 on the ULN2003 driver 1
#define motorPin3 5 // IN3 on the ULN2003 driver 1
#define motorPin4 6 // IN4 on the ULN2003 driver 1

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup() {
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);
stepper1.moveTo(20000);

}//–(end setup )—

void loop() {

//Change direction when the stepper reaches the target position
if (stepper1.distanceToGo() == 0) {
stepper1.moveTo(-stepper1.currentPosition());
}
stepper1.run();
}

tcmspark:
But it's written in non-blocking code. I'm looking to add digital buttons into the mix and for the motor to turn, I need to have blocking code.

You need non-blocking code so you can read your buttons while the stepper moves. And as far as I can see you are correct to say the code is non-blocking.

Just add a function call into loop() before stepper.run(). For example readButtons();

And add a function

void readButtons() {
   // code to read buttons and save the value
}

...R

Like this (Robin's code):

  // simple program that makes stepper move up or down depending on which button is pressed

unsigned int microsBetweenSteps = 2000;
byte buttonUpPin = 10;
byte buttonDownPin = 11;
byte dirPin = 5;
byte stepPin = 4;

byte buttonUp = HIGH;
byte buttonDown = HIGH;

void setup() {
  pinMode(buttonUpPin, INPUT_PULLUP);
  pinMode(buttonDownPin, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  readButtons();
  moveMotor();
}

void readButtons() {
  buttonUp = digitalRead(buttonUpPin);
  buttonDown = digitalRead(buttonDownPin);
}

void moveMotor() {
  if (buttonUp == LOW) {
     motorStep('F');
  }
  if (buttonDown == LOW) {
     motorStep('R');
  }
}

void motorStep(char dirn) {
  if (dirn == 'F') {
     digitalWrite(dirPin, HIGH);
  }
  else {
     digitalWrite(dirPin, LOW);
  }

  digitalWrite(stepPin, HIGH);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(microsBetweenSteps);
}

raschemmel:
Like this (Robin’s code):

Sorry @Raschemmel, but that code won’t work with a ULN2003 driver for a unipolar stepper. Accelstepper is good for the heavy lifting with those things.

…R

You mean like this ?

  /* 
 Stepper Motor Control - one revolution
 
 This program drives a unipolar or bipolar stepper motor. 
 The motor is attached to digital pins 8 - 11 of the Arduino.
 
 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.  
 
  
 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe
 
 */

#include <Stepper.h>
//Motor A
 int enableA = 5;
 int IN1 = 8;
 int IN2 = 9;
//Motor B
 int enableB = 6;
 int IN3 = 12;
 int IN4 = 7;
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                                     // for your motor
unsigned int microsBetweenSteps = 2000;
byte buttonUpPin = 10;
byte buttonDownPin = 11;
byte dirPin = 5;
byte stepPin = 4;

byte buttonUp = HIGH;
byte buttonDown = HIGH;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, IN1,IN2,IN3,IN4);            

void setup() 
{
   Serial.begin(9600);
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
 
 pinMode (enableA, OUTPUT);
 pinMode (IN1, OUTPUT);
 pinMode (IN2, OUTPUT);
 pinMode (enableB, OUTPUT);
 pinMode (IN3, OUTPUT);
 pinMode (IN4, OUTPUT);
 digitalWrite (enableA, HIGH);
 digitalWrite (enableB, HIGH);
}

void loop() 
{
   readButtons();
 }

void readButtons() 
{
   buttonUp = digitalRead(buttonUpPin);
   
  if (buttonUp == LOW) 
    {
   
    // step one revolution  in one direction:
    Serial.println("clockwise");
    myStepper.step(stepsPerRevolution);
   
    }
  
  buttonDown = digitalRead(buttonDownPin);
    
  if (buttonDown == LOW) 
  {
   // step one revolution in the other direction:
   Serial.println("counterclockwise");
   myStepper.step(-stepsPerRevolution);
 
  }
}

(CHANGE THE PIN NUMBERS FOR YOUR WIRING)