Button activated Stepper sketch

Good evening everyone,

I'm very new at this and I’ve read the tutorials and got the hang of basic stuff, like editing pin inputs, outputs , but when it comes to creating a sketch that will do what i need, i’m lost.
I've read the basic tutorials and got to a point where i need a sketch that will perform a very basic function (so i believed), so here is a breakdown of where I’m at:

Equipment being used:
1 x Uno
1 x 28BYJ-48 Stepper
1 x ULN2003 stepper driver board
1 x Button (5v pin , 10Kh on gnd pin, output pin 2)
(i will add the other button once i've figured out how to make 1 work!)

Functions Required:
2 x button inputs to move the stepper from pos A-B wait until called and then back pos B-A
I like the AccelStepper.h library for the acceleration and deceleration commands such as

stepper1.setMaxSpeed
stepper1.setAcceleration
stepper1.setSpeed

So I would ideally like the stepper to start off slow and then speeding up before gradually slowing down.
I’ve tried to combine different sketches (because I’m no good at writing them) to get what i need but I’ve been going round in circles.

i have attached a the two sketches i've been trying to combine

Can someone please point me in the right direction?

Many thanks,
Mark

1 BUTTON TEST switch ok.txt (2.04 KB)

8 turn gate stepper.txt (1.16 KB)

@markcra, please include short programs in your post (like this) so people don't have to download them.

/*
  Stepper Motor Demonstration 1
  StepperMotor.ino
  Demonstrates 28BYJ-48 Unipolar Stepper with ULN2003A Driver
  Uses Arduino Stepper Library

  BluIcedPen 2018
  https://www.linkedin.com/in/blueicedpen/
*/

//Include the Arduino Stepper Library
#include <Stepper.h>

// Define Constants

// Number of steps per internal motor revolution 
const float STEPS_PER_REVOLUTION = 32; 

//  Amount of Gear Reduction
const float GEAR_REDUCTION = 64;

// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = STEPS_PER_REVOLUTION * GEAR_REDUCTION;

// Define Variables

// Number of Steps Required
int StepsRequired;
int switchPin = 7;
//Extra Boolean variables to keep track of button and motor state
boolean current = LOW;
boolean last = LOW;
boolean isOpen = LOW;

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// The pins used are 8,9,10,11 
// Connected to ULN2003 Motor Driver In1, In2, In3, In4 
// Pins entered in sequence 1-3-2-4 for proper step sequencing

Stepper steppermotor(STEPS_PER_REVOLUTION, 8, 10, 9, 11);

void setup()
{
// Nothing  (Stepper Library sets pins as outputs)
  pinMode(switchPin, INPUT);
}
//Corrects push button bounce (not the best debounce function)
boolean debounce(boolean inLast){
  boolean inCurrent = digitalRead(switchPin);
  if(inLast != current){
    delay(5);
    inCurrent = digitalRead(switchPin);
  }
  return inCurrent;
}

void garageAction(float factor){
  // Slow - 4-step CW sequence to observe lights on driver board
  
   // Rotate CW 1/2 turn slowly
  StepsRequired  =  STEPS_PER_OUT_REV*factor; 
  steppermotor.setSpeed(1000);   
  steppermotor.step(StepsRequired);
  if(isOpen == LOW){
    delay(10000);
  }else if(isOpen == HIGH){
    delay(5);
  }
}

void loop()
{
  current = debounce(last);
  if(current == HIGH && last == LOW && isOpen == LOW){
    garageAction(2);
    isOpen = !isOpen;
  }
  if(current == HIGH && last == LOW && isOpen == HIGH){
    garageAction(-2);
    isOpen = !isOpen;
  }
}
#include <AccelStepper.h>  
  
#define HALFSTEP 8  
  
#define motorPin1  8     // IN1 on ULN2003 ==> Blue   on 28BYJ-48  
#define motorPin2  9     // IN2 on ULN2004 ==> Pink   on 28BYJ-48  
#define motorPin3  10    // IN3 on ULN2003 ==> Yellow on 28BYJ-48  
#define motorPin4  11    // IN4 on ULN2003 ==> Orange on 28BYJ-48  
  
int endPoint = 32770;    // Move this many steps - 1024 = approx 1/4 turn  
  
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ-48  
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);  
  
