Faster speed microstep code

Hi I am fairly new to Arduino code and have made a camera slider using the code from a YouTube project https://youtu.be/6Mp4lrz7Vc0?feature=shared It does everything I need it but I would like to increase the speed and have tried changing the values in the following section: -
(Speed_Delay = map(read_ADC, 0, 1023, 8000, 10); //value map for Microstep resolution Delay)
But it doesn't change. I have had a search around for how to do so and I think that I may not be able to without changing part of the code. If anyone could offer some help I would be very grateful.

If poss I would like to make the potentiometer have a wide range from very slow to high speed etc.

I am using a nema 17 Act Motor with an a4988 and a Nano board

Here is the code I am using:-

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define potentiometer  A0  //10k Variable Resistor
#define bt_F A1 // Clockwise Button
#define bt_S A2 // Stop Button
#define bt_B A3 // Anticlockwise Button

#define dirPin  8  //8Pin  of Arduino--Direction of stepper motor driver 
#define stepPin 9  //9Pin  of Arduino--Step of stepper motor driver 
#define enPin   10 //10Pin of Arduino--Enabled of stepper motor driver  

int read_ADC;
int Speed_LCD, Speed_Delay;
int Mode=1, flag=0;

void setup() { // put your setup code here, to run once
  
pinMode(potentiometer, INPUT); // declare potentiometer as input 

pinMode(bt_F, INPUT_PULLUP); // declare bt_F as input
pinMode(bt_S, INPUT_PULLUP); // declare bt_S as input
pinMode(bt_B, INPUT_PULLUP); // declare bt_B as input

pinMode(dirPin,  OUTPUT); // declare as output for Direction of stepper motor driver 
pinMode(stepPin, OUTPUT); // declare as output for Step of stepper motor driver 
pinMode(enPin,   OUTPUT); // declare as output for Enabled of stepper motor driver 
  
lcd.begin(16,2);  
lcd.setCursor(0,0);
lcd.print(" WELCOME TO  MY ");
lcd.setCursor(0,1);
lcd.print(" CAMERA SLIDER");
delay(2000); // Waiting for a while
lcd.clear();
}

void loop() { 

read_ADC = analogRead(potentiometer); // read analogue to digital value 0 to 1023 
Speed_Delay = map(read_ADC, 0, 1023, 8000, 10); //value map for Microstep resolution Delay
Speed_LCD = map(read_ADC, 0, 1023, 0, 100); //value map to Display on the LCD

lcd.setCursor(0,0);
lcd.print("   Speed: ");
lcd.print(Speed_LCD); 
lcd.print("%  ");

if(digitalRead (bt_F) == 0){Mode = 2; digitalWrite(enPin, LOW);} //For Clockwise

if(digitalRead (bt_S) == 0){ //For Stop
if(flag==0){flag=1;
 if(Mode>1)Mode=1; 
      else{Mode=!Mode; 
      if(Mode==0)digitalWrite(enPin, HIGH);
            else digitalWrite(enPin, LOW);
      }
delay(100);
 }
}else{flag=0;} 

if(digitalRead (bt_B) == 0){Mode = 3; digitalWrite(enPin, LOW);} //For Anticlockwise

lcd.setCursor(0,1);

     if(Mode==0)lcd.print("      Free      ");
else if(Mode==1)lcd.print("      Stop      ");
else if(Mode==2)lcd.print("     > Right >  ");
else if(Mode==3)lcd.print("     < Left <   ");

if(Speed_LCD>0 && Mode>1){ 
   
     if(Mode==2)digitalWrite(dirPin, LOW);// Stepper motor rotates CW (Clockwise)
           else digitalWrite(dirPin, HIGH);// Stepper motor rotates CCW (Anticlockwise)

digitalWrite(stepPin, HIGH);
delayMicroseconds(Speed_Delay);
digitalWrite(stepPin, LOW);
delayMicroseconds(Speed_Delay);
}  

}

I moved your topic to an appropriate forum category @sophiekatie123.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

To find out why not, put in some Serial.print() statements to check the values, and whether they change as expected.

e.g. after this line:

Speed_Delay = map(read_ADC, 0, 1023, 8000, 10); //value map for Microstep resolution Delay

add:

Serial.print("delay value ");
Serial.println(Speed_Delay);

As long as you are not using acceleration, the stepper speed will be limited. A stepper can't go from 0 to several thousand steps per second instantaneously. Use a library like AccelStepper or MobaTools if you want high speeds. I prefer MobaTools.

Or you can write your own stepper code with acceleration. See Increase Steppe motor speed - #8 by Robin2

You create only one step per loop cycle. And you do a lot in your loop - especially many lcd prints - what slows down your loop. This defines the speed much more than your Speed_Delay.

Static characters should be printed at the end of setup().

lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Speed:");
lcd.setCursor(14, 0);
lcd.print("% ");

Variables should only be printed when they change.
Leo..

Hi as mentioned I a pretty new to the Arduino code. I have tried to put a couple of codes together to try and achive what im after but I am struggling as I don't really know what im doing code wise but I am good with the electronics side of things.

This is what I have done so far: I have made a motorized camera slider with a Nema 17 (initially with an a4899 driver but have now changed it to a tmc2208 which is quieter) and used a set up
(which I found on YouTube) and it works exactly as I would like it to except is is too slow even when the potentiometer is full up.

I have tried changing the values in the speed delay but it doesn't go any faster.

I have seen other codes that work with a potentiometer but don't have all the functions that I would like ie:-
potentiometer (to control the speed of the sider)
direction change ( via a microswitch and button)
stop button
lcd display (to show motor speed, but not essential)

I would be good if I could just adapt the code to setup I have as I have assembled and soldered it all on to a pcb board but again not essential as I may have to put it all in a bigger box to allow for a fan to cool the heatsink on the tmc2208 as it is pretty hot (also would also be grateful for advice on whether a fan needed)

I will try and upload a jpeg of the circuit diagram shortly as I cant see how to do so on here at the moment.

If anyone could post a code that would suit or amend mine to make it faster I would be very grateful

Many thank in advance

here is the code

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define potentiometer  A0  //10k Variable Resistor
#define bt_F A1 // Clockwise Button
#define bt_S A2 // Stop Button
#define bt_B A3 // Anticlockwise Button

#define dirPin  8  //8Pin  of Arduino--Direction of stepper motor driver 
#define stepPin 9  //9Pin  of Arduino--Step of stepper motor driver 
#define enPin   10 //10Pin of Arduino--Enabled of stepper motor driver  

int read_ADC;
int Speed_LCD, Speed_Delay;
int Mode=1, flag=0;

void setup() { // put your setup code here, to run once
  
pinMode(potentiometer, INPUT); // declare potentiometer as input 

pinMode(bt_F, INPUT_PULLUP); // declare bt_F as input
pinMode(bt_S, INPUT_PULLUP); // declare bt_S as input
pinMode(bt_B, INPUT_PULLUP); // declare bt_B as input

pinMode(dirPin,  OUTPUT); // declare as output for Direction of stepper motor driver 
pinMode(stepPin, OUTPUT); // declare as output for Step of stepper motor driver 
pinMode(enPin,   OUTPUT); // declare as output for Enabled of stepper motor driver 
  
lcd.begin(16,2);  
lcd.setCursor(0,0);
lcd.print(" WELCOME TO  MY ");
lcd.setCursor(0,1);
lcd.print(" CAMERA SLIDER");
delay(2000); // Waiting for a while
lcd.clear();
}

void loop() { 

read_ADC = analogRead(potentiometer); // read analogue to digital value 0 to 1023 
Speed_Delay = map(read_ADC, 0, 1023, 5000, 10); //value map for Microstep resolution Delay
Speed_LCD = map(read_ADC, 0, 1023, 0, 100); //value map to Display on the LCD

lcd.setCursor(0,0);
lcd.print("   Speed: ");
lcd.print(Speed_LCD); 
lcd.print("%  ");

if(digitalRead (bt_F) == 0){Mode = 2; digitalWrite(enPin, LOW);} //For Clockwise

if(digitalRead (bt_S) == 0){ //For Stop
if(flag==0){flag=1;
 if(Mode>1)Mode=1; 
      else{Mode=!Mode; 
      if(Mode==0)digitalWrite(enPin, HIGH);
            else digitalWrite(enPin, LOW);
      }
delay(100);
 }
}else{flag=0;} 

if(digitalRead (bt_B) == 0){Mode = 3; digitalWrite(enPin, LOW);} //For Anticlockwise

lcd.setCursor(0,1);

     if(Mode==0)lcd.print("      Free      ");
else if(Mode==1)lcd.print("      Stop      ");
else if(Mode==2)lcd.print("     > Right >  ");
else if(Mode==3)lcd.print("     < Left <   ");

if(Speed_LCD>0 && Mode>1){ 
   
     if(Mode==2)digitalWrite(dirPin, LOW);// Stepper motor rotates CW (Clockwise)
           else digitalWrite(dirPin, HIGH);// Stepper motor rotates CCW (Anticlockwise)

digitalWrite(stepPin, HIGH);
delayMicroseconds(Speed_Delay);
digitalWrite(stepPin, LOW);
delayMicroseconds(Speed_Delay);
}  

}


here is the youtube link which has the circuit diagram and build details

Hi Im not sure what you mean by "You create only one step per loop cycle" I must admit I know very little about coding is there a way I could get a little more speed by using the code i am using or is it a case of starting with another code.

Thanks

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