/* flash 3 Pixel LED strips randomly version 2
www. steamtraininfo.com.
This sketch uses 3
WS2812B pixel strips.
*/
#include <FastLED.h>
#define NUM_LEDS 41 /*the number of leds that will light. If */
//****************************
#define DATA_PINA 8 // Connect to the data wires on the pixel strips
//********** MPU6050 Definitions *******************
#include <Wire.h> //Include wire library
#include <MPU6050_light.h> //Include library for MPU communication
MPU6050 mpu(Wire); //Create object mpu
#define zeroAdj 7
CRGB ledsA[NUM_LEDS]; // sets number of pixels that will light on each strip.
//*****************************************************
void setup() {
FastLED.addLeds<WS2812B, DATA_PINA, GRB>(ledsA, NUM_LEDS);
pinMode(zeroAdj, INPUT_PULLUP);
Serial.begin(115200);
Wire.begin();
mpu.begin();
Serial.print(F("MPU6050 status: "));
Serial.println(F("Calculating offsets, do not move MPU6050"));
delay(1000);
mpu.calcGyroOffsets(); //Calibrate gyroscope
Serial.println("Done!\n");
}
//********************************************************
int offset=0;
unsigned long timer = 0;
int angleZ = 0;
void loop() {
mpu.update(); //Get values from MPU
if ((millis() - timer) > 100) { // print data every 100ms
timer = millis();
angleZ = int(mpu.getAngleZ())-offset;
Serial.print(" Angle: ");
Serial.println(angleZ); //Print Z angle value on LCD
delay(10);
float f = (angleZ) * 40.0/360;
angleZ = f;
if(angleZ < 0) angleZ = 0;
if(angleZ > 40) angleZ = 40;
showLED(angleZ);
}
if(digitalRead(zeroAdj) == 0){
Serial.println("Calibrating MPU6050");
offset = mpu.getAngleZ();
delay(1000);
//mpu.calcGyroOffsets(); //Calibrate gyroscope;
}
}
//**************************************************
void showLED(int pot) {
for(int i=0;i<41;i++){
if(i==pot)
ledsA[i] =CRGB(255, 255, 0);
else
ledsA[i] = CRGB(0, 0, 0);
}
FastLED.show();
}