Blink W/O delay with a TWIST

When you use

myServo.write(angle);

You are telling the servo named myServo to move to the required angle
The name of the servo is defined when you do

Servo myServo;

and you tell the sketch which pin myServo uses with the myServo.attach(pinNumber) function

Note that write() and attach() are both preceded by the name of the servo and a full stop and that you cannot create a servo with that name without using the Servo library as in

Servo myServo;

In short, you need to use the Servo library if you want to use the commands from the library. It is not strictly necessary to use the Servo library but you really, really do not want to do it any other way with your level of knowledge and understanding of C++ and the Arduino environment

We must deduce that either one version used commands from the servo library, and did not function correctly and one version did not use them and did not function correctly…

or

the one where you didn't include servo.h did not even compile.

Which is why just saying "did not work" leaves us wandering.

It does seem unlikely that you did the servo stuff "all by yourself", so my bet is that the way in which it did not work without the library include is that it did not even get a chance to malfunction, as the code failed to compile, upload and run.

So which was it?

a7

To come back on the main question I have summarised the wanted functionality
summarised description excerpt from a posting above

Used microcontroller:

Bare bones Atmega 328P w/ 16Mhz crystal and caps, 5v supply

The microcontroller "sleeps" all day in POWER_DOWN_MODE, and the µC "wakes" up at night by monitoring a LDR & 56K divider with a threshold of 200.

WHEN the microcontroller wakes up the first thing it does is power a PIR directly from an IO-pin of the 328P,
The IO-pin delivers power to the PIR until the microcontroller goes back to sleep.

When the PIR is activated (senses a person walking by) this shall be detected by the code and start these actions

switching power on for the Adafruit FX stereo Sound Board
switching on power for the servo
Making the servo sweep 15 times to very different angles and different delays between each sweep.

These action go on for 18 seconds
After 18 seconds the power for the soundboard and the servo shall be switched of to save power.
Only the microcontroller itself stays powered on until the LDR-circuit detects "ambient light is above threshold"
which will put the microcontroller back into POWER_DOWN_MODE.

15 different servo-positions and 15 delay-times can be stored in the struct like suggested by user @UKHeliBob

steps inbetween on the way to complete functionality

taking a demo-code that does nothing more than
drive servo to position A
wait for some time
drive servo to position B
wait for some time
repeat

expanding this demo-code to use the numbers for position-angle and delaytime
stored in an array build of type struct phaseData

Taking a new demo-code that does nothing more than
switch on an LED
wait 2 seconds in a non-blocking way
switch off an LED
wait 1 second in a non-blocking way
repeat this infinitely

modifying the LED-on/off-code to only switch LED on when a button is pressed
(which simulates the PIR-signal)
and switch off LED after some seconds
and then again wait for the button-signal

combining wait for signal-code=>do action for 18 secpnds
with the switch on soundboard / sweep servo code

best regards Stefan

UKHeliBob, in comparison to other examples I have seen, studied and that have been sent to me. They make yours seem as simple and probably as stream lined as it can possibly get. I have actually grasped a hold of how it works. Of course I had to take each word and character out 1 at a time and define them individually in Arduino Speak, then put the line back together and see what that meant as a whole. But it was well worth the time spent. IT WORKS....... REALLY WELL! Quite a lesson learned. I was having problems with it doing erratic things, but that turned out to be a power supply issue. Thank you for helping me with this project, it is greatly appreciated. In addition to learning how the code works it has also broadened my knowledge unexpectedly in other areas. Unintentional, but with all the reading and searching for definitions and explanations of the code you presented, I learned a great deal of other things that are useful but don't pertain to this in any manner at all. All kinds of cool stuff that I'm sure will come in handy somewhere down the road. I also hope it is found to be helpful to others wanting to learn how to do more with a servo than make it act like a wind shield wiper. or would that be a wind screen wiper? I'm now moving on to Control Structure. Just one of the things I have a much better understanding of now!!!

Thank you again, Sir
Fred

3 Likes

I am glad that you found it useful and have learnt things that will help going forward

2 Likes

I looked it up, and it is grammatically correct in the UK.

More than likely it is because it requires dedication, devotion, persistence, patients, hours of study and a drive to learn and that magic smoke. All of which these days seems to becoming a "lost art" so to speak.

I need a pointer, please. I can set my PIR, to OUTPUT a digital signal anywhere from 2 seconds to give or take 3 (+-) minutes from the on board timer when it gets triggered by movement.
I can also do a digitalRead of the PIRs OUTPUT signal and make something happen while the signal is HIGH. These things are CHEAP and it is IMPOSSIBLE to set it to OUTPUT for a specific amount of time. Lets use 20 seconds for this example. Unless you are EXTREMELY LUCKY, and even then it can vary.

