2021-02-14 13:13:53 +00:00
|
|
|
/*
|
|
|
|
Arduino and MPU6050 Accelerometer and Gyroscope Sensor Tutorial
|
|
|
|
by Dejan, https://howtomechatronics.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <FastLED.h>
|
2021-02-14 16:42:09 +00:00
|
|
|
#include <MPU6050_light.h>
|
2021-02-14 13:13:53 +00:00
|
|
|
|
|
|
|
#define LED_PIN 7
|
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
const int mode = 2; // 1 for acceleration, 2 for fire
|
|
|
|
const int debug = 2;
|
|
|
|
const int STRIPS = 2;
|
|
|
|
const int NUM_LEDS = 15;
|
|
|
|
const int FRAMES_PER_SECOND = 30;
|
|
|
|
const float range = 0.5; //accelleration range in g
|
2021-02-14 13:13:53 +00:00
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
const int BRIGHTNESS = 50;
|
|
|
|
const int COOLING = 80;
|
|
|
|
const int SPARKING = 50;
|
|
|
|
|
|
|
|
float accelerationHistory [STRIPS];
|
|
|
|
|
|
|
|
MPU6050 mpu(Wire);
|
|
|
|
CRGB leds[NUM_LEDS * STRIPS];
|
|
|
|
CRGBPalette16 gPal;
|
2021-02-14 13:13:53 +00:00
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(19200);
|
|
|
|
//setup LEDs
|
2021-02-14 16:42:09 +00:00
|
|
|
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS*STRIPS).setCorrection( TypicalLEDStrip );
|
|
|
|
FastLED.setBrightness( BRIGHTNESS );
|
|
|
|
|
|
|
|
gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
|
|
|
|
//gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White);
|
|
|
|
|
|
|
|
for (int i = 0; i<STRIPS; i++) {
|
|
|
|
accelerationHistory[i] = 1;
|
|
|
|
}
|
2021-02-14 13:13:53 +00:00
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
Wire.begin();
|
|
|
|
byte status = mpu.begin();
|
|
|
|
Serial.print(F("MPU6050 status: "));
|
|
|
|
Serial.println(status);
|
|
|
|
while(status!=0){ } // stop everything if could not connect to MPU6050
|
|
|
|
|
|
|
|
Serial.println(F("Calculating offsets, do not move MPU6050"));
|
|
|
|
delay(1000);
|
|
|
|
mpu.calcOffsets(true,true); // gyro and accelero
|
|
|
|
Serial.println("Done!\n");
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
float calculateOrientationData() {
|
|
|
|
mpu.update();
|
|
|
|
float accCombined = sqrt(pow(mpu.getAccX(), 2) + pow(mpu.getAccY(), 2) + pow(mpu.getAccZ(), 2));
|
2021-02-14 13:13:53 +00:00
|
|
|
|
|
|
|
// Print the values on the serial monitor
|
2021-02-14 16:42:09 +00:00
|
|
|
if (debug <= 1) {
|
|
|
|
Serial.print(F("TEMPERATURE: "));Serial.println(mpu.getTemp());
|
|
|
|
Serial.print(F("ACCELERO X: "));Serial.print(mpu.getAccX());
|
|
|
|
Serial.print("\tY: ");Serial.print(mpu.getAccY());
|
|
|
|
Serial.print("\tZ: ");Serial.println(mpu.getAccZ());
|
|
|
|
|
|
|
|
Serial.print(F("GYRO X: "));Serial.print(mpu.getGyroX());
|
|
|
|
Serial.print("\tY: ");Serial.print(mpu.getGyroY());
|
|
|
|
Serial.print("\tZ: ");Serial.println(mpu.getGyroZ());
|
|
|
|
|
|
|
|
Serial.print(F("ACC ANGLE X: "));Serial.print(mpu.getAccAngleX());
|
|
|
|
Serial.print("\tY: ");Serial.println(mpu.getAccAngleY());
|
|
|
|
|
|
|
|
Serial.print(F("ANGLE X: "));Serial.print(mpu.getAngleX());
|
|
|
|
Serial.print("\tY: ");Serial.print(mpu.getAngleY());
|
|
|
|
Serial.print("\tZ: ");Serial.println(mpu.getAngleZ());
|
|
|
|
}
|
|
|
|
if (debug <= 2) {
|
|
|
|
Serial.print("ACC COMBINED: ");
|
|
|
|
Serial.println(accCombined, 3);
|
|
|
|
}
|
|
|
|
return accCombined;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawAccelerationOnStrip(int strip, float acceleration){ // 0 1
|
|
|
|
if (debug <= 2) {
|
|
|
|
Serial.print("drawing strip ");
|
|
|
|
Serial.print(strip);
|
2021-02-14 13:13:53 +00:00
|
|
|
Serial.print("\t");
|
2021-02-14 16:42:09 +00:00
|
|
|
Serial.print(acceleration, 4);
|
2021-02-14 13:13:53 +00:00
|
|
|
Serial.print("\t");
|
2021-02-14 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int stripStart = NUM_LEDS * strip; // 10*0 0
|
|
|
|
if (debug <= 2) {
|
|
|
|
Serial.print(stripStart);
|
2021-02-14 13:13:53 +00:00
|
|
|
Serial.print("\t");
|
2021-02-14 16:42:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ledcutoff = stripStart + int((NUM_LEDS/(2*range))*acceleration - (NUM_LEDS/2)); // 0+(10/(2*0,5)*1-10/2) 5
|
|
|
|
if (debug <= 2) {
|
|
|
|
Serial.print(ledcutoff);
|
2021-02-14 13:13:53 +00:00
|
|
|
Serial.print("\t");
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
int stripEnd = (NUM_LEDS * (strip + 1)) - 1; // (10*(0+1))-1 9
|
|
|
|
if (debug <= 2) {
|
|
|
|
Serial.println(stripEnd);
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
2021-02-14 16:42:09 +00:00
|
|
|
|
|
|
|
if ((ledcutoff < stripStart) || (ledcutoff > stripEnd)){
|
|
|
|
for (int i = stripStart; i <= stripEnd; i++) {
|
|
|
|
leds[i] = CRGB(0, 0, 64);
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:42:09 +00:00
|
|
|
else {
|
|
|
|
for (int i = stripStart; i <= ledcutoff; i++) {
|
|
|
|
leds[i] = CRGB(64, 0, 0);
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
2021-02-14 16:42:09 +00:00
|
|
|
for (int i = ledcutoff; i <= stripEnd; i++) {
|
|
|
|
leds[i] = CRGB(0, 64, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void enableLEDsOnAcceleration(float accCombined){
|
|
|
|
// draw all stored accelerations
|
|
|
|
for (int i = 0; i < STRIPS; i++){
|
|
|
|
drawAccelerationOnStrip(i, accelerationHistory[i]);
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
2021-02-14 16:42:09 +00:00
|
|
|
//shift them to the front
|
|
|
|
for (int i = 0; i < STRIPS-1; i++) {
|
|
|
|
accelerationHistory[i] = accelerationHistory[i+1];
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
2021-02-14 16:42:09 +00:00
|
|
|
//add new entry at the end
|
|
|
|
accelerationHistory[STRIPS-1] = accCombined;
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
void calculateFire(int strip){
|
|
|
|
// Array of temperature readings at each simulation cell
|
|
|
|
static byte heat[STRIPS][NUM_LEDS];
|
|
|
|
|
|
|
|
// Step 1. Cool down every cell a little
|
|
|
|
for( int i = 0; i < NUM_LEDS; i++) {
|
|
|
|
heat[strip][i] = qsub8( heat[strip][i], random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2. Heat from each cell drifts 'up' and diffuses a little
|
|
|
|
for( int k= NUM_LEDS - 1; k >= 2; k--) {
|
|
|
|
heat[strip][k] = (heat[strip][k - 1] + heat[strip][k - 2] + heat[strip][k - 2] ) / 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3. Randomly ignite new 'sparks' of heat near the bottom
|
|
|
|
if( random8() < SPARKING ) {
|
|
|
|
int y = random8(7);
|
|
|
|
heat[strip][y] = qadd8( heat[strip][y], random8(160,255) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 4. Map from heat cells to LED colors
|
|
|
|
for( int j = 0; j < NUM_LEDS; j++) {
|
|
|
|
// Scale the heat value from 0-255 down to 0-240
|
|
|
|
// for best results with color palettes.
|
|
|
|
byte colorindex = scale8( heat[strip][j], 240);
|
|
|
|
CRGB color = ColorFromPalette( gPal, colorindex);
|
|
|
|
int pixelnumber = (strip * NUM_LEDS) + j;
|
|
|
|
leds[pixelnumber] = color;
|
|
|
|
}
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
void drawFire(){
|
|
|
|
for (int i = 0; i < STRIPS; i++){
|
|
|
|
calculateFire(i);
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 13:13:53 +00:00
|
|
|
|
2021-02-14 16:42:09 +00:00
|
|
|
void loop() {
|
|
|
|
if (mode == 1) {
|
|
|
|
// === Read acceleromter data === //
|
|
|
|
float accCombined = calculateOrientationData();
|
|
|
|
enableLEDsOnAcceleration(accCombined);
|
|
|
|
FastLED.show();
|
|
|
|
FastLED.delay(1000 / FRAMES_PER_SECOND);
|
|
|
|
}
|
|
|
|
else if (mode == 2) {
|
|
|
|
random16_add_entropy( random());
|
|
|
|
drawFire();
|
|
|
|
FastLED.show();
|
|
|
|
FastLED.delay(1000 / FRAMES_PER_SECOND);
|
|
|
|
}
|
2021-02-14 13:13:53 +00:00
|
|
|
}
|
2021-02-14 16:42:09 +00:00
|
|
|
|
|
|
|
|