Degrees vs Stepper 28BYJ-48+ULN2003 Drive

Hey all,

I’m trying to run a stepper (28BYJ-48+ULN2003 Driver) based on a for statement. however, I’ve not been able to get what i want.

The goal is to have the stepper rotate 90 deg until it does one revolution. then stop until further instruction is given. However, this is not happening as it seems that the code cant get out of the For Loop.

Can you help me find what I’m doing wrong?

Thanks in advance,
Frederico

below the Code:

(I’ve checked using a different sketch that this stepper as Full revolution = 2038)

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

int motSpeed=10; //good speed for this stepper (max would be 15 RPM)

float Ang_Rot_Des=90; // Angular Rotation Desired in Degrees. Set here the value you want.

// Number of steps per geared output rotation
const float STEPS_PER_OUT_REV = 32 * 64;

const float step_des =(STEPS_PER_OUT_REV*Ang_Rot_Des)/360; //step for stepper based on the desired angular rotational value

Stepper myStepper(STEPS_PER_OUT_REV, 8, 10, 9, 11);

String msg1= "Start??"; // asks the user to load the preform

void setup() {
  
  // put your setup code here, to run once:
Serial.begin(9600);
myStepper.setSpeed(motSpeed); //sets the speed to the stepper

}

void loop() {
  
// lets now step i.e. rotate the desired amount

Serial.println(msg1);
while (Serial.readString()!="Y"){ 
  }
Serial.println("In Progress...");
for (float i=6; i<=STEPS_PER_OUT_REV;i=step_des){

  myStepper.step(i);
  delay(1000);
  
  }
Serial.println("completed");





}
while (Serial.readString()!="Y"){
  }

