the function delay() is often seen in the Arduino-examples.
This guides newcomers in the wrong direction. As soon as you want to have something with more complexity that an single LED switch On/Off function delay() is just delaying finishing your project.
The much better suited alternativ to function delay is non-blocking timing
Non-blocking timing enables to do multiple things in "parallel".
But this is a different programming-paradigma.
You have to think very different than with using the function delay()
The basics are:
-
all looping is done by function void loop() itself
-
it requires to code all functions to
- quickly jump in do a small step
then - jump out because -- while----: void loop is really looping fast the next "jump in" will be done just 0,05 seconds in the future
void loop()
jump in myfuntion1 (quickly jump out)
jump in myfuntion2 (quickly jump out)
jump in myfuntion3 (quickly jump out)
...
So all defined functions are executed in sequence but it seems to be executed in parallel because each function proceeds just a small step and then jumps out
well "knowing" executing my next step will occur 0,15 seconds in the future.
Executing a certain sequence of steps is done by using a coding-technique called state-machine.
You can think of a state-machine like a machine that has different operation-modes.
as an example I use a laser-printer
if you switch on the laser-printer he will run through different operation-modes:
Operationmode 1: selftest
Operationmode 2: heating up
Operationmode 3: awaiting print-jobs
Operationmode 4: executing a print-job
In each mode the laserprinter does just this one thing
selftest or heating up or awaiting print-jobs or executing a print-job
Well some printers will be able to "receive" a second print-job while the first is not yet finished. (Doing things in parallel just we plan to do)
But I think you get the principle.
which operation-mode is "active" is controlled by using a state-variable.
After power-on the state is set to selftest
if selftest has finished set operation-mode to heating up
if heating up has finished set operation-mode to await printing-jobs
if a printing-job is received set operation-mode to print-this printing-job
if printing-job has finished set operation-mode to await printing-jobs
In your case the operation-modes are:
mode 1: wait for PIR-signal "active" if signal is detected set mode to 2
mode 2: set new servo-position "switch flipped" immidiately set mode 3
mode 3: wait for servo having reached this servo-position
check servo-position (with a read-command)
if servo has reached new position set mode 4
mode 4: set servo-position to start-position immidiately set mode 5
mode 5: wait for servo to reach start-position
check servo-position through a read-command
if servo has reached start-position set mode 1
one cycle has ran through
This is the concept. I'm sure you have a big question-mark inside your head
"sounds good but how do I code that?????
It is important to have an overview about the concept and then diving into the coding...
Quite some stuff to learn
here is a demo-code and explanation about non-blocking timing
and here is a demo-code how a state-machine is coded in principle. You have to adapt the switch-case-break-statement to your needs
Be the change you want to see in the world
best regards Stefan