How to use Ticker correctly

Hi, using esp8266 i am trying to make a led turn on only for 0.5 seconds every 10 seconds, for that i am using Ticker but it seems that there is some error because the led turns on only a few milliseconds, it is almost imperceptible, could someone tell me how correct this?

#include <Ticker.h>

#define LED_BUILTIN   2     // Led
Ticker ledStatedTick;

void parpadeoLed() {
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH);
}

void setup() {
  Serial.begin(115200);
  Serial.println("Starting");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  ledStatedTick.attach(10, parpadeoLed);    
}

void loop() {
 }

Did the sample code work correctly for you?

Because it is called from a loop, this is almost the same as

void parpadeoLed() {
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(LED_BUILTIN, LOW);
}

Do you see the problem?

Yes

Ok, I understand, I should not use delay within the call

10 seconds, or 10 milliseconds?

Well, that might be a problem too. But I spotted another issue...

Maybe change the 10 to 10000... this is the tick time in milliseconds.

Swap around the LOW and HIGH... on for 500ms... then off.

10 seconds

Then how would I time 10.5 seconds, for example?

For 10 seconds you should put 10, I already tried it wap around the LOW and HIGH without including the delay, it works, the problem appears when including the delay

What happens when you try a larger value?

Ha ha that is funny. Yeah, use '10.0' or '10f'. You have to specifically make it a float value.

Or, '10000'...

Actually.. I misread... 2nd one was attach_ms :slight_smile:

This code runs ok, every 10 seconds the led changes state, but it is not what I need, I need the led to turn on only for 500ms and then turn off and repeat every 10 seconds.

How can I get that?

#include <Ticker.h>

#define LED_BUILTIN   2     // Led
Ticker ledStatedTick;

void parpadeoLed() {
  int state = digitalRead(LED_BUILTIN);  
  digitalWrite(LED_BUILTIN, !state);     

}

void setup() {
  Serial.begin(115200);
  Serial.println("Starting");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  ledStatedTick.attach(10, parpadeoLed);
}

void loop() {
}

I tried your sketch with the following, on an ESP32 and it worked fine.

void parpadeoLed() 
{
  digitalWrite(LED_BUILTIN, HIGH);     
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);      
}

I suspect on the ESP8266 the delay statement is using the same timer as Ticker.h so it doesn't work.

As an alternative you could use the following. Ticker is called every 500ms, but only lights the LED on every 20th call.

#include <Ticker.h>

#define LED_BUILTIN   2     // Led
Ticker ledStatedTick;

void parpadeoLed() 
{
  static uint8_t count = 0;
  
  count++;

  if (count == 20)
  {
    digitalWrite(LED_BUILTIN, HIGH);     
    count = 0;
  }
  else
    digitalWrite(LED_BUILTIN, LOW);     
}

void setup() {
  Serial.begin(115200);
  Serial.println("Starting");
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  ledStatedTick.attach(0.5, parpadeoLed);
}

void loop() {
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.