Adding delays between operations

Hi everyone. I built a machine thats moves are controlled by tripping position switches. The problem I am having is that whenever I add a delay(3000) it only adds to the amount of time before the program realizeses it is in the correct position to start the next move. In other words it makes it so the position switch needs to be held for three seconds before the program is allowed to move forward. Where should I add delays so that the machine pauses before it moves to the next move. All the things in the code labeled "xFor1" are outs to relays and the position switches should be self evident. They are on two axis x and y. There are four on the y axis. Any help would be great thanks !!

if (digitalRead(prog4) == HIGH) {program4();}

void program4() { while (digitalRead(xLimit) != HIGH) xPass1();

while (digitalRead(yPos2) != HIGH) yMove1();

while (digitalRead(xHome) != HIGH) xPass2();

while (digitalRead(yPos3) != HIGH) yMove2();

while (digitalRead(xLimit) != HIGH) xPass1();

while (digitalRead(yPos4) != HIGH) yMove1();

while (digitalRead(xHome) != HIGH) xPass2();

while (digitalRead(xHome) != LOW) allStop(); }

void xPass1() { digitalWrite(yFor1, LOW); digitalWrite(air, HIGH); delay(0); digitalWrite(xFor1, HIGH);

}

void xPass2() { digitalWrite(yFor1, LOW); delay(0); digitalWrite(air, HIGH); delay(0); digitalWrite(xRev0, HIGH); delay(20); digitalWrite(xRev1, HIGH); }

void yMove1() { digitalWrite(xFor1, LOW); digitalWrite(air, LOW); delay(0); digitalWrite(yFor1, HIGH); }

void yMove2() { digitalWrite(xRev1, LOW); digitalWrite(air, LOW); delay(20); digitalWrite(xRev0, LOW); delay(0); digitalWrite(yFor1, HIGH); }

void allStop() { digitalWrite(xRev0, LOW); digitalWrite(xRev1, LOW); digitalWrite(xFor1, LOW); digitalWrite(yRev0, LOW); digitalWrite(yRev1, LOW); digitalWrite(yFor1, LOW); digitalWrite(air, LOW); }

The problem I am having is that whenever I add a delay(3000) it only adds to the amount of time before the program realizeses it is in the correct position to start the next move

Blink without delay. Tutorial. Without delay. Look at it.

(please post code using the # icon on the toolbar)

Ok. Thanks

I don't think that solves my problem cause the delay is dependent on the rest of the program.

I don't think that solves my problem cause the delay is dependent on the rest of the program.

So write your code in a way that events are based on the amount of time has passed. This means building a simple state machine, but eliminates the use of "delay()".

That's an idea but the program works very well aside from a simple "stop doing anything for a few seconds" here and there command. Should this be so difficult?

Should this be so difficult?

It is NOT difficult. It simply requires a different approach. You need to think about how YOU would do the task(s).

If you need to do something on a periodic basis, you can perform the task, set a timer for the required wait period, and not do a thing until the timer goes off. When the timer goes off, you perform the task, set a timer for the required wait period, and not do a thing until the timer goes off. Repeat forever.

Or, you could perform the task, note the time, and do other stuff. Periodically, you check to see if it is time to perform the task again. If it is, you perform the task, note the time, and do other stuff. If not, you just do other stuff.

The first method is what the delay() function is all about. The second is what the blink without delay example is all about.

I get what you're saying but the machine has to work with higher precision than that. Is there really no way to say "stop everything for a few seconds" between "xMov1" and "yMov1"? Without sacrificing the programs ability to see where the machine is?

the machine has to work with higher precision than that

Higher precision than what?

Is there really no way to say "stop everything for a few seconds" between "xMov1" and "yMov1"? Without sacrificing the programs ability to see where the machine is?

Just think about it, how would this be coded? It's not on. So no.

You could always use interrupts and interrupt service routines for handling inputs while you have your dead time delay.

Mostly for the y axis moves. It needs to move exactly 12 inches to work properly.

Like posting only snippets of code, posting snippets of answers doesn't help. Why don't you tell us what you want to do, and why you think you can't use the blink without delay tutorial as a template. Unless whatever it is move extremely fast, I don't see any problem.

What are you trying to achieve with the delay? If you just want to stop for three seconds between each move, you can do this:

void program4()
{
 while (digitalRead(xLimit) != HIGH)
 xPass1();
 allStop();
 delay(3000);

 while (digitalRead(yPos2) != HIGH)
 yMove1();

But it's not a very elegant way of doing it. I'll add another voice to the chorus of state machine, blink without delay.

Edit:spelling

I don't think that solves my problem cause the delay

What is it that causes the delay?

thanks WildBill you come through for me again. Thanks everyone else for your input as well, I don't think I explained the problem as well as I could have because I was posting from my phone and it made things a little bit more tricky. But again thanks for all your help :) !!!