I'm working to develop a code for the stepper motor to use it in a project. And since using delay() isn't as efficient as millis(). So I first developed the code that use delay with 1ms delay between the steps which is the fastest I could do with this stepper motor.
Then I developed similar code using millis() with same 1ms delay but it didn't feel as smooth/fast as the one with delay() function.
So what's the reason for that?
These are the code:
With delay():
void clockwise(void){ digitalWrite(in4, HIGH); digitalWrite(in3, LOW); digitalWrite(in2, LOW); digitalWrite(in1, LOW); delay(1); digitalWrite(in4, HIGH); digitalWrite(in3, HIGH); digitalWrite(in2, LOW); digitalWrite(in1, LOW); delay(1); digitalWrite(in4, LOW); digitalWrite(in3, HIGH); digitalWrite(in2, LOW); digitalWrite(in1, LOW); delay(1); digitalWrite(in4, LOW); digitalWrite(in3, HIGH); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); delay(1); digitalWrite(in4, LOW); digitalWrite(in3, LOW); digitalWrite(in2, HIGH); digitalWrite(in1, LOW); delay(1); digitalWrite(in4, LOW); digitalWrite(in3, LOW); digitalWrite(in2, HIGH); digitalWrite(in1, HIGH); delay(1); digitalWrite(in4, LOW); digitalWrite(in3, LOW); digitalWrite(in2, LOW); digitalWrite(in1, HIGH); delay(1); digitalWrite(in4, HIGH); digitalWrite(in3, LOW); digitalWrite(in2, LOW); digitalWrite(in1, HIGH); delay(1); }
With millis():
void stepper_run_cw(void){ stepper_step_cw(1,0,0,0,1); stepper_step_cw(1,1,0,0,1); stepper_step_cw(0,1,0,0,1); stepper_step_cw(0,1,1,0,1); stepper_step_cw(0,0,1,0,1); stepper_step_cw(0,0,1,1,1); stepper_step_cw(0,0,0,1,1); stepper_step_cw(1,0,0,1,1); } void stepper_step_cw(uint8_t m_in1, uint8_t m_in2, uint8_t m_in3, uint8_t m_in4, uint8_t delay_time){ if (!stepper_run){ digitalWrite(in1, m_in1); digitalWrite(in2, m_in2); digitalWrite(in3, m_in3); digitalWrite(in4, m_in4); stepper_start = millis(); stepper_run = 1; } if(stepper_run){ stepper_current = millis(); if(stepper_current - stepper_start >= delay_time){ stepper_run = 0; } } }
digitalWrite(in1, LOW);
five times in a row? .... you only need to run it one time .... same for the other pins ........... also, theif(stepper_run){
should be anelse
insteadelse
thanks.digitalWrite
commands has anything to do with the speed difference .... one is 4321 and the other is 1234 ................ change the order in one of them ....... also putstepper_start = millis();
at the top of theif
block