stepper.setSpeed (float)

Hello,
I automated my telescope with stepper motors and the role of one of them is to make the telescope rotate as fast as the stars move in the sky (as we see it) with the equatorial mount.
The speed of my motor is 1, but it's too fast, and I can't put a smaller value because as it has to be a float, and the setSpeed() function requires an int (0.8 is like 0, and 5.4 is like 5).

Any idea how I could reduce the speed of the motor without making it null.
I guess I have to reprogram the setSpeed() function...

Thanks in advance for helping me !

Hugo.T

You need to post your program.

And please post your code between [code] [/code] tags so your code looks like thisand is easy to copy to a text editor

...R

There it is (note that there is another motor called ‘myStepper’ that controls the focus of the telescope using a remote control. The motor I’m having trouble with (‘stepper1’) is always on):

#include <IRremote.h>
#include <Stepper.h>

int pin = 3;
IRrecv receiver(pin);
decode_results decode_ir;

const int stepsPerRevolution = 600;
const int steps_per_revolution = 30;
Stepper myStepper(steps_per_revolution, 4,5,6,7);
Stepper stepper1(stepsPerRevolution, 8,9,10,11);

void setup() {
  Serial.begin(9600);
  receiver.enableIRIn();
  myStepper.setSpeed(100);
  stepper1.setSpeed(1);
}

void loop() {
  stepper1.step(-1);
   if(receiver.decode(&decode_ir)){
      Serial.println(decode_ir.value, DEC);
      receiver.resume();
        if(decode_ir.value == 3772794553){
        myStepper.step(-steps_per_revolution);
        }
        else if(decode_ir.value == 3772819033){
         myStepper.step(steps_per_revolution);
         }       
    } 
}

If this is too fast

stepper1.setSpeed(1);

I think you need to use the more comprehensive AccelStepper library.

...R

Thanks, Indeed I once used it for another project, but didn't know it could solve my problem.
Actually, I just modified the Stepper library and set the setSpeed() function's parameter to a float.
Thanks a lot anyway for helping me!

Hello,

I have the same issue as you. My question is how you edited the library code to set the value to float?

Thanks a lot in advance!

Max

MaxPower123:
My question is how you edited the library code to set the value to float?

What library?

The AccelStepper library uses floating point maths as standard.

...R

Thanks for your fast answer!

I meant the stepper-library.

Yes, AccelStepper would be a better library, but the problem is, that I want to change the speed (float value necessary) during the stepper-motor is running. And I've already read, that it is not possible with AccelStepper library.
So the only way is to use stepper.library with adjusted .setSpeed (int to float).

But I don't know how to change that?

Thanks a lot for help!
Max

If you are using a stepper driver that takes step and direction signals you can easily control it without any library. Have a look at this Simple Stepper Code

...R
Stepper Motor Basics

No, unfortunately I use the L298n.

Thanks for the link, you posted.
But just let me know, is there a way to change the library sourcecode, so I can control the motorspeed with high accuracy?

Thanks in advance!
Max

MaxPower123:
But just let me know, is there a way to change the library sourcecode

It is certainly possible but I have never tried to do it myself so I can't tell you how.

Do you really need to use floating point maths? At bottom an Arduino only works with integers so, no matter what you do it will eventually be converted to integers before anything physical happens.

What calculation do you need to do that you cannot do with integer maths?

If you are going to change the Stepper library code may I suggest that you do that in a copy of the library to which you have given a new name so you can still use the standard version. Giving it a new name will also greatly reduce confusion if you want to ask for advice here.

...R

Maybe it is better to give a greater view to my topic:

I want to control a stepper motor with a HM-10 (BLE-Module), so I can control the motor with a android app, I already have made. The only thing I send from the smartphone to the arduino is a string, who contains the speed (with two decimals). The only issue I have to solve, is to run the stepper at this speed and when I change the speed on the smartphone, that it also changes the speed. I’ve already tried it with the AccelStepper library, but there was the problem, that I could not run the motor without give him a number of Steps he has to run. But I want to run the speed as long as I don’t change the speed per smartphone. So I think the stepper library is better for that, but there is the problem with the int value of setSpeed method…
Maybe you can help me

Here is the code with accelStepper:

<#include <AccelStepper.h>

