led Task

Hack-Mike
Posts: 8
Joined: Wed Jul 15, 2020 1:42 pm

led Task

Postby Hack-Mike » Mon Jan 11, 2021 9:49 am

Hello everybody,

I have a custom board with ESP32 and there is 3 LEDs.
Depending on the board state there are different LEDs blinking combinations, so I want to understand what is the best way to implement on my code these LEDs functions.

I use vTaskDelay() to block the LED in ON or OFF state, but if I need to change the LEDs blinking function ESP32 has to wait the delay time before changing, but I want the change to be immediate.

Hope in your help.

sebasdt
Posts: 17
Joined: Fri Jul 24, 2020 8:18 am

Re: led Task

Postby sebasdt » Mon Jan 11, 2021 1:49 pm

hi Hack-Mike,

if you want to change the state of the LEDs immediate. please don't use the vTaskDelay() this "blocks" or "locks" that bit of code. I rather use millis() for a big delay. you could look at blink without delay of how to use millis() (http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay)

Code: Select all

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Hack-Mike
Posts: 8
Joined: Wed Jul 15, 2020 1:42 pm

Re: led Task

Postby Hack-Mike » Mon Jan 11, 2021 7:50 pm

Hi sebasdt,

Yes I know Wiring and Arduino function mills(), but I'm using ESP-IDF with freeRTOS.

So I'm looking for the best way with the freeRTOS, I thought to use xTaskGetTickCount() like millis() functions, but I want to understand if this is the best method.

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: led Task

Postby ESP_Sprite » Tue Jan 12, 2021 2:23 am

You could implement your delay as an xQueueReceive with the delay you'd normally wait as the timeout. If the receive times out, you continue as it it were a wait. If it actually receives something, you use that information to switch to a different pattern. Other tasks can then set the blinking pattern by putting the desired pattern into the queue.

Who is online

Users browsing this forum: No registered users and 374 guests