Page 1 of 1

blinking led

Posted: 01 Jan 2021, 15:28
by pw444
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!

Re: blinking led

Posted: 01 Jan 2021, 17:01
by Ath
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

Re: blinking led

Posted: 01 Jan 2021, 17:20
by pw444
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.

Re: blinking led

Posted: 02 Jan 2021, 10:39
by TD-er
@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.

Re: blinking led

Posted: 02 Jan 2021, 12:39
by Ath
- 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

Re: blinking led

Posted: 02 Jan 2021, 12:43
by TD-er
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 :)

Re: blinking led

Posted: 02 Jan 2021, 13:52
by Ath
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

Re: blinking led

Posted: 02 Jan 2021, 17:54
by pw444
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