My first LUA script

Hi,

I set myself a challenge try and and make a program in LUA, this is my first attempt!

My objective was to apply a BRIGHT flashing LED, 3W or bigger, as an orientation indicator, via a LED CC driver board!

The, available: Github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Scripting/examples/relay_control.lua script, (thanks to who ever provided the example) with the Toggle only gave a 50:50 ratio. I wanted a variable, the script below gives 6:1 but can be varied as required.

After at least 12 Hours of hair pulling, and remember this is my first attempt I am publishing my first script. It seems so short for such effort.

Others may find better ways to do it, improve on it or point out possible options.

(It’s called F**K it WORKS slow.lua)

local RELAY_NUM = 2 – Relay number 3 in Mateksys H743

local relay_status = true

function led_action() –

if relay_status then

relay:off(RELAY_NUM)

relay_status = false

return led_action, 1200 – off time

else –

relay:on(RELAY_NUM)

relay_status = true

end

return led_action, 200 – on time

end

return led_action() – run immediately

end

return update() –

Appoligies if posted in the wrong place?

Your last two lines do nothing and are in error.

You have a bunch of unnecessary hyphens for comments that don’t exist.

This is functionally equivalent and does not rely on a global state variable:

local RELAY_NUM = 2

function on()
    relay:on(RELAY_NUM)
    return off, 200
end

function off()
    relay:off(RELAY_NUM)
    return on, 1200
end

return on()

And this should also work:

local RELAY_NUM = 2
local INTERVAL_MS = {
    [0] = 1200,
    [1] = 200
}

function toggle()
    relay:toggle(RELAY_NUM)
    return toggle, INTERVAL_MS[relay:get(RELAY_NUM)]
end

return toggle()

I have several videos on the subject that you may find useful:
Yuri’s Homebrew & DIY - YouTube

Thanks for the reply, I am always willing to learn. (And i had fun trying out my problem solving logic).
I have alway found it easier to learn a programming language when you have a personal objective. I started with ASM in ATMEL in 2002!
The hyphens were a doulble - - comment block but when put in the post would only show a single (above my pay grade!)
Thanks agin i’ll check your Video.

You can use triple grave accents (`) to surround code blocks on the forum. And I see that your comments only have single dashes as a result of copy/paste auto formatting. But you also have pointless trailing dashes on a bunch of lines.

You’re in good company. My degree predates your ASM experiments by a few years…

Just for grins, here it is in 5 (ugly) lines:

function update()
    relay:toggle(2)
    return update, (function() return ({[0] = 1200, [1] = 200})[relay:get(2)] end)()
end
return update()

So much to learn, so few years!
Now you’re showing off :rofl:
We were taught BASIC in college, and thermionic valve theroy, transistors were way off!

My next LUA script is to develope this in to Aircraft navigation lights, it could take a few more years.

I ended up teaching Radio Communications and Industrial Electronics, built my first Quad in 2009!

Don’t reinvent the wheel…

Please don’t take this the wrong way, this is excellent support, and thank you, but sometimes the journey is more important than the destination.
Maybe it’s my way of learning.
Kind regards,
Roy

PS my intention is to use 3W or 5W fixed color LEDS, pulsed to keep the current draw low, but the intensity high.