As the title says... Chat on...

User avatar
By selvak
#63413 I had a problem with this code, can anyone help me

gpio.mode(1,gpio.OUTPUT)
gpio.mode(2,gpio.INPUT,gpio.PULLUP)

if (gpio.read(2)==1) then
gpio.write(1,gpio.HIGH)

else
gpio.write(1,gpio.LOW)

end

In this code ,if i connect the pin 2 to ground, the led connected to pin 1 should turn off instead it keeps on glowing.
User avatar
By jwmeyer
#63466 You need some way for nodemcu to know there is a change on the input pin, usually with an interrupt. Try this:
Code: Select alldo
gpio.mode(1,gpio.OUTPUT)
gpio.mode(2,gpio.INT,gpio.PULLUP) --sets gpio2 as interrupt instead of input

function changeled(level) --callback function to change the output, level is the state of gpio2
  if level==1 then
    gpio.write(1,gpio.HIGH)

 else
   gpio.write(1,gpio.LOW)

 end
end
gpio.trig(2,"both", changeled) --sets up the trigger so any change on pin 2 calls the function
end