Swinging help. WS2812 strip & Gyro Sensor MPU6050


I’m trying to hookup a swing with a WS2812 led strip.

I guess most swings perform in some sort of a sine wave however I had a lot of difficulty translating that into a smooth output.

I’m using the Fastled Library for WS2812 leds and the leds turn out either bright too quickly to understand, or flicker all around the scale or do not light at all.

I tried using a simple library for MPU6050. GitHub - tockn/MPU6050_tockn: Arduino library for easy communication with MPU6050

I’m attaching everything including raw data from a swing.
The library spits out 8 kinds of data.

Of these gyro for example GYROX data seems to be the smoothest. I’m attaching a sample graph and the dataset is here: JustPaste.it - Share Text & Images the Easy Way

  1. Is it possible to use MPU6050 to create a smooth organic transition from dark to bright for the swing?

  2. What way would you follow to reverse the effect. In other words the gyro sensor gets bright when the movement is fastest when the swing is in the center. Is it possible to reverse this somehow and make the leds brightest when the swing is at the very edges and dark at the center?

I tried smoothing the output with moving averages GitHub - JChristensen/movingAvg: A simple Arduino library for calculating moving averages. and used constrain and didnt even need to map the data since values were between -255 and 255 generally but still. The jitter is high on this one.

#include "FastLED.h"                                          // FastLED library.
#include <MPU6050_tockn.h>
#include <Wire.h>
#include <movingAvg.h>

MPU6050 mpu6050(Wire);
long timer = 0;

movingAvg avgGYRO(5);

uint8_t thishue = 0;                                          // Starting hue value.
uint8_t deltahue = 10;
#define LED_DT 2                                             // Data pin to connect to the strip.
#define COLOR_ORDER GRB                                       // It's GRB for WS2812B and BGR for APA102
#define LED_TYPE WS2812B                                       // What kind of strip are you using (WS2801, WS2812B or APA102)?
#define NUM_LEDS 300                                           // Number of LED's.
#define MAX_BRIGHTNESS 128      // Thats full on, watch the power!
#define MIN_BRIGHTNESS 1        // set to a minimum of 25%
struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.

void setup() {
  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);        // Use this for WS2812B


void loop() {


  if (millis() - timer > 10) {
    Serial.print("& gyroX & "); Serial.print(mpu6050.getGyroX());
    timer = millis();

  int constrainedgetgyro = constrain(mpu6050.getGyroX(), -250, 250);
  int absolutegyrox = abs(constrainedgetgyro);
  int avggx = avgGYRO.reading(absolutegyrox);

 // int mappedValue = map(avggx, 0, 250, 0, 255);
//  Serial.print("& mappedValue & "); Serial.println(mappedValue);

  EVERY_N_MILLISECONDS(5) {                           // FastLED based non-blocking routine to update/display the sequence.



void rainbow_march() {                                        // The fill_rainbow call doesn't support brightness levels
  thishue++;                                                  // Increment the starting hue.
  fill_rainbow(leds, NUM_LEDS, thishue, deltahue);
  addGlitter(300);// Use FastLED's fill_rainbow routine.
} // rainbow_march()

void addGlitter( fract8 chanceOfGlitter)
  if ( random8() < chanceOfGlitter) {
    leds[ random16(NUM_LEDS) ] += CRGB::White;

That looks to me like you are not sampling fast enough.

Exactly. Less than 10 samples per swing period.

Save a copy of your sketch. Remove all the led related code, just leaving the gyro related code and increase the serial speed to 115200. What does your graph look like now? Please put some labels/units on your graph so we can understand what the graph axes mean.