float drehzahl =0.00;
int incomingByte;
int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
AccelStepper Motor(AccelStepper::FULL4WIRE, 11, 10, 9, 8);


void setup() {
 Serial.begin(9600);
 Motor.setAcceleration(200);
 Motor.setMaxSpeed(100);
 
}

void loop() {
 Motor.setSpeed(3.333*drehzahl);
 Motor.runSpeed();
 if (Serial.available() > 0) {
// read the incoming byte:
drehzahl = Serial.parseFloat();
if(drehzahl > 0.02 && drehzahl <100) {


Serial.print(" I received:");
Serial.println(drehzahl);



 } 
 else{
   float acceptedDiff = 0.0001;
   if (fabsf(drehzahl-0.01) < acceptedDiff) {
     Serial.print(" I received:");
     Serial.println("0.00");
   } else {
   return;
   }
 }
 float acceptedDiff  = 0.0001;
 if(fabsf(drehzahl-0.01) < acceptedDiff) {
   Motor.stop();
   //Motor stoppt!
 }
 }

  
}

Thanks a lot!
Max

MaxPower123:
but there is the problem with the int value of setSpeed method....

Please explain why that is a problem before I spend time looking at the AccelStepper code.

Give some examples of the values you want to send to the Arduino and how you want the Arduino to deal with them.

...R

That is a problem because I need a good accuracy of the speed (2 decimals)

For example:

Motor stands at the beginning and when i type in the speed value (example: 5.87 rpm) and press the run button in the app the stepper starts running with 5.87rpm, and when I change the speed value in the app ( example: 7,38rpm) and press the button in the app again the stepper runs with the new speed (7.38 rpm)
And then i have a button which stops the motor (0.00rpm)

I hope it is clear.

Thanks a lot!
Max

MaxPower123:
Motor stands at the beginning and when i type in the speed value (example: 5.87 rpm) and press the run button in the app the stepper starts running with 5.87rpm,
I hope it is clear.

Sorry, it is not clear.

You say that you can type in 5.87 rpm and then the motor runs at 5.87 rpm. Isn't it doing what you want?

This is all taking so long that it seems to me it would make sense to get a specialized stepper driver that takes step and direction signals and write your own code using my examples to get started.
Of course you could create a couple of functions called step() and direction() that make the L298 move one step and in the selected direction.

Isn't the simple solution to get the Stepper library to do one step each time and then call that at whatever interval you want? Just work out how many millisecs or microsecs are needed between each step to get the desired RPM.

...R

Sorry the example is the desire.

Actually the motor steps just one step (with the right speed when I press the button in the app. I can do that as often as I want. But I want the motor to run continously with the selected speed. So i press the button and the motor runs as long as I don’t change the speed. That is the reason why i prefer the stepper.library with the float value of setSpeed, as I mentioned before.

I’m sorry, that I am so unprecise.

Thanks Robin for your support!
Max

MaxPower123:
So i press the button and the motor runs as long as I don't change the speed. That is the reason why i prefer the stepper.library with the float value of setSpeed, as I mentioned before.

You can achieve what you want by writing code that gets a single step to happen at regular intervals. Something like this

void loop() {
   if (millis() - previousStepMillis >= stepIntervalMillis ) {
        previousStepMillis += stepIntervalMillis;
        myStepper.step(1);
   }
   // other code
}

and you can change the speed any time you like by changing the value of stepIntervalMillis

...R

Thanks for your answer Robin!

So the only thing I have to check is the relation between the stepIntervallMillis and the rpm, right?

Max

MaxPower123:
So the only thing I have to check is the relation between the stepIntervallMillis and the rpm, right?

Yes.

...R

PS ... We will both probably regret such a simple question and its answer.

Hugo_Trombert:
Thanks, Indeed I once used it for another project, but didn't know it could solve my problem.
Actually, I just modified the Stepper library and set the setSpeed() function's parameter to a float.
Thanks a lot anyway for helping me!

I also need to set the rpm variable to less than 1rpm, so I need it to be float.
How did you edit the stepper.h library?
I opened it with WordPad and changed:
// speed setter method:
void setSpeed (long whatSpeed);
for:
// speed setter method:
void setSpeed (float whatSpeed);

but it gives error. It does not accept float type. How did you do that? Thanks.