I'm using an Arduino with an ultrasonic sensor to operate a belt conveyor and an actuator. The belt conveyor brings a part, when it's close enough the actuator moves it to a seperate conveyor, comes back and a delay starts to wait for the other conveyor to clear.
My problem is that I need to maintain similar spacing on the 2nd conveyor belt but depending on how loaded the 1st conveyor is, speed changes. If I could get the 1st conveyor to move in position while the delay is running instead of after that would fix it, but I don't know if it's possible.
Unfortunately you can't run code during a delay. But the behaviour you want can be achieved using some logic and the millis()
function. Here is some sample code:
int delay = 500;
void loop(){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= delay) {
previousMillis = currentMillis;
//run the second conveyor at delay
}
//run first conveyor logic
}
Check out this article on arduino.cc for more info.