Hey guys,
I'm quite new to embedded programming and currently facing the following problem. I'm using an Arduino UNO to control a stepper motor (linear actuator) and up to ten different sensors (buttons, switches, capacity sensor...). For driving the stepper motor, I use the common driver shield A4988. The way this works for moving the stepper motor is the following:
- setting sleep Pin to LOW
- setting dir Pin to either zero or one
- the speed of the actuator is determined by the pulse length of the signal at the step Pin. So setting it to HIGH, then waiting some microsenconds and setting it back to LOW makes the stepper motor move! In code:
digitalWrite(sleepPinZ, LOW);
digitalWrite(dirPinZ, dirZ);
digitalWrite(stepPinZ, HIGH);
delayMicroseconds(500);
digitalWrite(stepPinZ, LOW);
delayMicroseconds(500);
So in each computation loop, one "step" is made. This all worked fine up to the point where I've added a lot of different sensor, which require to call digital/analogRead
during every loop and before the stepper motor makes the next step. Code:
void loop() {
// serial stuff user can stop motor ////////////////////////////////////Not important
....
//////////////////////////////////////////////////////////////////////////////////////////////
int stat1 = digitalRead(5);
int stat2 = digitalRead(6);
int stat3 = digitalRead(7);
int stat4 = digitalRead(8);
int stat5 = digitalRead(9);
int stat6 = ananlogRead(A0);
//and so on...
// if stat1 then...
// if stat2 then...
if(dirZ>-1 && !stop){
digitalWrite(sleepPinZ, LOW);
digitalWrite(dirPinZ, dirZ);
digitalWrite(stepPinZ, HIGH);
delayMicroseconds(500);
digitalWrite(stepPinZ, LOW);
delayMicroseconds(500);
}else{
digitalWrite(sleepPinZ, HIGH);
}
}
What happens is that the stepper motor does not move smoothly anymore. It is quite slow and the reason for that is pretty clear to me: Getting the status of each sensor by using digital or analog read requires some time and therefore the time between each step of the motor becomes bigger => motor moves slower. However, I definitely need to check every sensor after each step of the motor, as some of the determine the stop point of motor.
Do you have some idea how this can be solved? Of course on a multicore system I simply would use threads. On the other hand, I know the interrupt function an arduino has, however, I couldn't bring it in line with the current situation.
Sorry for not providing a minimal working example - I think it would be too much code that might confuse the reader, therefore I just displayed the important parts