problem with big stepper motor speed control

hello people , i am new guy in the arduino world and i have stuck on big stepper control speed with potensiometer , the slow speed is achieved but i can get more the half at the higher , here the code, i dont know if i have missed something , i apologise in advance if my post does not follow the forum requirements....

// Defines pins numbers
const int stepPin = 6;
const int dirPin = 7;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023,20,2000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}

but as far i set the delay time manually the motor runs slow and high max speed

// Defines pins numbers
const int stepPin = 6;
const int dirPin = 7;
//int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {
//customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(20);
digitalWrite(stepPin, LOW);
delayMicroseconds(20);
}
// Function for reading the Potentiometer
//int speedUp() {
//int customDelay = analogRead(A0); // Reads the potentiometer
// int newCustom = map(customDelay, 0, 1023,20,2000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (20 to 2000)
//return newCustom;
//}

Need to see datasheets for motor AND driver.

the slow speed is achieved but i can get more the half at the higher

Can you explain that more precisely - what are you expected to see, v. what you actually see.

Here my nema 24 stepper motor and driver, in my potentiometer test code , the motor can not get the high revs same as when i run the simplest code where i set the delay value manually , but the slow speeds can be achieved,

MarkT:
Can you explain that more precisely - what are you expected to see, v. what you actually see.

I test my motor by setting the delay values 2000 for slow and 20 for high speed and runs ok, speeds are achieved , but the code with the potentiometer can't give me the high revs as the manual test, i checked the monitor print values snd the mapping works fine , 2000 to 20 , but the motor doesn't response to the 20 same as manually , i can say the motor achieves half top speed revs

How is your pot wired up?

What values are you getting from the analogRead?

I suspect this line is what is responsible for your results:

[color=#222222]int customDelay = analogRead(A0); // Reads the potentiometer[/color]

I think analogRead is a very slow instruction.
Maybe only read it every 64th time through the loop?

Well you clearly have a servomotor, not a stepper motor!

Anyway the issue is that its using the same step/direction interface as a stepper motor would,
which needs a high rate of pulses for a high speed. You are calling analogRead() which takes a
long time on ATmega microcontrollers - 110µs or so, limiting the pulse rate.

Your management of time is poor too - using delayMicroseconds() instead of micros(), so that
the code execution time eats into your performance.

Perhaps try replacing

void loop() {
  customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin, LOW);
  delayMicroseconds(customDelayMapped);
}

by

void loop() {
  static unsigned long last_read= 0 ;
  if (millis() - last_read >= 20)   // only read pot occasionally
  {
    last_read += 20 ;
    customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
  }

  // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
  static unsigned long last_step = 0 ;
  if (micros() - last_step >= customDelayMapped)
  {
    last_step += customDelayMapped ;  // schedule next pulse without being affected by code execution time
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(stepPin, LOW);
  }
}

MarkT:
Well you clearly have a servomotor, not a stepper motor!

Anyway the issue is that its using the same step/direction interface as a stepper motor would,
which needs a high rate of pulses for a high speed. You are calling analogRead() which takes a
long time on ATmega microcontrollers - 110µs or so, limiting the pulse rate.

Your management of time is poor too - using delayMicroseconds() instead of micros(), so that
the code execution time eats into your performance.

Perhaps try replacing

void loop() {

customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
 // Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
 digitalWrite(stepPin, HIGH);
 delayMicroseconds(customDelayMapped);
 digitalWrite(stepPin, LOW);
 delayMicroseconds(customDelayMapped);
}



by


void loop() {
 static unsigned long last_read= 0 ;
 if (millis() - last_read >= 20)   // only read pot occasionally
 {
   last_read += 20 ;
   customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
 }

// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
 static unsigned long last_step = 0 ;
 if (micros() - last_step >= customDelayMapped)
 {
   last_step += customDelayMapped ;  // schedule next pulse without being affected by code execution time
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(stepPin, LOW);
 }
}