void setup()  
{  
  Serial.begin(9600);  
  Serial.println(stepper1.currentPosition());  
  delay(5000);  
  stepper1.setMaxSpeed(6000.0);  
  stepper1.setAcceleration(100.0);  
  stepper1.setSpeed(200);  
  stepper1.moveTo(endPoint);  
}  
  
void loop()  
{  
  //Change direction at the limits  
    if (stepper1.distanceToGo() == 0)  
   {  
     Serial.println(stepper1.currentPosition());  
     stepper1.setCurrentPosition(0);  
     endPoint = -endPoint;  
     stepper1.moveTo(endPoint);  
     Serial.println(stepper1.currentPosition());  
   }  
    stepper1.run();  
}

...R

I'm not at all clear what you want help with. Is it the case that the first program works properly and you just want to convert if to use the AccelStepper library?

...R

I wish to combine both sketches to have slow movement via button control if that makes sense?

markcra:
I wish to combine both sketches to have slow movement via button control if that makes sense?

I'm not convinced that that is the correct solution.

I know that the first program does not use acceleration, but does it otherwise do what you want? If not please describe in detail what it actually does and what you want it to do that is different (apart from the acceleration).

When we know what needs to happen adding the acceleration should be straightforward.

...R

Good evening, what i want is a Stepper motor to power a gate on my model railway, so i want to be able to open and close the gate (hence 2 buttons) the stepper motor has to turn aprox 8 times (32770 steps) to go from the open position to closed, i would like the motor to run variable speed, i.e. slow to start then faster then a gradual decrease in speed.

Does that make sense?

markcra:
Does that make sense?

Yes that makes sense, but it does not answer my question about whether the first program does what you want apart from not having acceleration. What actually happens when you run the first program?

Or is it the case that the first program is completely irrelevant and I should ignore it?

...R

After a lot of experimenting i've cracked the button activation accel stepper motor, it quite simple actually, just a lot of work getting the scripts to run correctly , here is the sketch:

#include <Stepper.h>
#include <AccelStepper.h>
#include <Pushbutton.h> //include Pushbutton library
// Motor pin definitions for Arduino
#define motorPin1 8 // IN1 on the ULN2003 driver
#define motorPin2 9 // IN2 on the ULN2003 driver
#define motorPin3 10 // IN3 on the ULN2003 driver
#define motorPin4 11 // IN4 on the ULN2003 driver

// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
#define HALFSTEP 8

// Define the buttons pins
#define BUTTON1 3 //button1 pin
#define BUTTON2 4 //button2 pin

// Define the buttons names
Pushbutton pbutton1(BUTTON1); //create button1
Pushbutton pbutton2(BUTTON2); //create button2

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

void setup()
{
// Set the maximum steps per second:
Serial.begin(9600); //initialise serial port
// Setup motor speed and acceleration
stepper.setMaxSpeed(1400);
stepper.setAcceleration(70.0);
}

void loop() {
if (pbutton1.getSingleDebouncedPress()){ //if button1 is pressed
Serial.println("Button1"); //serial monitor shows button3 pressed
Motoropen();
Serial.println("Open");
// Delay before power off
delay(500);
//Power off stepper board
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
if (pbutton2.getSingleDebouncedPress()){ //if button2 is pressed
Serial.println("Button2"); //serial monitor shows button2 pressed
Motorclose();
Serial.println("close");
// Delay before power off
delay(500);
//Power off stepper board
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
}
void Motoropen(){
stepper.runToNewPosition(0);
}
void Motorclose(){
//Adjust for endstop position
stepper.runToNewPosition(-32768);
}

Regards,
Mark