Help needed for stepper motor project

Im new to programming Arduino. I try to adjust codes for the stepper motor based on examples that I can find on the internet.

My goal is to:
1 turn on Arduino with toggle switch
2 press button (push button)
3 stepper motor turns 45 degrees and stops
4 press button (push button)
5 stepper motor turns 45 degrees and stops

Basically when the toggle switch is turned on every time the button is pushed the stepper should turn 45 degrees.

I have a working setup with:
-Arduino Board
-Stepper motor 28bjy-48
-Stepper driver board

By working I mean I am able to wire this thing up correctly and make the stepper turn with an example code from the internet.

Could someone please help me out here with a example code how to do this?
That would be very much appreciated!

Start by reading stepper motor basics. Read existing material about how to do with buttons. There is no example code doing what You want.

I'm not sure that my Stepper Motor Basics is all that useful for someone with a 28BYJ stepper motor. However there must be dozens of Forum Threads about them.

fredje15:
By working I mean I am able to wire this thing up correctly and make the stepper turn with an example code from the internet.

Post the program you have and tell us what you want it to do that it does not already do?

What is the project you want to implement? If you just want back-and-forth movement through 45° either side maybe a servo would be more suitable.

With a stepper motor you need to run some code at startup to make it go to the HOME or ZERO position..

...R

Many thanks for your reply. So please find code below, this is perfect for my project.
Now I need to execute the movement with a push button. After it turns 512 steps (45 degrees) it needs hold position and with next push button click it needs to do another 512 steps (45 degrees)

This is my test setup https://imgur.com/a/VAL7soC

#include <Stepper.h>

const int stepsPerRevolution = 512;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(5000);

 
}

fredje15:
Many thanks for your reply. So please find code below, this is perfect for my project.
Now I need to execute the movement with a push button. After it turns 512 steps (45 degrees) it needs hold position and with next push button click it needs to do another 512 steps (45 degrees)

If you just want it to do another 5i2 steps in the same direction you could add these few lines marked ----New

#include <Stepper.h>

const int stepsPerRevolution = 512;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

byte buttonPin = 6; // ----NEW

void setup() {
  // set the speed at 60 rpm:
  
  pinMode(buttonPin, INPUT_PULLUP);  // ----New
  
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  
  while (digitalRead(buttonPin == HIGH) {  // ----New
  } // ----NEW

 
}

Wire the switch so it connects pin 6 to GND when pressed.

...R

Thank you so much for your suggestion. I tried it but it did not work for me. I will try again later. Meanwhile I found a code from youtube which is working after some tweaks! Im so happy.

Only thing is that it works perfectly fine with power from laptop over USB but when I disconnect the USB and power the Arduino with 9V battery It turns ones and then the motor gets stuck. Like it tries to move but it is just buzzing. Any ideas what this could be caused by and perhaps fixed?

Btw the code is:

/*
  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 = 8;

// 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(2000);
  }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){
    ;
    isOpen = !isOpen;
  }
}

}

IF it's working with computer USB, then that means your code and circuitry is fine. The problem's with the battery, so I would suggest you to change the battery. Buy a 12V Battery, power up your Arduino from Black Power Jack and I think it will do the trick.

You should also check the Grounds of your circuit. The ground of Arduino, Stepper Motor and its driver should be same.

Also share your circuit then we can suggest better.

Changing the 9v battery worked! Thanks man much appreciated.
From what I understand the stepper motor is constantly consuming power when the Arduino is on.
I can see the leds on the stepper motor driver on (2 of them)

fredje15:
Thank you so much for your suggestion. I tried it but it did not work for me.

Without seeing the actual code that YOU tried I cannot help. The phrase "did not work" does not convey any useful information.

The program in Reply #5 seems to have much the same elements as in my suggestion.

...R