Thanks MarkT for youryour suggestion , it works fine , low revs high revs , ....i am mechanical engineer , i must stydy deeply the electronics, thanks for your valuable help, thank all the guys offered their help

http://www.xinhuanet.com/culture/2018-01/08/c_1122227025.htm?from=timelin

Here some youtube videos about my work on robotics animatronics

I suspect this line is what is responsible for your results:
I

[color=#222222]int customDelay = analogRead(A0); // Reads the potentiometer[/color]

I think analogRead is a very slow instruction.
Maybe only read it every 64th time through the loop?

mayyortom:
I suspect this line is what is responsible for your results:

[color=#222222]int customDelay = analogRead(A0); // Reads the potentiometer[/color]

I think analogRead is a very slow instruction.
Maybe only read it every 64th time through the loop?

mayyortom:
I suspect this line is what is responsible for your results:

[color=#222222]int customDelay = analogRead(A0); // Reads the potentiometer[/color]

I think analogRead is a very slow instruction.
Maybe only read it every 64th time through the loop?

wildbill:
How is your pot wired up?

What values are you getting from the analogRead?
The wiring is same as most of cases , GND , U+ 5 to arduino , COM to A0, and when i print monitor i got the mapped values, 20, 2000 and the low revs are achieved fine, same as the manual test

vpanagiotou:
Thanks MarkT for youryour suggestion , it works fine , low revs high revs , ....i am mechanical engineer , i must stydy deeply the electronics, thanks for your valuable help, thank all the guys offered their help
https://youtu.be/m7gl5Y-FEk8
https://youtu.be/DfIHu8HsOTc
https://youtu.be/TzNOG7NmIJU
https://youtu.be/K4Ygm8msBtQ
http://www.xinhuanet.com/culture/2018-01/08/c_1122227025.htm?from=timelin
http://mp.weixin.qq.com/s/TR2lxFo-r6vintqYi1Pklg
Here some youtube videos about my work on robotics animatronics

Hi, again , i builta code to contor my closed servo motor with potensiometer and limit switch , but my motor runs slow , i dont know how to compile your suggestion to my application, my mechanical world seems far away from electronic world , so i need a help to sort it out

const int driverPUL = 6;    // PUL- pin
const int driverDIR = 7;    // DIR- pin
const int spd = A0;     // Potentiometer
const int limit1 = A1;
const int limit2 = A2;
// Variables
int slv;
int pd = 30;
void setup() {
Serial.begin(9600);
pinMode (driverPUL, OUTPUT);
pinMode (driverDIR, OUTPUT);
pinMode (limit1,INPUT);
pinMode (limit2,INPUT);
}
void loop()
{
slv=analogRead(spd);
if (slv > 575)
{
if (!digitalRead(limit1)){}
else
{ pd = map(slv,575,1023,2000,30);
digitalWrite(driverDIR,HIGH);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
}
if (slv < 350)
{
if (!digitalRead(limit2)){}
else
{ pd = map(slv,350,0,2000,30);
digitalWrite(driverDIR,LOW);
digitalWrite(driverPUL,HIGH);
delayMicroseconds(pd);
digitalWrite(driverPUL,LOW);
delayMicroseconds(pd);
}
}
}

I need help to compile your time delay suggestion in previous messages

// Defines pins numbers
const int stepPin = 6;
const int dirPin = 7;
int speedUp();
int customDelayMapped ;
int customDelay ;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop ()
{
static unsigned long last_read = 0;
if (millis() - last_read >= 20)
{
last_read += 20 ;
customDelayMapped = speedUp();
}
static unsigned long last_step = 0;
if (micros() - last_step >= customDelayMapped)
{
last_step += customDelayMapped ;
digitalWrite(stepPin,HIGH);
delayMicroseconds(5) ;
digitalWrite(stepPin, LOW);
}
}
int speedUp()
{
int customDelay = analogRead(A0);
int newCustom = map(customDelay , 0, 1023, 20 ,2000);
return newCustom ;
}

Thanks in advance

That compiles without errors for me.

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