Ultra-simple beginner scripts apparently not working

I got my hands on an FC with 2MB flash for the first time, so I wanted to try out ‘this LUA thing’. The only results I was hoping to get from it were: Neopixel LED customization, custom tunes from the buzzer, and maybe some GCS messages based on certain flight parameter conditions.

All of that is really just for fun and to give this plane a personal touch. (Although I wouldn’t mind if it drags me in deeper into programming due to being fun.)

Unfortunately it turned out the Wiki is not quite up to date concerning LEDs, so I spent some time trying to get my Neopixel one to work with “set_num_LEDs( output_number , number_of_LEDs )” instead of “serialLED:set_num_neopixel”. Also some of the example scripts still had the old command. But finally I succeded in at least making them light up in different colours with:

serialLED:set_num_neopixel(13, 6)
serialLED:set_RGB(13, 0, 21, 131, 36)
serialLED:set_RGB(13, 1, 15, 131, 136)
serialLED:set_RGB(13, 2, 2, 131, 3)
serialLED:set_RGB(13, 3, 255, 255, 255)
serialLED:set_RGB(13, 4, 215, 0, 0)
serialLED:set_RGB(13, 5, 0, 0, 255)
serialLED:send(13)

Next I took a look at how to create loops in LUA and tried the following ones to get changing colours:

local colourcodearray = {0, 50, 100, 150, 200, 255}

for colour (colourcodearray) do

	serialLED:set_num_neopixel(13, 6)
	serialLED:set_RGB(13, 0, colour, colour, colour)
	serialLED:send(13)
end
for colour = 1, 255, 1 do

	serialLED:set_num_neopixel(13, 6)
	serialLED:set_RGB(13, 1, colour, colour, colour)
	serialLED:send(13)
end

local colour = 0

while true do

	colour = colour + 1

	serialLED:set_num_neopixel(13, 6)
	serialLED:set_RGB(13, 2, colour, colour, colour)
	serialLED:send(13)

	if colour > 255 then
		break
	end

end

Unfortunately none of those has any effect - maybe it just happens too fast?

Same goes for my GCS message and buzzer tune tests, which in the end I set to play multiple times, hoping one would ‘get through’, but nothing happens.

for message = 1, 50, 1 do

gcs:send_text(1, 'HELLO 1')
gcs:send_text(6, 'HELLO 2')
gcs:send_text(0, 'HELLO 3')

end

for music = 1, 10, 1 do

local MYTUNE = "MBT200L7<<FG#>C#2FG#>C#2"

notify:play_tune(MYTUNE)

end

I put all of this into one script, it’s a bit annoying at least the working LED part only runs with USB disconnected and after rebooting the FC with battery connected, so I didn’t want to fiddle around with more than one file, which I was uploading dozens of times this way.

I’m aware this is extremely basic stuff, but that’s why I think even I should be able to to get it working. :wink:

You have to consider that a script has only a small timeslot to run. If it takes too long, it will be stopped by the operating system, because it seems to be crashed.

each script is allocated a fixed time chunk (measured in VM instructions) in which it is expected to complete it’s processing

https://ardupilot.org/plane/docs/common-lua-scripts.html

So you should divide your code in portions that are separated by return - e.g. by a counter and a switch-like code.

If that’s not solved till now, you should post your complete script here.

Thanks for taking the time to reply. I was in fact a little more successful after I had posted this, but then other RC stuff came in between, so I didn’t continue.

But I was able to display a GCS message, play a dissonant tune in a loop and make a single LED flash. :wink: All of that is not relevant to the actual build, so I’ll get back to LUA scripts once the plane is otherwise finished.

Hi! How did you get the tune to work? Did you use a counter?

You don’t need a counter. The wiki shows the format for playing tones. You can get quite elaborate.

The MBT prefix will make that note sequence repeat indefinitely, which is quite annoying. Use MFT instead, and call the function periodically to repeat the notification as needed.

Okay! I tried using some examples I saw here and in the AP_Scripting folder, but could not get it to work :frowning: The script runs in the controller (I put some debug statements), but no sound is played. When I power the Copter, the ESCs play some sound, but nothing from the Lua script. NTF_BUZZ_TYPES needs to be 7 so the ESCs can play the sound?

I just tried a (correct) variation of the OP’s script, and it works fine on the built-in buzzer:

local MYTUNE = 'MFT200L7<<FG#>C#2FG#>C#2'

function play()
  notify:play_tune(MYTUNE)
    return play, 5000
end

return play, 5000

NTF_BUZZ_TYPES,7 enables output on DSHOT ESCs. If you aren’t using DSHOT, then you can’t use the ESC based buzzer type.

Now I understand why it did not work :slight_smile: Forgot my ESCs are PWM. Will try with a buzzer! Thank you for the help!