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
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
Thanks.