blinking led

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

blinking led

#1 Post by pw444 » 01 Jan 2021, 15:28

Hya,

i want to include a blinking led routine in the rules but am unsure how to make it, because all is related to events.

The idea - when idle, led blinking 30Hz, when working, blinking 60Hz.

I appreciate any help.

Thx!

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: blinking led

#2 Post by Ath » 01 Jan 2021, 17:01

You could have a look at the LoopTimerSet_ms command, where an exact repeating timer can be started at a pretty high (ms) resolution.
As the human eye can't (consciously) see the difference 30 and 60 Hz I assume you mean 0.5Hz and 1Hz (and a higher than 1Hz frequency is quite stressful for an ESP8266), I mocked this script:

Code: Select all

// Dummy Device name=State, type=Dual, values=Active,Value, interval=1
on State#Active do
  if %v1%!=%eventvalue%
    LoopTimerSet_ms,1,0 // Stop timer
    if %eventvalue%=1
      TaskValueSet,3,2,500 // on/off time
    elseif %eventvalue%=0
      TaskValueSet,3,2,1000 // on/off time
    else
      TaskValueSet,3,2,0  // Turn off
    endif
    LoopTimerSet_ms,1,[State#Value]
    let,1,%eventvalue%
  endif
endon

on Rules#Timer=1 do
  if [Led#State]=1 // Switch on GPIO-2 = On-board led
    gpio,2,0
  else
    gpio,2,1
  endif
  delay,0 // Avoid crashes because of the high frequency of processing rules
endon
It expects 2 tasks, a Dummy Device with 2 variables, and a Switch to report (and easily view) the state of the GPIO.

Edit: Did some minor script-improvements
/Ton (PayPal.me)

pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

Re: blinking led

#3 Post by pw444 » 01 Jan 2021, 17:20

Yes, you are right.. .0.5 and 1 Hz (every 2 seconds and every second). Sorry for the mistake :mrgreen:

I will try to figure out how it works.

Thx for the help.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: blinking led

#4 Post by TD-er » 02 Jan 2021, 10:39

@Ath:
2 remarks of your example:
- Why use a dummy task instead of a variable? Is it needed to keep the state after a crash/reboot?
- Why using a loop timer? Why not using PWM? PWM has a frequency parameter.

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: blinking led

#5 Post by Ath » 02 Jan 2021, 12:39

- I used a Dummy Device to get the current state visible in the Devices page, can easily be replaced by an internal variable
- I tried the PWM frequency option, but it wouldn't work here for 0.5 or 1 Hz (latest build), and I haven't had time to further investigate that yet
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: blinking led

#6 Post by TD-er » 02 Jan 2021, 12:43

Hmm, strange that it isn't working.
That is worth investigation as why it doesn't accept such a low frequency.
I thought I had tested with at least 1 Hz signals when testing the fade bugfix.
< 1 Hz will fail as the frequency is an integer value.

But you can also change the duty cycle to make it noticable different :)

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: blinking led

#7 Post by Ath » 02 Jan 2021, 13:52

Tried it once more like this:

Code: Select all

pwm,2,999,0,1
(Using Wemos D1 on-board led)

And the output says this:

Code: Select all

{
"log": "GPIO: 2 duty: 999 f: 1 Hz",
"plugin": 1,
"pin": 2,
"mode": "PWM",
"state": 999
}
(Chose duty 999 to get the 1 Hz in the log without being truncated)

I'm running a self-built release (including P037 improvements), updated to latest mega branch
/Ton (PayPal.me)

pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

Re: blinking led

#8 Post by pw444 » 02 Jan 2021, 17:54

Used this one, and like a charm...

On for 200mS and Off for 2 seconds.

%v2% toggle state.
%v3% comes from MQTT#Connected/Disconnected, so it only blinks when MQTT is connected and able to send/receive topics ;)

Code: Select all

On Rules#Timer=1 do //When Timer1 expires, do
  if %v2%=0 and %v3%=0
    pulse,2,1,200  // pulse    
    let,2,1
    timerSet,1,1 
  else
    let,2,0
    timerSet,1,2 //Resets the Timer 1 for another X seconds
  endif 
endon

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests