I'm running a stepper motor with easydriver. I'm using a potentiometer and analogRead to set the delay between the steps so i can control the speed.
Below is the code i'm using right now (sorry, there's some stuff in portuguese but i'll explain it)
I'm trying to set a speed for analogRead values between 0 and 250, another speed for values between 250 and 500, and so forth.
It's all running smoothly except for when it reads below 100 or above 1000. What happens is that it runs a little bit faster when its <100 than when it's between 100 and 250, and a little bit slower when its over 1000 than when its between 750 and 1000.
What I suspect is that it has to do with the number of bits being read. I.e, the code runs a little bit faster when it reads only 2 numbers and a little bit slower when it reads 4. Is that a possibility? Any ideas on how to get around this?
//ler potenciômetro e determinar velocidade
valorpotenciometro = analogRead (potenciometro);
if (valorpotenciometro<250){
delayvel = 5000; //velocidade rapida
digitalWrite(m1, HIGH); //microstep 1/8 de passo
digitalWrite(m2, HIGH);
Serial.println (valorpotenciometro);
retorno =0;
}else if(valorpotenciometro >250 & valorpotenciometro<500){
delayvel = 10000; // velocidade media
digitalWrite(m1, HIGH);
digitalWrite(m2, HIGH);
Serial.println (valorpotenciometro);
retorno = 0;
}else if(valorpotenciometro>500 & valorpotenciometro<750){
delayvel = 20000; // velocidade lenta
digitalWrite(m1, HIGH);
digitalWrite(m2, HIGH);
Serial.println (valorpotenciometro);
retorno = 0;
}else if (valorpotenciometro>750){
delayvel = 2;
digitalWrite (m1, LOW); //microstep meio passo
digitalWrite (m2, LOW);
Serial.println (valorpotenciometro);
retorno = 1;
}
rhasenack:
What I suspect is that it has to do with the number of bits being read. I.e, the code runs a little bit faster when it reads only 2 numbers and a little bit slower when it reads 4. Is that a possibility?
You need to post a complete program so we can see how the variables are defined etc.
Your code for causing a step seems very complex. Have a look at the code in the 2nd example in this Simple Stepper Code. To change the speed all that is needed is to change the value of millisBetweenSteps. If you need higher speeds you could change that to microsBetweenSteps.
Checking time with millis() and micros() should always use subtraction so as not to be affected when the value rolls-over.
Robin2:
Your code for causing a step seems very complex. Have a look at the code in the 2nd example in this Simple Stepper Code. To change the speed all that is needed is to change the value of millisBetweenSteps. If you need higher speeds you could change that to microsBetweenSteps.
Checking time with millis() and micros() should always use subtraction so as not to be affected when the value rolls-over.