I have an Arduino nano connected to an MMA 8452 accelerometer and an amplified speaker. I am looking to collect data from the accelerometer and play sounds through a speaker connnected to the amplifier. I am using 330 Ohms pull up resistors across I2C lines.The problem is that after a while the arduino stops receiving data from the accelerometer. I have to replug the power supply to get it to work again.
I did something similar earlier with an arduino Uno and adxl 345 accelerometer and it was working perfectly. In that instance I used a sparkfun musical instrument shield which communicated over software serial. Now I am using another synth library which uses the hardware serial.
Here's my code :-
#include <Wire.h> // Must include Wire library for I2C
#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library
#include <synth.h> // Include the synth library
synth edgar; // Create a synth instance
MMA8452Q accel; // Create an accelerometer instance
// Declare the variables for Co-ordinates
double x = 0.0;
double y = 0.0;
double z = 0.0;
void setup()
{
Serial.begin(9600);
if(!accel.init(SCALE_8G))
{ // Initialize the accelerometer
Serial.println("Ooops, no accelerometer detected ... Check your wiring!");
while(1);
}
edgar.begin(); // Initialize the synth
//Set up the voices:
//setupVoice( voice[0-3] , waveform[SINE,TRIANGLE,SQUARE,SAW,RAMP,NOISE] , pitch[0-127], envelope[ENVELOPE0-ENVELOPE3], length[0-127], mod[0-127, 64=no mod])
edgar.setupVoice(0,SQUARE,60,ENVELOPE1,60,64);
edgar.setupVoice(1,RAMP,0,ENVELOPE3,60,64);
edgar.setupVoice(2,TRIANGLE,0,ENVELOPE2 ,60,64);
edgar.setupVoice(3,NOISE,0,ENVELOPE3,20,64);
}
// The loop function will simply check for new data from the
// accelerometer and print it out if it's available.
void loop()
{
// Use the accel.available() function to wait for new data
// from the accelerometer.
if (accel.available())
{
// First, use accel.read() to read the new variables:
accel.read();
// Getting the values from the accelerometer
x = accel.cx;
y = accel.cy;
z = accel.cz;
if (abs(y) > 0.5 /*&& abs(y) < 4.0 */) {
edgar.setLength(3,60);
edgar.setMod(3, x*10 +64);
edgar.mTrigger(3,20);
delay(150);
}
if (abs(z) > 0.5 /* && abs(z) < 4.0 */) {
edgar.setMod(0, x*10 + 64);
edgar.setMod(1, x*10 + 64); edgar.mTrigger(0, 40);
edgar.mTrigger(1, 40);
delay(150);
}
}
printCalculatedAccels();
//printAccels(); // Uncomment to print digital readings
Serial.println(); // Print new line every time.
}
void printAccels()
{
Serial.print(accel.x, 3);
Serial.print("\t");
Serial.print(accel.y, 3);
Serial.print("\t");
Serial.print(accel.z, 3);
Serial.print("\t");
}
void printCalculatedAccels()
{
Serial.print(accel.cx, 3);
Serial.print("\t");
Serial.print(accel.cy, 3);
Serial.print("\t");
Serial.print(accel.cz, 3);
Serial.print("\t");
}
void printOrientation()
{
// accel.readPL() will return a byte containing information
// about the orientation of the sensor. It will be either
// PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
// LOCKOUT.
byte pl = accel.readPL();
switch (pl)
{
case PORTRAIT_U:
Serial.print("Portrait Up");
break;
case PORTRAIT_D:
Serial.print("Portrait Down");
break;
case LANDSCAPE_R:
Serial.print("Landscape Right");
break;
case LANDSCAPE_L:
Serial.print("Landscape Left");
break;
case LOCKOUT:
Serial.print("Flat");
break;
}
}
Does it have anything to do with serial library?