Tuesday, October 4, 2016

Blink the LED on the NodeMCU ESP8266

This code blinks the blue LED on the ESP8266 board.

ledState = gpio.LOW
ledCtr = 0
ledCurrentCnt = 0

function blinkLed(ctr)
  local pin = 4
  ledCtr = ctr 
  ledCurrentCnt = 0
  gpio.mode(pin, gpio.OUTPUT)
  tmr.alarm(6, 100, tmr.ALARM_AUTO, blinkLedCallback)
end

function blinkLedCallback()
  print("blink", ledCurrentCnt, ledCtr)
  
  if ledCurrentCnt >= ledCtr then
    print("Stopping LED blink")
    tmr.stop(6)
    gpio.write(4, gpio.HIGH)
    return
  end
  
  if ledState == gpio.LOW then
    ledState = gpio.HIGH
  else
    ledState = gpio.LOW
  end
  gpio.write(4, ledState)
  ledCurrentCnt = ledCurrentCnt + 1 
end

blinkLed(6)

No comments:

Post a Comment