ESP32 driving pixels

For the discussion of Arduino related topics.

Moderator: phalanx

Post Reply
jdanna
Posts: 2
Joined: Wed Nov 28, 2018 10:33 pm

ESP32 driving pixels

Post by jdanna » Wed Nov 28, 2018 10:39 pm

Hi everyone - I'm trying to recreate this project:
https://learn.sparkfun.com/tutorials/us ... pixels/all

With some small modifications. I've hooked up a WS2811 strip to 12v, and used a level shifting circuit to shift the 3v logic of the ESP32 to the 5v data input on the strip.

I couldn't seem to get the FastLED library to work until I lifted some code from one of their examples to make it work with ESP32. I can control the whole strip, and everything seems to work - but when I use the example code to control it via ArtNET - it happens 3 pixels at a time. (for example channel 1 adjusts the Red value on the first 3 pixels of the strip and so-on)

Here is the code for my sketch as I have it now (its mostly the example code, slightly modified). I cant for the life of me figure out why its doing 3 pixels at a time instead of one - any help would be greatly appreciated:

Code: Select all

#include <WiFi.h>
#include <WiFiUdp.h>
#include <ArtnetWifi.h>
#include "FastLED.h"

const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxx";
FASTLED_USING_NAMESPACE

#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif

#define DATA_PIN    21
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    300
int numLeds = NUM_LEDS;
CRGB leds[NUM_LEDS];

#define BRIGHTNESS          255
#define FRAMES_PER_SECOND  120

#define FASTLED_SHOW_CORE 0

// -- Task handles for use in the notifications
static TaskHandle_t FastLEDshowTaskHandle = 0;
static TaskHandle_t userTaskHandle = 0;

/** show() for ESP32
    Call this function instead of FastLED.show(). It signals core 0 to issue a show,
    then waits for a notification that it is done.
*/
void FastLEDshowESP32()
{
  if (userTaskHandle == 0) {
    // -- Store the handle of the current task, so that the show task can
    //    notify it when it's done
    userTaskHandle = xTaskGetCurrentTaskHandle();

    // -- Trigger the show task
    xTaskNotifyGive(FastLEDshowTaskHandle);

    // -- Wait to be notified that it's done
    const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
    ulTaskNotifyTake(pdTRUE, xMaxBlockTime);
    userTaskHandle = 0;
  }
}

/** show Task
    This function runs on core 0 and just waits for requests to call FastLED.show()
*/
void FastLEDshowTask(void *pvParameters)
{
  // -- Run forever...
  for (;;) {
    // -- Wait for the trigger
    ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

    // -- Do the show (synchronously)
    FastLED.show();

    // -- Notify the calling task
    xTaskNotifyGive(userTaskHandle);
  }
}
// Artnet settings
ArtnetWifi artnet;
const int startUniverse = 0;

bool sendFrame = 1;
int previousDataLength = 0;

// connect to wifi – returns true if successful or false if not
boolean ConnectWifi(void)
{
  boolean state = true;
  int i = 0;

  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 20) {
      state = false;
      break;
    }
    i++;
  }
  if (state) {
    Serial.println("");
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("");
    Serial.println("Connection failed.");
  }

  return state;
}

void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data)
{
  sendFrame = 1;
  // set brightness of the whole strip
  if (universe == 15)
  {
    FastLED.setBrightness(data[0]);
  }
  // read universe and put into the right part of the display buffer
  for (int i = 0; i < length / 3; i++)
  {
    int led = i + (universe - startUniverse) * (previousDataLength / 3);
    if (led < numLeds)
    {
      leds[led] = CRGB(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]);
    }
  }
  previousDataLength = length;
  FastLEDshowESP32();
}

void setup() {
  delay(3000); // 3 second delay for recovery
  Serial.begin(115200);
  ConnectWifi();
  artnet.begin();
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  artnet.setArtDmxCallback(onDmxFrame);
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);

  int core = xPortGetCoreID();
  Serial.print("Main code running on core ");
  Serial.println(core);

  // -- Create the FastLED show task
  xTaskCreatePinnedToCore(FastLEDshowTask, "FastLEDshowTask", 2048, NULL, 2, &FastLEDshowTaskHandle, FASTLED_SHOW_CORE);
}
int i = 0;

void loop()
{
/*
  if (i >= 255) i = 0;
  i++;
  fill_rainbow(leds, 300, i);
  FastLEDshowESP32();

*/
  artnet.read();
}



jdanna
Posts: 2
Joined: Wed Nov 28, 2018 10:33 pm

Re: ESP32 driving pixels

Post by jdanna » Thu Nov 29, 2018 7:41 pm

whoooops. this was me being stupid. I had bought strips that had a single WS2811 for 3 LEDs, so it was working exactly correct!

Post Reply