Problems using OSC and fastLED at the same time

Hi,

I have a BNo055 gyro/accelorometer/magnetometer and an WS2812B LED strip connected to the same board.

I am having problems making the two portions of this code work at the same time. The BN0055 IMU is able to send the data via OSC perfectly if I comment out the LED loop and the LED strip works perfectly if I comment out the OSC data. At first I thought the problem had to do with the delay() function in the fastLED code, but even after I replaced that I am still unable to make it work. The code loads onto the board without a hitch but it freezes the device. Any help would be appreciated.

void loop() {
  
//Gyroscope, acceleorometer and magnetometer
  sensors_event_t event;
  sendCount++;

  if (sendCount > 1000) {
   

    imu::Vector<3> magnetometer = bno.getVector(Adafruit_BNO055::VECTOR_MAGNETOMETER);
    imu::Vector<3> linearAccel = bno.getVector(Adafruit_BNO055::VECTOR_LINEARACCEL);
    bno.getEvent(&event);

    sendViaOSC(event.orientation.x, event.orientation.y, event.orientation.z, linearAccel.x(), linearAccel.y(), linearAccel.z(), magnetometer.x(), magnetometer.y(), magnetometer.z());
    sendCount = 0;
  }

//LED strip WS2812B

  currentMillis = millis();
  if (currentMillis - startMillis >= period) {
    startMillis = currentMillis;
    leds[x - 4] = CRGB::Black;
    x++;
    leds[x].setRGB(255, 255, 255);
    FastLED.show();
    if (x >= NUM_LEDS) x = 0;
  }

}

I have no idea what OSC is. Oscillator? Oscilloscope? ...

Posting your full code might also help people to help you.

If sendViaOsc relies on interrupts, you will have a problem as neopixels are timing sensitive and for that reason interrupts are disabled during FastLed.show().

Hi,

Sorry, this is one of my first posts on this forum. OSC stands for Open Sound Control in this case -communication protocol using Wifi. Here is the full code:

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 20;
int x;

#define LED_PIN     18
//#define CLOCK_PIN 4
#define NUM_LEDS    111
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS  64

CRGB leds[NUM_LEDS];

// ESP32 WiFi
#include <WiFi.h>

// Secrets file
#include "secrets.h"

// BNO055 / Adafruit Sensor library
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>

#define BNO055_ADDRESS 0x28
//BNO055 is by standard for the fusion board on 0x28
Adafruit_BNO055 bno = Adafruit_BNO055(55, BNO055_ADDRESS);

// OSC Messaging
#include <OSCMessage.h>  /// https://github.com/CNMAT/OSC
#include <OSCBundle.h>  /// https://github.com/CNMAT/OSC

unsigned int localPort = 8888;
int sendCount;
int count;
int lastEntry;
const IPAddress outIp(SECRET_IP);
WiFiUDP udp;

#define SERIAL_SPEED 115200
#define VERSION 0.1




void setup()  {



 startMillis = millis();

 pinMode(LED_PIN, OUTPUT);
 FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
 FastLED.setBrightness(BRIGHTNESS);
 //  fill_solid(leds, NUM_LEDS, CRGB::Blue);
 //  FastLED.show();
 x = 0;

 Serial.begin(SERIAL_SPEED);
 while (!Serial) {
   //wait for Serial to start up
 }

 //Try to connect to the Wifi netowrk provided in secrets
 Serial.print("OSC IMU ");
 Serial.println(VERSION);
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(SECRET_SSID);

 WiFi.begin(SECRET_SSID, SECRET_PASSWORD);

 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print('.');
 }
 //
 Serial.println();
 Serial.print("Success! Connected, IP Address is ");
 Serial.println(WiFi.localIP());
 //
 Serial.println("\nStarting connection to server...");
 // if you get a connection, report back via serial:
 udp.begin(localPort);


 //Now try to find the BNO055 sensor
 if (bno.begin()) {
   Serial.print("Found BNO055 on I2C address 0x");
   Serial.println(BNO055_ADDRESS, HEX);
 } else {
   /* There was a problem detecting the BNO055 ... check your connections */
   Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
   while (1);
 }
 bno.setExtCrystalUse(true);


}


void loop() {

 //Gyroscope, acceleorometer and magnetometer
 sensors_event_t event;
 sendCount++;

 if (sendCount > 1000) {


   imu::Vector<3> magnetometer = bno.getVector(Adafruit_BNO055::VECTOR_MAGNETOMETER);
   imu::Vector<3> linearAccel = bno.getVector(Adafruit_BNO055::VECTOR_LINEARACCEL);
   bno.getEvent(&event);
   sendViaOSC(event.orientation.x, event.orientation.y, event.orientation.z, linearAccel.x(), linearAccel.y(), linearAccel.z(), magnetometer.x(), magnetometer.y(), magnetometer.z());
   sendCount = 0;
 }

 //LED strip WS2812B

 currentMillis = millis();
 if (currentMillis - startMillis >= period) {
   startMillis = currentMillis;
   leds[x - 4] = CRGB::Black;
   x++;
   leds[x].setRGB(255, 255, 255);
   FastLED.show();
   if (x >= NUM_LEDS) x = 0;
 }

}


void sendViaOSC(float gyroX, float gyroY, float gyroZ, float acX, float acY, float acZ, float oX, float oY, float oZ) {

 OSCMessage msg("/oscimu");
 msg.add((int32_t)gyroX);
 msg.add((int32_t)gyroY);
 msg.add((int32_t)gyroZ);
 msg.add((int32_t)acX);
 msg.add((int32_t)acY);
 msg.add((int32_t)acZ);
 msg.add((int32_t)oX);
 msg.add((int32_t)oY);
 msg.add((int32_t)oZ);
 //send out
 udp.beginPacket(outIp, SECRET_OUTPORT);
 msg.send(udp);
 udp.endPacket();
 msg.empty();
 //  sendCount = 0;
}

Sorry, I have no idea where this can go wrong.