Post your best Lua script examples here

User avatar
By primetime
#57530 Hi guys!

Just a bit of background. I started coding as a hobby and I'm moving on to programming microcontrollers. I picked up a ESP8266 based NodeMCU board (Lolin V3) and I'm playing around with it. I'm a novice at electronics also, so if I wire things improperly, I apologize. I'm posting this here to get some feedback as well as to help some other beginners with trying to figure stuff out.

Here's how I wired my button to the NodeMCU
Image

Code: Select all-- NodeMCU Onboard LED Control by External Button
led = 4
pin = 1

local count = 0

function onled()
   state = gpio.read(4)
   if state == 0 then
      gpio.write(led, gpio.HIGH)
      print("LED Off")
   else
      gpio.write(led, gpio.LOW)
      print("LED On")
   end
   gpio.write(pin, gpio.LOW)
   count = count + 1
   print("Push Counter is: "..count)
end


gpio.mode(pin, gpio.INT)
gpio.mode(led, gpio.OUTPUT)
gpio.write(pin,gpio.LOW)
gpio.trig(pin, "up", onled)


Quick explanation:
GPIO 4 has an LED (Lolin v3 module) that lights up when the pin is set to LOW

GPIO 1 is the "trigger" pin. GPIO 1 initial state is LOW. If voltage is applied to the PIN the GPIO 1 state is read as HIGH and the LED is turned off or on depending on the state of the LED. I wired it to the 3v3 pin that passes through the button. When the button is pressed, the connection is complete which routes 3 volts to GPIO 1.

Any feedback is appreciated. I am going to try to work on some sort of debounce function when I get the chance.
User avatar
By trackerj
#58065 Might worth to take a look also to this post:

Buttons, pushbuttons and debouncing story


Image