Blinking LED

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Post Reply
Message
Author
Shimokuta
Posts: 113
Joined: Wed Feb 03, 2021 12:52 pm

Blinking LED

#1 Post by Shimokuta »

I am trying to make a LED , according to a switch posistion :

pos 1 turn LED off
pos 2 turn LED on
pos 3 turn LED blink

It is no problem turning the led on or off ... ofcourse but making it blink with a delay of 1 sec is difficult with LUA.!!
I can use Arduino C++ with messageport to realize this, but prefer to use AM LUA only.

Anybody got an idea??

lonespace
Posts: 64
Joined: Wed Mar 25, 2020 1:58 am

Re: Blinking LED

#2 Post by lonespace »

http://siminnovations.com/wiki/index.ph ... _API#Timer

When the switch comes on, call a timer to turn it off, and have that timer call it’s own timer to turn it back on, which calls its own timer, so on and so forth

@Corjan Also somehow in my own stupidity I also reported this thread instead of replying to it, so please ignore that

User avatar
Ralph
Posts: 7878
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Blinking LED

#3 Post by Ralph »

There are more ways to do this, but this one might be the easiest to understand.
The timer_stop at position 1 should be enough in theory, but I've added it to position 0 as well, it's good to be sure that it stopped.

Code: Select all

status_led = hw_output_add("My status LED", false)
hw_switch_add("LED switch", 3, function(position)

    if position == 0 then
        timer_stop(blink_timer)
        hw_output_set(status_led, false)
    elseif position == 1 then
        timer_stop(blink_timer)
        hw_output_set(status_led, true)
    elseif position == 2 then
        if not timer_running(blink_timer) then
            blink_timer = timer_start(nil, 1000, function(count)
                hw_output_set(status_led, count%2 == 0)
            end)
        end
    end

end)

Shimokuta
Posts: 113
Joined: Wed Feb 03, 2021 12:52 pm

Re: Blinking LED

#4 Post by Shimokuta »

Thanks guys

I used Ralph's code , modified it a bit because i use a 3 position switch with middle position not having an input pin, but the result is a blinking led when switch is in the right position!

Post Reply