this does nothing !for (float i=6; i<=STEPS_PER_OUT_REV;i=step_des){This initializes i as 6, then compares it to the number of steps, and then assigns step_des to ‘i’ so the comparing statement will never be true and the loop just runs forever (or it only runs once, but gets initiated over and over again)
There is no condition in which the stepper is not moving.

Hey Deva_Rishi

Thanks for the fast reply!! And thanks for the tips.

What would be your suggestion to get this working ?

Ps:
I’m more concerned about the For statement rather then the Serial input ...

considering that STEPS_PER_OUT_REV is 2048 and there’s a 1 sec delay per loop iteration, it will take 30+ minutes to complete. so why a 1 sec delay?

also does mystepper.step () the specified number of steps, so that if i is a 1000, it performs 1000 steps that iteration. shouldn’t you only step (1)?

I’m more concerned about the For statement rather then the Serial input

Well i think you need to get rid of the for statement, because that is not the way to do it.

The goal is to have the stepper rotate 90 deg until it does one revolution.

what is it ? 90 deg or 360 deg.
Anyway the stepper.h is not my favorite library, because it’s rather blocking in nature, but let’s just go back to basics here

/*
 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>

const int stepsPerRevolution = 2038;  // 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, 9, 10, 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(500);

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

The Stepper One Revolution example. Does that work for you if you set the steps per revolution to 2038 ? Does it move equally far in both directions ? Do you think you can just expand on this example until you have what you want ?

Hey Deva,

Thanks.
Yes the goal is to step every 90 deg (in this case).

The tutorial works perfectly and i could achieve what i want if i step it without a for Statment.

however, the idea on using the FOR loop is to actually enable the user to introduce whatever the angle he desires and the the motor steps accordignly until it reaches a full revolution. this is better as it would allow a better "program" with higher capability.

i guess ive tried everything i can but still cant find out why it doesnt work properly with a FOR Loop....

thanks for the help again

have you considered?

void
stepToAng (
    int ang )
{
    int steps = ang * STEPS_PER_OUT_REV / 360;

    for (int i=0; i < steps; i++)  {
        myStepper.step (1);
        delay (20);
    }
}
for (uint8_t i = 0; i < 4; i++; {
  myStepper.step(stepsPerRevolution / 4);
  delay(500);
}

Is that what you want ? You know it is still not quite clear to me.
Maybe partly it is the use of the word steps ?! we are using a stepper motor, so lets use steps, just for the steps the stepper is making. Now i suspect you want the revolution to be done in chunks or parts or sections or whatever you want to call them (anything but steps ! ) and you want that done until a full revolution is complete, i think ?! And then what do you want it to do ?
And if the degrees do not add up to an exact 360 ? do you want it to stop at 360, or do you want to complete the ‘chunk’ .

The tutorial works perfectly

The example you mean ?! A tutorial is a thing with a whole explanation that people put online at instrucables and sorts. This example is part of the stepper.h library.
I am sorry if i seem a bit fussy about it, but i just want to make sure i answer the right question.

Hello gcjr and Deva

big thanks for both of you!!!

I got what i was doing wrong based on your help.

i was including i within the FOR loop and once i remove it and did it like this:

for (float i=step_des; i<=STEPS_PER_OUT_REV;i+=step_des){

  myStepper.step(step_des);
  delay(500);
  Serial.println(i);}

it worked.

however there are two additional questions that i still have to find out myself:

  • my idea on the For Statement was that he would run the condition until it was TRUE and it would occurr based on the I incremement. However, it seems that the i+ is stored “internally” and the system runs it even without i included as myStepper.step(i).
  • What to do when i don’t have the 360 deg as you mentioned Deva? how to ensure i do all the parts\chucks.

Again!! thanks for the big help!

i’ll keep doing this as of now it seems to yield what i want. once this is done i can think on improving the system and then ill contact you!!!

i’ll keep doing this as of now it seems to yield what i want. once this is done i can think on improving the system and then ill contact you!!!

Just add to this topic and i will find it or PM me with a link to a new topic. (please keep it in the forum, so others can butt-in and i can see the whole conversation (which in a PM i can not) )
I have a remark about this for (float i=step_des; i<=STEPS_PER_OUT_REV;i+=step_des)float is not a suitable variable type for a counter. Sure dividing STEPS_PER_OUT_REV by the part of the complete circle may result in fractional steps, but your stepper can not make fractional steps.
In this case the ‘<=’ in ‘i<=STEPS_PER_OUT_REV’ should ensure that the circle is complete if it adds up to 360 exactly.
Turns out you haven’t actually completed the circle (if it is 2038 for a full revolution and not 2048 as you’ve used in your program)
I would anyway use a while loop so you can do the addition before the start of the rest of the loop

#define STEPS_PER_OUT_REV 2038

const uint16_t step_des = STEPS_PER_OUT_REV / 4;

void setup() {
  Serial.begin(115200);
  Serial.println();
  uint16_t i = 0;
  while (i < STEPS_PER_OUT_REV) {
    i = i + step_des; 
    if (i > STEPS_PER_OUT_REV) {
      Serial.print("= ");
      Serial.println(step_des - (i - STEPS_PER_OUT_REV));  // this is the final result
    }
    else Serial.println(i);
    delay (1000);
  }
}

void loop() { }

Of course you should be able to get to the remaining steps using modulo (%) as well.

Actually the modulo is the way

#define STEPS_PER_OUT_REV 2038

const uint16_t step_des = STEPS_PER_OUT_REV / 4;  // step_des can be anything
                            // as an indication this is almost 90 degrees

void setup() {
  Serial.begin(115200);
  Serial.println();
  uint8_t chunks = STEPS_PER_OUT_REV / step_des;
  uint16_t modulo = STEPS_PER_OUT_REV % step_des;
  uint16_t totalsteps = 0;
  for (uint8_t i = 0; i < chunks; i++) {
    totalsteps += step_des;
    Serial.println(totalsteps);
    delay (1000);
  }
  totalsteps += modulo;
  Serial.println(totalsteps);  
}

void loop() { 

}

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