#include <Ticker.h>
#define LED_BUILTIN1 13
char msg[50] = { 0, };
void message();
void flip();
void restart_msg();
void led_off();
Ticker ticker1;
Ticker ticker2;
Ticker ticker3;
int count1 = 0;
int count2 = 0;
void restart_msg() {
Serial.println("\t attach ticker2->message() with period 0.5s");
ticker2.attach(0.5, message);
count2 = 0;
}
void flip() {
//int state = digitalRead(LED_BUILTIN1); // get the current state of GPIO1 pin
//digitalWrite(LED_BUILTIN1, !state); // set pin to the opposite state
digitalWrite(LED_BUILTIN1, HIGH); // set pin to the opposite state
ticker3.once_ms(90, led_off);
++count1;
// when the counter reaches a certain value, start blinking like crazy
if (count1 == 10) {
ticker1.attach(1, flip);
}
// when the counter reaches yet another value, stop blinking
else if (count1 == 20) {
ticker1.attach(3, flip);
count1 = 0;
}
}
void led_off() {
digitalWrite(LED_BUILTIN1, LOW);
}
void message() {
//Serial.print("Callback from ticker2->mesage(), count2="); Serial.println(count2);
// Serial.println(msg);
sprintf(msg, "Callback from ticker2->mesage(), count2=%d", count2);
printf("%s\n", msg);
++count2;
// when the counter reaches a certain value, send message every 200 ms
if (count2 == 20) {
Serial.println("\t set ticker2 period to 0.2s");
ticker2.attach(0.2, message);
}
// when the counter reaches yet another value, stop sending
else if (count2 == 50) {
Serial.println("\t ticker2.once->restart_msg() started with delay 10s");
//ticker2.detach();
ticker2.once(10.0, restart_msg);
}
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN1, OUTPUT);
ticker1.attach(0.3, flip); // flip1() every 0.3s
ticker2.attach(0.5, message); // message() every 0.4s
}
void loop() {
// nothing to do!
}