There is no point in it OUTPUTTING for 20 seconds. (it's using power) I believe it would be possible to set it to 2 seconds. And have that be a "TRIGGER" or possibly it would/could be an interrupt of sorts?

So now I'm faced with a 2 second digitalRead, but I want something to happen for 20 seconds.

There's a down side to this though. If the signal is only 2 seconds and someone else walks in front of it 3 seconds later it is going to trigger again before the 20 seconds has expired. Unless the 2nd trigger is BLOCKED until the first trigger is finished. I'm not looking for code, I'm looking for ideas, or alternative ideas. An assessment of my idea. Like, could this actually be done in the first place?

I'm also working to make my questions "specific and direct" I hope I have done a better job this time with my explanation. Thanks, Fred

1 Like

Yes one solution is a state-machine that uses the switch-case-break;-statement

As in your case the main thing is only two states

  1. waiting for PIR-signal "motion detected"
  2. do "action" for 18 seconds (and ignore PIR-Output-signals)

It can be done with a state-machine but it can be done alternatively with a boolean flag

  1. waiting for PIR-signal "motion detected" => boolean flag has value false
  • when motion is detected do two things:
  • a.) store a timestamp
  • b.) store value true in boolean flag
  1. boolean flag has value true do "action" for 18 seconds
  • check if already 18 seconds have passed by
  • when 18 seconds have passed by since timestamp
  • store value false in boolean flag

which makes the code change to state 1

Here is a WokSim of this

I have also written a tutorial about switch-case-break;-statement
state-machines

best regards Stefan

this sorta sounds like one of the automatic light switches that keeps a light on as long as there is motion in the room

but you say you want to ignore re-triggering your activity while it's active

so isn't the trigger condition to detect a trigger only while the output is not active?

for this case you could just use a delay to time the actve duration.

If you re-read the complete wanted functionality in post # 49
the complete code will do timed servo-sweeping and this requires non-blocking timing.

The WokSim is only showing that part
use 2 second triggersignal to make a 18 second action work.
best regards Stefan

if other events don't need to be recognized or performed while the servos are sweeping, why is the servo sweeping activity required to be non-blocking?

I think he thought you wanted to do delay(18000); and had this in mind

I suppose you were suggesting to do something like this

for(int i=0; i<15; i++) {
  servo.write(random(0, 181);
  delay(random(3000, 20000);
}

ie bundle the delay after each movement

(but a bit more complex if you want really 15 movement with a random delay in between and a total time of 15s)

The requirement is to move the servo to several different but defined angles for defined periods rather than random positions for random times

Note too that the requirement that the servo movements do not block the sketch seems to come and go in this topic. I for one do not have a firm grasp of the requirements and it seems to me that the topic has not advanced for the last 30 or so posts, maybe more

@Fredric58 please clarify exactly what you want to do and how much of it you have already achieved

1 Like

Should just be assumed! If it comes and goes, embrace it when it's here and craft a solution.

Non-blocking code can always be made to act like blocking code. Not so much the other way 'round.

So why not just proceed on the assumption that sooner later, making everything non-blocking will prove to have been a reasonable investment.

a7

See reply #15 in this topic, which seems a lifetime ago now

I guess the OP has left the bulding already.

Please take a look at the below Ladder Logic for your reference,
Where:
PIR: Passive InfraRed
INH: INHibit
TON: Timer ON
MD: Motion Detected
DN: DoNe
RY: RelaY

Ladder Logic

                                    _______________
         PIR          INH          |      TON      |           MD
---------| |----------|/|----------|IN  2000mS  OUT|----------( )
                                   |_______________|

          MD           DN                                     INH
---------| |----------|/|-------------------------------------( )
                 | 
         INH     | 
---------| |-----

                                    _______________
         INH                       |      TON      |           DN
---------| |-----------------------|IN  20000mS OUT|----------( )
                                   |_______________|


         INH                                                   RY
---------| |--------------------------------------------------( )

Structured Text
MD := MD_TON(PIR AND NOT INH) ;
IHN := (INH OR MD) AND NOT DN ;
DN := INH_TON(INH) ;
RY := INH ;

μB

1 Like

Thank you, I thought I presented a, to the point, concise question. I got distracted and forgot to push send when I got your comment to thank you for the pointer. I have a basic understanding as I researched state machines and switch state while I was taking UKs code apart to see what made his code tick. Found a garage door open state sketch that made it even easier to understand. And Thank you for post #63. Uhmmmm, I mentioned it before, I post a question and others get into a pi**ing contest about NOT the question that was asked. That's partially why the thread is so long. I think. I can tell you actually took the time to read it. Thanks,

Thank you as well for the input.

That is correct as I have explained. Don't want to use delay.

Because there are other events as noted

The time between sweeps is specific, ALL the timing in this is specific. From the servo to the audio tracks, sleep time, wake time.

It's building, and no I haven't left. I asked a question, post# 53 I believe, I got a real answer from Delta_G. I am simply off studying it to see if, and I think it will be applicable for my project. That's if you were to have read the question.

This thread is CLOSED! However, after a deep dive into state machines and or switch case the OP will be opening a new thread called Freds Project in the near future. Where a discussion will take place about blending all of the individual and sometimes complicated sketches that work individually into one magnificent work of art.

Thank you for your participation and input. See you soon. Best Regards, Fred

You can ask a moderator to really close the thread so nobody can add comments in this thread anymore. Though I don't know if a moderator will do it in your case. I think easiest way is to contact a moderator is to use the flag-symbol below the postings and then write a short message.

perhaps that new thread (???) can specify the requirements, correctly, completely and unambiguous in the first post

1 Like

I´m curious.

Why?