Ticker

Updated on 3 September 2021
dev board WeMos D1 Mini
chip ESP8266
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code ticker.ino
// 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

Description

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