Motion sensor increase Time high status via software



  • Hello!!
    I would need a little help, if possible !! I'm building a Movement node with the PIR sensor HC-SR501, and I started from the example already present on the MySensors site.
    With this sensor there is a potentiometer that allows you to adjust once passed in the HIGH state, how long it must last before returning LOW.
    What I want to do is put the potentiometer at 0 and do this work in the Code
    So every time there is a movement must communicate the new state to the gateway, stay a time of 10 seconds ON and then if the status is New LOW communicate the new state or at each movement lengthen those 10 seconds

    I don't know how to create the code, does anyone have any examples or can you help me ??



  • I have this for lighting and it is so easy.....

    Set trigger (as you said) to minimum value on pir.

    Then in code detect motion (use arduino uno pin 2 or 3 as interrupt). Then simply put a wait(time-in-milliseconds) after that.

    Here is my example....

    void loop()
    {
      //sendHeartbeat();
    	// Read digital motion value
       bool tripped = digitalRead(DIGITAL_INPUT_SENSOR);
    //	 send(msg.set(tripped?"1":"0"),true);  // Send tripped value to gw
       if (tripped == HIGH){
       send(msg.set(tripped = 0),true);
       }
       while(tripped == HIGH){
       tripped = digitalRead(DIGITAL_INPUT_SENSOR);
       wait(5000);
       }
       send(msg.set(tripped = 1),true);  
    	
    	// Sleep until interrupt on motion sensor. Send update every ten minutes.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    

    Just change wait(5000) to wait(10000).



  • I had thought of WAIT or rather DELAY, but the sketch is more complex than just the motion sensor, and using DELAY is not correct because it would block the execution of the code.
    But can't I find other solutions ???


  • Hardware Contributor

    You should better use async coding, it won't block like wait() 😉
    I did that for some old avr+analog pir nodes (custom pir state machine).

    So you simply need to use millis() + async coding. Below an example which explains how to blink a led without using any delay.
    https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

    By doing this way, your loop will never be blocked, and will run multiple times as you wish.
    So you'll need to use some vars to store time/durations. And some booleans for your pir states, time to sleep node etc.



  • @sindrome73

    I never said use 'DELAY'.

    My code does not use 'DELAY'.

    'WAIT' and 'DELAY' are not the same thing.



  • @scalz said in Motion sensor increase Time high status via software:

    o non verrà mai bloccato e verrà eseguito più volte come desideri.
    Quindi dovrai usare alcune vars per memorizzare tempo / durata. E alcuni b

    We know Millis, it's already been used several times, but what I can't solve is the code, I can't process the code !!



  • @skywatch
    WAIT and DELAY, aren't they the same thing ??
    You can explain me better, because I didn't know this difference, thanks in advance for your availability

    Searching, I found this
    link text


  • Mod

    @sindrome73 MySensors' wait() is different from Arduino's delay(). See https://www.mysensors.org/download/sensor_api_20#waiting



  • @skywatch still if you use wait(), the rest of the code in the loop is not executed during the waiting time. Only the mysensors core is executed



  • @electrik Since you haven't bothered to even post your full code, how is anyone to know what you want to do?

    You asked a specific question and I tried to help without knowing anything about your hardware, code or desired operation.



  • @skywatch said in Motion sensor increase Time high status via software:

    ti sei

    Hello and sorry, if I gave the idea of being courteous !! sorry again
    I didn't publish the code because I really don't know where to start, but now that I understand WAIT, that in MySensors and a separate thing, I also understand your example, and I thank you for helping me !!
    But then again I didn't know that this WAIT, was something different from DELAY
    I answer little, because I'm a little busy these days ..... But anyway for now thanks again for help


 

353
Online

8.4k
Users

9.3k
Topics

97.8k
Posts