Ticker

dev board WeMos D1 Mini
chip ESP8266

Before starting

Dependancies

Ensure the following requirements are available:

Pre-requisites

Try these simpler examples:

Buy the components

Description

Blink the on-board LED without using the loop().

Code

Download code
// https://github.com/esp8266/Arduino/blob/master/libraries/Ticker/examples/TickerBasic/TickerBasic.ino
#include <Ticker.h>

Ticker ticker;
int count = 0;
bool isLEDToggle = false;  // some kind of status

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.begin(115200);
  Serial.println("Hello!");

  ticker.attach(1, blink);  // start the ticker
  isLEDToggle = true;
}

void loop() {
  if (count > 10) {  // off the LED when not require
    ticker.detach();
    ledOFF();
    isLEDToggle = false;
  }

  Serial.println(count);
  count++;
  delay(2000);
}

void blink() {
  int state = digitalRead(LED_BUILTIN);
  if (isLEDToggle) {
    digitalWrite(LED_BUILTIN, !state);
  }
}

void ledOFF() {
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("OFF LED");
}

Makefile

.PHONY: lint compile upload clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-readability/todo *.ino

compile:
	arduino-cli compile --fqbn esp8266com:esp8266:d1_mini .

upload: compile
	arduino-cli upload -p /dev/cu.wchusbserial1410 --fqbn esp8266com:esp8266:d1_mini .

clean:
	rm -f .*.bin
	rm -f .*.elf

flash: lint compile upload clean