From 13b4aa1d4bb5483ec54644de28da8f7ea263ce03 Mon Sep 17 00:00:00 2001 From: Sophie Schiller Date: Sun, 14 Feb 2021 17:42:09 +0100 Subject: [PATCH] acceleration graph and fire --- platformio.ini | 4 +- src/main.ino | 305 ++++++++++++++++++++++++------------------------- 2 files changed, 154 insertions(+), 155 deletions(-) diff --git a/platformio.ini b/platformio.ini index 4e15e48..3dcb42a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,4 +12,6 @@ platform = atmelavr board = uno framework = arduino -lib_deps = fastled/FastLED@^3.4.0 +lib_deps = + rfetick/MPU6050_light@^1.1.0 + fastled/FastLED@^3.4.0 diff --git a/src/main.ino b/src/main.ino index 723c17b..c407b85 100644 --- a/src/main.ino +++ b/src/main.ino @@ -5,189 +5,186 @@ #include #include +#include #define LED_PIN 7 -#define NUM_LEDS 20 -const int MPU = 0x68; // MPU6050 I2C address -float AccX, AccY, AccZ; -float GyroX, GyroY, GyroZ; -float accAngleX, accAngleY, accCombined, gyroAngleX, gyroAngleY, gyroAngleZ; -float roll, pitch, yaw; -float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ; -float elapsedTime, currentTime, previousTime; -int c = 0; -boolean debug = true; +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 -CRGB leds[NUM_LEDS]; +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; void setup() { Serial.begin(19200); //setup LEDs - FastLED.addLeds(leds, NUM_LEDS); - - //setup IMU - Wire.begin(); // Initialize comunication - Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68 - Wire.write(0x6B); // Talk to the register 6B - Wire.write(0x00); // Make reset - place a 0 into the 6B register - Wire.endTransmission(true); //end the transmission + FastLED.addLeds(leds, NUM_LEDS*STRIPS).setCorrection( TypicalLEDStrip ); + FastLED.setBrightness( BRIGHTNESS ); - // Configure Accelerometer Sensitivity - Full Scale Range (default +/- 2g) - /*Wire.beginTransmission(MPU); - Wire.write(0x1C); //Talk to the ACCEL_CONFIG register (1C hex) - Wire.write(0x10); //Set the register bits as 00010000 (+/- 8g full scale range) - Wire.endTransmission(true); - // Configure Gyro Sensitivity - Full Scale Range (default +/- 250deg/s) - Wire.beginTransmission(MPU); - Wire.write(0x1B); // Talk to the GYRO_CONFIG register (1B hex) - Wire.write(0x10); // Set the register bits as 00010000 (1000deg/s full scale) - Wire.endTransmission(true); - */ - delay(20); + gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White); + //gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua, CRGB::White); - // Call this function if you need to get the IMU error values for your module - //calculate_IMU_error(); - delay(200); + for (int i = 0; i stripEnd)){ + for (int i = stripStart; i <= stripEnd; i++) { + leds[i] = CRGB(0, 0, 64); + } + } + else { + for (int i = stripStart; i <= ledcutoff; i++) { + leds[i] = CRGB(64, 0, 0); + } + for (int i = ledcutoff; i <= stripEnd; i++) { + leds[i] = CRGB(0, 64, 0); + } } } -void enableLEDsOnAcceleration(){ - int ledcutoff = int(accCombined * NUM_LEDS / 2); - if(debug) { - Serial.print("["); +void enableLEDsOnAcceleration(float accCombined){ + // draw all stored accelerations + for (int i = 0; i < STRIPS; i++){ + drawAccelerationOnStrip(i, accelerationHistory[i]); } - for (int i = 0; i <= ledcutoff; i++) { - leds[i] = CRGB(64, 0, 0); - if (debug) { - Serial.print("|"); - } + //shift them to the front + for (int i = 0; i < STRIPS-1; i++) { + accelerationHistory[i] = accelerationHistory[i+1]; } - for (int i = ledcutoff; i <= NUM_LEDS ; i++) { - leds[i] = CRGB(0, 64, 0); - if (debug) { - Serial.print("-"); - } + //add new entry at the end + accelerationHistory[STRIPS-1] = accCombined; +} + +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)); } - if(debug) { - Serial.println("]"); + + // 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; + } +} + +void drawFire(){ + for (int i = 0; i < STRIPS; i++){ + calculateFire(i); } - FastLED.show(); } void loop() { - // === Read acceleromter data === // - calculateOrientationData(); - enableLEDsOnAcceleration(); - delay(20); + 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); + } } -void calculate_IMU_error() { - // We can call this funtion in the setup section to calculate the accelerometer and gyro data error. From here we will get the error values used in the above equations printed on the Serial Monitor. - // Note that we should place the IMU flat in order to get the proper values, so that we then can the correct values - // Read accelerometer values 200 times - while (c < 200) { - Wire.beginTransmission(MPU); - Wire.write(0x3B); - Wire.endTransmission(false); - Wire.requestFrom(MPU, 6, true); - AccX = (Wire.read() << 8 | Wire.read()) / 16384.0 ; - AccY = (Wire.read() << 8 | Wire.read()) / 16384.0 ; - AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0 ; - // Sum all readings - AccErrorX = AccErrorX + ((atan((AccY) / sqrt(pow((AccX), 2) + pow((AccZ), 2))) * 180 / PI)); - AccErrorY = AccErrorY + ((atan(-1 * (AccX) / sqrt(pow((AccY), 2) + pow((AccZ), 2))) * 180 / PI)); - c++; - } - //Divide the sum by 200 to get the error value - AccErrorX = AccErrorX / 200; - AccErrorY = AccErrorY / 200; - c = 0; - // Read gyro values 200 times - while (c < 200) { - Wire.beginTransmission(MPU); - Wire.write(0x43); - Wire.endTransmission(false); - Wire.requestFrom(MPU, 6, true); - GyroX = Wire.read() << 8 | Wire.read(); - GyroY = Wire.read() << 8 | Wire.read(); - GyroZ = Wire.read() << 8 | Wire.read(); - // Sum all readings - GyroErrorX = GyroErrorX + (GyroX / 131.0); - GyroErrorY = GyroErrorY + (GyroY / 131.0); - GyroErrorZ = GyroErrorZ + (GyroZ / 131.0); - c++; - } - //Divide the sum by 200 to get the error value - GyroErrorX = GyroErrorX / 200; - GyroErrorY = GyroErrorY / 200; - GyroErrorZ = GyroErrorZ / 200; - // Print the error values on the Serial Monitor - Serial.print("AccErrorX: "); - Serial.println(AccErrorX); - Serial.print("AccErrorY: "); - Serial.println(AccErrorY); - Serial.print("GyroErrorX: "); - Serial.println(GyroErrorX); - Serial.print("GyroErrorY: "); - Serial.println(GyroErrorY); - Serial.print("GyroErrorZ: "); - Serial.println(GyroErrorZ); -}