--[[ Script to control LED strip from RC Transmitter. I am using 4 LED strips in series with 12 LEDs each. --]] -- Define variables local num_leds = 12 local led_function = 0 local color_set = 0 local msg_sent = 0 local hold_update = 0 local r_set = 0 local g_set = 0 local b_set = 0 --[[ Use SERVOn_FUNCTION 94 for LED strip. For Matek 743 Slim, SERVO13_FUNCTION should be used. --]] local chan = assert(SRV_Channels:find_channel(94),"LEDs: channel not set") -- Find_channel returns 0 to 15, convert to 1 to 16 chan = chan + 1 -- Initialisation code assert(serialLED:set_num_neopixel(chan, num_leds),"Failed LED setup") function update_LEDs() -- Get RC input from RC channels 7 and 11 - 14 pwm12 = rc:get_pwm(12) pwm14 = rc:get_pwm(14) pwm13 = rc:get_pwm(13) pwm11 = rc:get_pwm(11) pwm7 = rc:get_pwm(7) if pwm7 >= 1500 and hold_update == 0 then --Use Channel 7 to determine LED Function led_function = led_function + 1 hold_update = 1 end if led_function == 6 then --Reset function to 0 after cycling to the end led_function = 0 msg_sent = 0 if msg_sent == 0 then gcs:send_text(6,"Reset LED Function to 0") msg_sent = 1 end elseif led_function == 0 then -- Calculate color blend based on RC input LED_br = math.floor((pwm11-982) * 255 / 1024) r = math.floor((pwm12 - 982) * LED_br / 1024) g = math.floor((pwm14 - 982) * LED_br / 1024) b = math.floor((pwm13 - 982) * LED_br / 1024) if msg_sent == 1 or msg_sent == 0 then gcs:send_text(6,"LED Function = 0, Custom color") msg_sent = msg_sent + 1 end elseif led_function == 1 then --Lock LED color to current color if color_set == 0 then LED_br = math.floor((pwm11 - 982) * 255 / 1024) r_set = math.floor((pwm12 - 982) * LED_br / 1024) g_set = math.floor((pwm14 - 982) * LED_br / 1024) b_set = math.floor((pwm13 - 982) * LED_br / 1024) r = r_set g = g_set b = b_set color_set = 1 elseif color_set == 1 then r = r_set g = g_set b = b_set end if msg_sent == 2 then gcs:send_text(6,"LED Function = 1, Custom color locked") msg_sent = msg_sent + 1 end elseif led_function == 2 then --Change LED to Red r = math.floor((pwm11 - 982) * 255 / 1024) g = 0 b = 0 if msg_sent == 3 then gcs:send_text(6,"LED Function = 2, Color set to Red") msg_sent = msg_sent + 1 end elseif led_function == 3 then --Change LED to Green r = 0 g = math.floor((pwm11 - 982) * 255 / 1024) b = 0 if msg_sent == 4 then gcs:send_text(6,"LED Function = 3, Color set to Green") msg_sent = msg_sent + 1 end elseif led_function == 4 then --Change LED to Blue r = 0 g = 0 b = math.floor((pwm11 - 982) * 255 / 1024) if msg_sent == 5 then gcs:send_text(6,"LED Function = 4, Color set to Blue") msg_sent = msg_sent + 1 end elseif led_function == 5 then -- Calculated color blend based on RC Input with Strobe if color_set == 0 then LED_br = math.floor((pwm11 - 982) * 255 / 1024) r_set = math.floor((pwm12 - 982) * LED_br / 1024) g_set = math.floor((pwm14 - 982) * LED_br / 1024) b_set = math.floor((pwm13 - 982) * LED_br / 1024) r = r_set g = g_set b = b_set color_set = 1 if msg_sent == 6 then gcs:send_text(6,"LED Function = 5, Strobe Activated") msg_sent = msg_sent + 1 end elseif color_set == 1 then r = 0 g = 0 b = 0 color_set = 0 end else if msg_sent ~= 10 then gcs:send_text(3,"LED Function error") msg_sent = 10 end end serialLED:set_RGB(chan, -1, r, g, b) serialLED:send(chan) if pwm7 < 1500 then hold_update = 0 end return update_LEDs, 200 end return update_LEDs, 600