medusa
August 11, 2022, 12:00am
1
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?
medusa
August 11, 2022, 12:41am
6
anon57585045:
Do you see the problem?
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.
Then how would I time 10.5 seconds, for example?
medusa
August 11, 2022, 12:48am
12
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
red_car
August 11, 2022, 12:49am
13
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'...
red_car
August 11, 2022, 12:51am
15
Actually.. I misread... 2nd one was attach_ms
medusa
August 11, 2022, 1:14am
16
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
system
Closed
February 7, 2023, 2:14am
18
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.