Hey Guys, I am using the MPU6050 to switch in between two animations i

Hey Guys,

I am using the MPU6050 to switch in between two animations i have done. I can not make them communicate together but then again I’m a total beginner and I was wondering if any can help?

here is my code

#include “FastLED.h”

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include “I2Cdev.h”
#include “MPU6050.h”

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include “Wire.h”
#endif

#define SPEED 20

// How many leds in your strip?
#define NUM_LEDS 25

// For led chips like Neopixels, which have a data line, ground, and power, you just
// need to define DATA_PIN. For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
#define DATA_PIN 3
#define CLOCK_PIN 13

MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // ← use for AD0 high

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define OUTPUT_READABLE_ACCELGYRO

// Define the array of leds
CRGB leds[NUM_LEDS];
TBlendType currentBlending;

boolean state = true;

void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it’s really up to you depending on your project)
Serial.begin(115200);

// initialize device
Serial.println(“Initializing I2C devices…”);
accelgyro.initialize();

// verify connection
Serial.println(“Testing device connections…”);
Serial.println(accelgyro.testConnection() ? “MPU6050 connection successful” : “MPU6050 connection failed”);
}

void loop() {
//MPU6050 Serial read

if (Serial.available() > 0) {
byte inByte;
inByte = Serial.read();
if (inByte == ‘A’) {
state = true;
};
if (inByte == ‘B’) {
state = false;
};
}
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);

#ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y/z values
Serial.print(“a/g:\t”);
Serial.print(ax); Serial.print(“\t”);
Serial.print(ay); Serial.print(“\t”);
Serial.print(az); Serial.print(“\t”);
Serial.print(gx); Serial.print(“\t”);
Serial.print(gy); Serial.print(“\t”);
Serial.println(gz);
#endif

//Stationary Mode
if (state == true) {

 for( int colorStep=0; colorStep<256; colorStep++ ) {

  int r = colorStep;
  int b = 225-colorStep;
  int g = 0;

  for(int x = 0; x < NUM_LEDS; x++){
  leds[x] = CRGB(r,g,b);
  }
  
  FastLED.show();
  FastLED.delay(800 / SPEED);
  delay(10); 

}

for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
 
  currentBlending = LINEARBLEND;
  leds[whiteLed] = CRGB::BlueViolet;
  FastLED.show();
  FastLED.delay(1500 / SPEED);
  currentBlending = LINEARBLEND;
   fadeToBlackBy( leds, NUM_LEDS, 30);
}

  for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = (whiteLed + 1) ){

  leds[whiteLed] = CRGB::MidnightBlue;
  FastLED.show();
  FastLED.delay(2000 / SPEED);
  fadeToBlackBy( leds, NUM_LEDS, 30);
  currentBlending = LINEARBLEND;
  
  }

}

//motion mode

if (state == false)
{

for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
  fadeToBlackBy( leds, NUM_LEDS, 20);
  FastLED.show();
  leds[whiteLed] = CRGB::Grey;
}
   for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
  fadeToBlackBy( leds, NUM_LEDS, 100);
  FastLED.show();
  leds[whiteLed] = CRGB::White;
} 

}
}

First off - in the future, please don’t post code in g+ posts. The faq listed on this page - http://fastled.io/faq - asks that you upload code to http://gist.github.com - it makes it a lot easier to read.

That said, unfortunately, I2C and WS812 leds don’t play very nicely together. I2C relies on interrupts to deal with the reading in of data and WS2812 led writing disables interrupts. https://github.com/FastLED/FastLED/wiki/Interrupt-problems discusses some of the things around interrupts. However, some changes that I would suggest making:

  • Don’t use FastLED.delay, use regular delay, that will allow interrupts to run and the I2C interrupt handler to pull in data as it comes in.
  • Don’t do long loops inside of loop() - because that increases the amount of time between when you call the accel code which increases the likely hood that you’ll lose data because it isn’t being fully handled/read.

This means keeping state in global/static variables and drawing one frame each time through the loop function. (I don’t have the time to do changes to your code to show you what I mean, but it’s possible someone else here can help with that - I may be able to get back to it in a couple days).