Delay one action based on a second action

I am working on a Sip & Puff control for a radio controlled car using a UNO board. I started with the sketch that Bob Paradiso had already made. I’ve made some modifications to that sketch for my hardware and application. There is a pressure switch that measures +/- 1 psi. To control the car you give 2 quick puffs, by mouth, into the pressure switch and the car goes forward, 2 quick Sips and it goes reverse. A single sustained puff turns right, single sustained sip turns left.

The problem is when you do the 2 puffs to go forward the steering also does 2 quick rights returning to center between. Same with 2 sips to go reverse I get 2 lefts. I want to add a delay so the steering does not jerk back and forth while doing the 2 quick sip/puffs.

I’ve tried a couple of things. First I set a variable (SteeringPause) to a 1 when the sip/puff starts and put that as a “AND” condition in the steering control If function. Setting the variable back to 0 when the direction was set. The other thing I tried was a time delay (SteeringPauseStart) subtracting "millis() and comparing that to a set delay (SteeringPauseDelay) putting that in my if condition as well. I’ve tried a number of variations of these and I can get it so it either doesn’t work at all or I get no steering at all.

I’ve attached both my sketch and a wiring diagram. Not everything on the wiring is used in this sketch. I have another sketch that controls the car via a joystick. I did get that one to work properly.

All suggestions, comments and criticisms welcomed


Arduino UNO Interface.pdf (141 KB)

sipPuffToyCar_Mod.ino (9.63 KB)

I start reading code by looking at what setup() and loop() do. You've completely subverted the idea of having two functions by sticking a while loop in setup() and doing nothing in loop().

When you stop that crap, I'll look at the code again.

Actually that's not my crap. As I mentioned I got the original sketch off of the forum from someone else. That was how he did it and it works. I did send him a message asking him about that and he said it was just his way of doing it, programmers prerogative. I didn't want to go rearranging something that was working, I agree that it would be better, more correct, if it was all in the loop function. I saw no need to change something that was working. Do you think it would work better if I did put it in the loop function?

Sorry if it confuses you

I have move the While loop that was in the setup function and placed it in the loop function. As PaulS pointed out that is not the proper way to write the code. The revised code is attached.

I also want to thank PaulS for taking the time to look into my problem.


sipPuffToyCar_Mod_2.ino (8.78 KB)

sipStarted should be a boolean, with values true or false.

puffStarted should be a boolean, with values true or false.

Actually, neither variable is needed. You know when a sip started or when a puff started. If the start time for either action is non-zero, then sipStarted is true. So, you can see that sipStarted is redundant. The same holds true for puffStarted.

Just be sure to set the start times to 0 where you currently set sipStarted and puffStarted to false.

The real problem, though, is that you take action (call setSteering()) as soon as a sip or puff starts, not when the sip or puff or double sip or double puff ends.

Thanks for the help PaulS that fixed it. I had a delay in there but I had it after the call to setSteering had already started. I changed it to before setSteering was called and that did the trick.

I also changed the variables that should be Booleans to Boolean. I see your point about using sipStarted and puffStarted as being redundant as I know that based on the SENSOR_PIN being below or above the thresholds. Although it does work they are not needed and I'll have to clean it up.

Thanks again for your help John