Audio fine until mpu6050 code is added

Hello everyone,
I'm working on a project that uses a speaker and an MPU6050 gyroscope/accelerometer. The audio was playing perfectly fine, but after introducing the code for the MPU it suddenly turned into a small, high-pitched squeal.

#include <avr/pgmspace.h>
#include <SD.h>
#include <TMRpcm.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include <toneAC.h>
#include "FastLED.h"
#include <EEPROM.h>
#include <SPI.h>
#define SD_ChipSelectPin 8
#define LED_PIN 6
#define NUM_LEDS 124
#define BRIGHTNESS 255  
#define IMU_GND A1
#define SWING_TIMEOUT 500   // timeout between swings
#define SWING_L_THR 150     // swing angle speed threshold
#define SWING_THR 300       // fast swing angle speed threshold
#define STRIKE_THR 150      // hit acceleration threshold
#define STRIKE_S_THR 320    // hard hit acceleration threshold
#define FLASH_DELAY 80      // flash time while hit
TMRpcm tmrpcm;
CRGB leds[NUM_LEDS];
MPU6050 accelgyro;

const char bpin = 3;
bool on = false;
bool off = true;
bool lsOn = false;
bool pressed = false;

byte LEDcolor;  // 0 - red, 1 - green, 2 - blue, 3 - pink, 4 - yellow, 5 - ice blue
byte nowColor, red, green, blue, redOffset, greenOffset, blueOffset;

// ------------------------------ VARIABLES ---------------------------------
int16_t ax, ay, az;
int16_t gx, gy, gz;
unsigned long ACC, GYR, COMPL;
int gyroX, gyroY, gyroZ, accelX, accelY, accelZ, freq, freq_f = 20;
float k = 0.2;
unsigned long humTimer = -9000, mpuTimer, nowTimer;
int stopTimer;
boolean bzzz_flag, ls_chg_state, ls_state;
boolean btnState, btn_flag, hold_flag;
byte btn_counter;
unsigned long btn_timer, PULSE_timer, swing_timer, swing_timeout, battery_timer, bzzTimer;
byte nowNumber;
boolean eeprom_flag, swing_flag, swing_allow, strike_flag;
float voltage;
int PULSEOffset;
// ------------------------------ VARIABLES ---------------------------------


// --------------------------------- SOUNDS ----------------------------------
const char strike1[] PROGMEM = "SK1.wav";
const char strike2[] PROGMEM = "SK2.wav";
const char strike3[] PROGMEM = "SK3.wav";
const char strike4[] PROGMEM = "SK4.wav";
const char strike5[] PROGMEM = "SK5.wav";
const char strike6[] PROGMEM = "SK6.wav";
const char strike7[] PROGMEM = "SK7.wav";
const char strike8[] PROGMEM = "SK8.wav";

const char* const strikes[] PROGMEM  = {
  strike1, strike2, strike3, strike4, strike5, strike6, strike7, strike8
};

int strike_time[8] = {779, 563, 687, 702, 673, 661, 666, 635};

const char strike_s1[] PROGMEM = "SKS1.wav";
const char strike_s2[] PROGMEM = "SKS2.wav";
const char strike_s3[] PROGMEM = "SKS3.wav";
const char strike_s4[] PROGMEM = "SKS4.wav";
const char strike_s5[] PROGMEM = "SKS5.wav";
const char strike_s6[] PROGMEM = "SKS6.wav";
const char strike_s7[] PROGMEM = "SKS7.wav";
const char strike_s8[] PROGMEM = "SKS8.wav";

const char* const strikes_short[] PROGMEM = {
  strike_s1, strike_s2, strike_s3, strike_s4,
  strike_s5, strike_s6, strike_s7, strike_s8
};
int strike_s_time[8] = {270, 167, 186, 250, 252, 255, 250, 238};

const char swing1[] PROGMEM = "SWS1.wav";
const char swing2[] PROGMEM = "SWS2.wav";
const char swing3[] PROGMEM = "SWS3.wav";
const char swing4[] PROGMEM = "SWS4.wav";
const char swing5[] PROGMEM = "SWS5.wav";

const char* const swings[] PROGMEM  = {
  swing1, swing2, swing3, swing4, swing5
};
int swing_time[8] = {389, 372, 360, 366, 337};

