CNC Shield,steppers and potentionmeters [Advice]

Best regards,

First thing to say i have zero knowledge in programing,but in couple of days of searching and reading i managed to get my code to work but it is not that stable. So i need advice :slight_smile:

In my very first project im using 2 pots to control speed of 2 steppers independently.When i start code steppers start to rotate at min speed and when i use pots to change speed at some point steppers start to act weird like they are out of sycn and when i continue increase speed they seems to be fine but when they reach max speed they start to block themself and run completely crazy.

In this project im using:
Two NEMA 17 1.68A 2.8V 1.8degree (KS42STH48-168A)
CNC Shield V3 (red one)
Two Potentionmeters 50k ohm
24v power supply
Arduino uno

Here is my code:

byte enablePin = 8;           //Enable for both steppes
byte directionPinM1 = 5;      //Direction pin Stepepr M1
byte stepPinM1 = 2;           //Step pin Stepepr M1
byte directionPinM2 = 6;      //Direction pin Stepepr M2
byte stepPinM2 = 3;           //Step pin Stepepr M2



int potPinM1 = A4;            //Speed control Stepper M1
int potPinM2 = A5;            //Speed control Stepper M2


unsigned long curMiros;
unsigned long prevStepMicrosM1 = 0;      //Previous Step Micros Stepper M1
unsigned long prevStepMicrosM2 = 0;     //Previous Step Micros Stepper M2
unsigned long microsBetweenStepsM1;      //Micros Between Stepps Stepper M1
unsigned long microsBetweenStepsM2;     ////Micros Between Stepps Stepper M2

void setup() {

     Serial.begin(9600);

     
     pinMode(directionPinM1, OUTPUT);
     pinMode(stepPinM1, OUTPUT);
     pinMode(enablePin,OUTPUT);
     pinMode(directionPinM2,OUTPUT);
     pinMode(stepPinM2,OUTPUT);
     
     pinMode(potPinM1, INPUT);
     pinMode(potPinM2, INPUT);
     
     digitalWrite(enablePin, HIGH);
     digitalWrite(directionPinM1, HIGH);
     digitalWrite(directionPinM2, HIGH);






for (int x=1; x<1000; x=x+1){
       
            
      int delayTime = 500;
    
      digitalWrite(stepPinM1, HIGH);              //Tried to use this code to prevent blocking of steppers 
      delayMicroseconds(delayTime);               //when system is powered up and pots are on random position
      digitalWrite(stepPinM1, LOW);
      delayMicroseconds(delayTime);

      digitalWrite(stepPinM2, HIGH);
      delayMicroseconds(delayTime);
      digitalWrite(stepPinM2, LOW);
      delayMicroseconds(delayTime);
}



    



}

void loop() {
   curMiros = micros();
    stepperM1();
    stepperM2();
   
}

void stepperM1() {
   
        potPinM1=analogRead(A4);
        microsBetweenStepsM1 =map(potPinM1,0,1023,500,3000);

    if (curMiros - prevStepMicrosM1 >= microsBetweenStepsM1) {


        prevStepMicrosM1 = curMiros;
        digitalWrite(stepPinM1, HIGH);
        digitalWrite(stepPinM1, LOW);
        
    
    }
   }

void stepperM2() {



        potPinM2=analogRead(A5);
         microsBetweenStepsM2 =map(potPinM2,0,1023,500,3000);

    if (curMiros - prevStepMicrosM2 >= microsBetweenStepsM2) {


        prevStepMicrosM2 = curMiros;
        digitalWrite(stepPinM2, HIGH);
        digitalWrite(stepPinM2, LOW);
        
   
}


}

When im using delay ()function it seem to work perfectly but i cant use it.
Tried also with millis() but i got really strange working there and i didnt like sound they are making.

I would be grateful if some one can give some advice or point me where i should start diging again :slight_smile:

Thanks.

This code looks OK

    potPinM2=analogRead(A5);
    microsBetweenStepsM2 =map(potPinM2,0,1023,500,3000);
    if (curMiros - prevStepMicrosM2 >= microsBetweenStepsM2) {
        prevStepMicrosM2 = curMiros;
        digitalWrite(stepPinM2, HIGH);
        digitalWrite(stepPinM2, LOW);
    }

If you gradually increase the speed it will be same as writing code to make the motor accelerate. However it is likely that the motor won't be able to jump from 0 to a high speed without accelerating.

Also there will be a limit to the top speed of the motor that depends, among other things, on the power supply voltage and the setting of the current limit on the stepper motor driver.

Note how I have tidied up the layout of your code. Using the AutoFormat tool makes code much easier to read. And please don't include unnecessary white space.

And in this code

potPinM2=analogRead(A5);

the variable name potPinM2 seems particulary inappropriate. To my mind it should be the name for the pin A5 - perhaps like this

potM2Value = analogRead(potPinM2);

Using meaningful variable names makes it much easier to spot errors.

And generally it is not a good idea to include "magic numbers" such as the pin number (A5 in this case) because if you need to change anything you have to find all the occurences and change them or there will be an error that can be very difficult to find. Putting, for example, byte potPinM2 = A5; at the top of your program means {A} you don't have to remember what A5 is used for and {B} if you want to change to A3 you just need to make a single change.

...R

This is not a simple project for a first timer. You should get the Arduino cookbook by Michael Margolis and read it. Also on line there are many videos etc showing the Arduino and how to do what you want. This additional information will go a long way in getting your problem solved.