Tasks and LED Strips
Posted: Fri Jan 11, 2019 4:40 am
Hi All,
I have been playing around with Strip LEDs (AM103 style) on the ESP32 DEVKITC.
To do this I have been using PtformIO (based on Visual Studio Code). This has worked well and I have has some good fun with the LED strips.However, I have found some issues when running tasks to update the LED Strip. Specifically, I would like to know:
1. Am I doing this the right way, or should I be using some sort of synchronous method?
2. Why cant I update the LED strip on another task (thread)?
// This is included for use in PIO
#include <Arduino.h>
// Inclue the Dostar Library
#include <Adafruit_DotStar.h>
// Set the number of pixels
#define NUMPIXELS 144
// Set the Data and Clock Pins
#define DATAPIN 22
#define CLOCKPIN 23
// Create a new Adafruit_DotStar object called strip
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BGR);
// Structure to hold info for the traffic light
struct TrafficLight
{
int RedColour;
int OrangeColour;
int GreenColour;
int RedTime;
int OrangeTime;
int GreenTime;
int RedLED;
int OrangeLED;
int GreenLED;
} TrafficLight1, TrafficLight2, TrafficLight3, TrafficLight4;
void TrafficLightTask1(void *parameter)
{
TrafficLight thisTrafficLight = *(TrafficLight *)parameter;
// This traffipc lights simply cycles red, orange , green, orange for ever
while (true)
{
// Red on
strip.setPixelColor(thisTrafficLight.RedLED, thisTrafficLight.RedColour);
delay(thisTrafficLight.RedTime);
// Red off, Orange on
strip.setPixelColor(thisTrafficLight.RedLED, 0, 0, 0);
strip.setPixelColor(thisTrafficLight.OrangeLED, thisTrafficLight.OrangeColour);
delay(thisTrafficLight.OrangeTime);
// Orange off , Green on
strip.setPixelColor(thisTrafficLight.OrangeLED, 0, 0, 0);
strip.setPixelColor(thisTrafficLight.GreenLED, thisTrafficLight.GreenColour);
delay(thisTrafficLight.GreenTime);
// Green off, Orange on
strip.setPixelColor(thisTrafficLight.GreenLED, 0, 0, 0);
strip.setPixelColor(thisTrafficLight.OrangeLED, thisTrafficLight.OrangeColour);
delay(thisTrafficLight.OrangeTime);
// Orange off
strip.setPixelColor(thisTrafficLight.OrangeLED, 0, 0, 0);
}
}
void stripShow() {
strip.show();
delay(10);
}
void TrafficLightShowTask(void *paramater)
{
stripShow();
}
void setup()
{
//Setup the serial port
Serial.begin(9600);
// Start the strip object
strip.begin();
// Show all black on the strip
strip.show();
// Setup Traffic Light show task
// ***** this will cause a corea core dump
// xTaskCreate(
// TrafficLightShowTask, /* Task function. */
// "Traffic Light Show Task", /* String with name of task. */
// 10000, /* Stack size in bytes. */
// NULL, /* Parameter passed as input of the task */
// 2, /* Priority of the task. */
// NULL); /* Task handle. */
// Setup Traffic Light 1
TrafficLight1.GreenColour = 0x0000FF00;
TrafficLight1.GreenTime = 1000;
TrafficLight1.GreenLED = 2;
TrafficLight1.RedColour = 0x00FF0000;
TrafficLight1.RedTime = 1000;
TrafficLight1.RedLED = 0;
TrafficLight1.OrangeColour = 0x00FFFF00;
TrafficLight1.OrangeTime = 1000;
TrafficLight1.OrangeLED = 1;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 1", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight1, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
TrafficLight2.GreenColour = 0x0000FF00;
TrafficLight2.GreenTime = 500;
TrafficLight2.GreenLED = 5;
TrafficLight2.RedColour = 0x00FF0000;
TrafficLight2.RedTime = 500;
TrafficLight2.RedLED = 3;
TrafficLight2.OrangeColour = 0x00FFFF00;
TrafficLight2.OrangeTime = 500;
TrafficLight2.OrangeLED = 4;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 2", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight2, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
TrafficLight3.GreenColour = 0x0000FF00;
TrafficLight3.GreenTime = 250;
TrafficLight3.GreenLED = 8;
TrafficLight3.RedColour = 0x00FF0000;
TrafficLight3.RedTime = 250;
TrafficLight3.RedLED = 6;
TrafficLight3.OrangeColour = 0x00FFFF00;
TrafficLight3.OrangeTime = 250;
TrafficLight3.OrangeLED = 7;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 3", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight3, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
TrafficLight4.GreenColour = 0x0000FF00;
TrafficLight4.GreenTime = 125;
TrafficLight4.GreenLED = 11;
TrafficLight4.RedColour = 0x00FF0000;
TrafficLight4.RedTime = 125;
TrafficLight4.RedLED = 9;
TrafficLight4.OrangeColour = 0x00FFFF00;
TrafficLight4.OrangeTime = 125;
TrafficLight4.OrangeLED = 10;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 4", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight4, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
}
void loop()
{
// I have to do this here as doing it in a task causess a core dump
strip.show();
delay(10);
}
Thanks to all that reply.
Regards
Mike
I have been playing around with Strip LEDs (AM103 style) on the ESP32 DEVKITC.
To do this I have been using PtformIO (based on Visual Studio Code). This has worked well and I have has some good fun with the LED strips.However, I have found some issues when running tasks to update the LED Strip. Specifically, I would like to know:
1. Am I doing this the right way, or should I be using some sort of synchronous method?
2. Why cant I update the LED strip on another task (thread)?
// This is included for use in PIO
#include <Arduino.h>
// Inclue the Dostar Library
#include <Adafruit_DotStar.h>
// Set the number of pixels
#define NUMPIXELS 144
// Set the Data and Clock Pins
#define DATAPIN 22
#define CLOCKPIN 23
// Create a new Adafruit_DotStar object called strip
Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BGR);
// Structure to hold info for the traffic light
struct TrafficLight
{
int RedColour;
int OrangeColour;
int GreenColour;
int RedTime;
int OrangeTime;
int GreenTime;
int RedLED;
int OrangeLED;
int GreenLED;
} TrafficLight1, TrafficLight2, TrafficLight3, TrafficLight4;
void TrafficLightTask1(void *parameter)
{
TrafficLight thisTrafficLight = *(TrafficLight *)parameter;
// This traffipc lights simply cycles red, orange , green, orange for ever
while (true)
{
// Red on
strip.setPixelColor(thisTrafficLight.RedLED, thisTrafficLight.RedColour);
delay(thisTrafficLight.RedTime);
// Red off, Orange on
strip.setPixelColor(thisTrafficLight.RedLED, 0, 0, 0);
strip.setPixelColor(thisTrafficLight.OrangeLED, thisTrafficLight.OrangeColour);
delay(thisTrafficLight.OrangeTime);
// Orange off , Green on
strip.setPixelColor(thisTrafficLight.OrangeLED, 0, 0, 0);
strip.setPixelColor(thisTrafficLight.GreenLED, thisTrafficLight.GreenColour);
delay(thisTrafficLight.GreenTime);
// Green off, Orange on
strip.setPixelColor(thisTrafficLight.GreenLED, 0, 0, 0);
strip.setPixelColor(thisTrafficLight.OrangeLED, thisTrafficLight.OrangeColour);
delay(thisTrafficLight.OrangeTime);
// Orange off
strip.setPixelColor(thisTrafficLight.OrangeLED, 0, 0, 0);
}
}
void stripShow() {
strip.show();
delay(10);
}
void TrafficLightShowTask(void *paramater)
{
stripShow();
}
void setup()
{
//Setup the serial port
Serial.begin(9600);
// Start the strip object
strip.begin();
// Show all black on the strip
strip.show();
// Setup Traffic Light show task
// ***** this will cause a corea core dump
// xTaskCreate(
// TrafficLightShowTask, /* Task function. */
// "Traffic Light Show Task", /* String with name of task. */
// 10000, /* Stack size in bytes. */
// NULL, /* Parameter passed as input of the task */
// 2, /* Priority of the task. */
// NULL); /* Task handle. */
// Setup Traffic Light 1
TrafficLight1.GreenColour = 0x0000FF00;
TrafficLight1.GreenTime = 1000;
TrafficLight1.GreenLED = 2;
TrafficLight1.RedColour = 0x00FF0000;
TrafficLight1.RedTime = 1000;
TrafficLight1.RedLED = 0;
TrafficLight1.OrangeColour = 0x00FFFF00;
TrafficLight1.OrangeTime = 1000;
TrafficLight1.OrangeLED = 1;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 1", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight1, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
TrafficLight2.GreenColour = 0x0000FF00;
TrafficLight2.GreenTime = 500;
TrafficLight2.GreenLED = 5;
TrafficLight2.RedColour = 0x00FF0000;
TrafficLight2.RedTime = 500;
TrafficLight2.RedLED = 3;
TrafficLight2.OrangeColour = 0x00FFFF00;
TrafficLight2.OrangeTime = 500;
TrafficLight2.OrangeLED = 4;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 2", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight2, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
TrafficLight3.GreenColour = 0x0000FF00;
TrafficLight3.GreenTime = 250;
TrafficLight3.GreenLED = 8;
TrafficLight3.RedColour = 0x00FF0000;
TrafficLight3.RedTime = 250;
TrafficLight3.RedLED = 6;
TrafficLight3.OrangeColour = 0x00FFFF00;
TrafficLight3.OrangeTime = 250;
TrafficLight3.OrangeLED = 7;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 3", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight3, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
TrafficLight4.GreenColour = 0x0000FF00;
TrafficLight4.GreenTime = 125;
TrafficLight4.GreenLED = 11;
TrafficLight4.RedColour = 0x00FF0000;
TrafficLight4.RedTime = 125;
TrafficLight4.RedLED = 9;
TrafficLight4.OrangeColour = 0x00FFFF00;
TrafficLight4.OrangeTime = 125;
TrafficLight4.OrangeLED = 10;
//Create a task for the traffic Light
xTaskCreate(
TrafficLightTask1, /* Task function. */
"Traffic Light 4", /* String with name of task. */
10000, /* Stack size in bytes. */
&TrafficLight4, /* Parameter passed as input of the task */
1, /* Priority of the task. */
NULL); /* Task handle. */
}
void loop()
{
// I have to do this here as doing it in a task causess a core dump
strip.show();
delay(10);
}
Thanks to all that reply.
Regards
Mike