const char swingL1[] PROGMEM = "SWL1.wav";
const char swingL2[] PROGMEM = "SWL2.wav";
const char swingL3[] PROGMEM = "SWL3.wav";
const char swingL4[] PROGMEM = "SWL4.wav";

const char* const swings_L[] PROGMEM  = {
  swingL1, swingL2, swingL3, swingL4
};
int swing_time_L[8] = {636, 441, 772, 702};

char BUFFER[10];
// --------------------------------- SOUNDS ---------------------------------


void setup() {
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(150);
  setAll(0, 0, 0);
  Wire.begin();
  Serial.begin(9600);
  tmrpcm.speakerPin=9;
  pinMode(bpin, INPUT_PULLUP);
  pinMode(IMU_GND, OUTPUT);
  
  randomSeed(analogRead(2));    // starting point for random generator

  accelgyro.initialize();
  accelgyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_16);
  accelgyro.setFullScaleGyroRange(MPU6050_GYRO_FS_250);

}

void loop() {
  on_and_off();
}

void on_and_off(){
  bool buttonStatus = digitalRead(bpin);
  if (buttonStatus==pressed and lsOn==false){
    Serial.println("pressed");
    tmrpcm.setVolume(5);
    tmrpcm.quality(1);
    SD.begin(8);
    setColor(2);
    tmrpcm.play("ON.WAV");
    light_up();
    delay(600);
    lsOn=true;
  }
  else if (buttonStatus==pressed and lsOn==true){
    Serial.println("pressed");
    tmrpcm.setVolume(5);
    tmrpcm.quality(1);
    SD.begin(8);
    tmrpcm.play("OFF.WAV");
    light_down();
    delay(500);
    lsOn=false;
  }
  else if (buttonStatus!=pressed){
    tmrpcm.disable();
    Serial.println("not pressed");
  }
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
  leds[Pixel].r = red;
  leds[Pixel].g = green;
  leds[Pixel].b = blue;
}

void setColor(byte color) {
  switch (color) {
    // 0 - red, 1 - blue, 2 - green, 3 - pink, 4 - yellow, 5 - ice blue
    case 0:
      red = 255;
      green = 0;
      blue = 0;
      break;
    case 1:
      red = 0;
      green = 0;
      blue = 255;
      break;
    case 2:
      red = 0;
      green = 255;
      blue = 0;
      break;
    case 3:
      red = 255;
      green = 0;
      blue = 255;
      break;
    case 4:
      red = 255;
      green = 255;
      blue = 0;
      break;
    case 5:
      red = 0;
      green = 255;
      blue = 255;
      break;
  }
}

void setAll(byte red, byte green, byte blue) {
  for (int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  FastLED.show();
}

void light_up() {
  for (char i = 0; i <= (NUM_LEDS); i++) {        
    setPixel(i, red, green, blue);
    FastLED.show();
    delay(3);
  }
}

void light_down() {
  for (char i = (NUM_LEDS); i >= 0; i--) {      
    setPixel(i, 0, 0, 0);
    FastLED.show();
    delay(3);
  }
}

The last three lines of code under the setup are causing the issue. When they are deleted, it works fine. I think you can ignore the everything after void on_and_off(), but I included it just in case.

I would really appreciate any input.

It looks like a memory problem. What is Arduino IDE saying after compile?

Your program is filled with "heavy" libraries - FastLED, SD, TMRpcm... The SD library occupies 500 bytes for the card buffer, 124 LEDs - about 400 more bytes. The music playback library also has its own buffer. And the classic Nano has only 2k of memory.
I think you need to change the board, for example, to Mega or Nano Every

I might be wrong about the arduino model because I'm using an Inland Nano. It has 32 kb of flash memory. After compile, the IDE says:

Sketch uses 20390 bytes (66%) of program storage space. Maximum is 30720 bytes.
Global variables use 1759 bytes (85%) of dynamic memory, leaving 289 bytes for local variables. Maximum is 2048 bytes.

Could it still be a memory problem?

Yes, it certainly could be a memory problem.

1 Like

No, it is a another memory.

All lines like can benefit from the F-macro. Change all serial prints to something like

Serial.println(F("pressed"));

This one might also work (not sure)

tmrpcm.play(F("ON.WAV"));

Unfortunately you do not have many of those so the benefit will not be big.

Are you sure it can be used with tmrpcm? I tried it and got several errors.

As I said

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.