Need help with having this stepper motor go clockwise 34 revolutions then go counterclockwise 34 revolutions

The stepper motor I have is a 28BYJ-48 and the driver board is a ULN2003 and I'm powering it with a rechargeble 9v battery that's 600mAh This code here is the example code that was on Amazon , and it works. But other experiments codes don't.

#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
int Steps = 0;
boolean Direction = true;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
for(int i=0; i<4096; i++){
stepper(1);
delayMicroseconds(800);
}
Direction = !Direction;
}

void stepper(int xw) {
for (int x = 0; x < xw; x++) {
switch (Steps) {
case 0:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
break;
case 2:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 3:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 4:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 5:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 6:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 7:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
SetDirection();
}
}
void SetDirection() {
if (Direction == 1) {
Steps++;
}
if (Direction == 0) {
Steps--;
}
if (Steps > 7) {
Steps = 0;
}
if (Steps < 0) {
Steps = 7;
}
}

Also this code was from the direct website of elegoo , works fine aswell.

#include <Stepper.h>

const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
const int RevolutionsPerMinute = 15;         // Adjustable range of 28BYJ-48 stepper is 0~17 rpm

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

void setup() {
  myStepper.setSpeed(RevolutionsPerMinute);
  // initialize the serial port:
  Serial.begin(9600);
}

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

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

These two codes are the only thing that works can you guys assist me on how to make this stepper motor go 34 revolutions clockwise then go counterclockwise 34 revolutions then stop? if you want the stepper motor datasheets here are they
Stepper motor basic.pdf (189.8 KB)
Stepper-Motor-28BJY-48-Datasheet.pdf (164.1 KB)
ULN2003 datasheet.pdf (809.7 KB)
Lesson 31 ULN2003A Stepper motor driver.pdf (294.6 KB)

Won't last long with any motor, relay or solenoid.
Better to replace that before debugging, or you may be chasing the wrong issues.

Then -

 myStepper.step(stepsPerRevolution  * 34);  // 34 revolutions forward
     :::
     :::
  myStepper.step(-stepsPerRevolution * 34); // 34 revolutions backeard

I can't see your code in the last post, but I'd have three states in a global bvariable.
STOPPED, FORWARD , REVERSE

Then create your forward and reverse functions separately, and when in states 1 or 2 (FWD / REV)
Call the relevant function from a switch-case block.

// set your desiredstate to 0, 1 or 2...

switch (state) {
  case 0:  // STOPPED
        break;
  case 1:  // FORWARD
        // call the forward() function
        state = 0;  // revert to stopped
        break;
  case 2: // REVERSE
        // call the reverse() function
        state = 0;  // revert to stopped
        break;
   default:
      // do nothing
}

When you want to STOP, set the state to 0
(stopped), and you can do whatever you want in the case 0: block.

Use millis() timing to call, or space out the movements instead of delay()

Once you understand this, you might #define names, or create an enum to 'name' the states rather than 0, 1 and 2

#define STOPPED 0
#define FORWARD 1
#define REVERSE 2

**-OR -**

enum stateNames {STOPPED, FORWARD, REVERSE}; // default enums start at 0

Much appreciated will practice and study until I get this down. Helped alot lastchancename

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