Accelerometer readings causing music to play


I am new to Arduino and this forum, so apologies if this is in the wrong section. I am trying to work on this project that involves taking the output of an accelerometer (LIS3DH), that I converted gravity to degrees, and once it reads past a certain angle, music should play off of an SD card. I am currently using 2 separate Arduino UNOs to control each piece and have gotten them to work separately. I have no clue where to start to get them to talk to one another and cause a response, or if I am even heading in the right direction.


What do You mean by talk to one another ?

Only very specific messages can be exchanged, following the rules built into the code.

Using several controllers is a good way to make things more difficult, a lot more difficult.
Using one controller looks like possible from this first easy view.

I have been talking to several professors on my campus saying that how the accelerometer continuously updates doesn’t allow for one controller because Arduino is single-threaded. When I tried doing one controller, the continuous updating caused the music to restart every second no matter what orientation. So unless you can give me advice on how to get it working with one controller, that would be great.
I have attached the code we were using for the one controller.

finalcode.ino (1.9 KB)

Professor or not, I don't understand Your description. The controller should read data and perform calculations according to the need of output data. How often a sensor is reacting is not interesting as I Think.
Continues updating…. Makes no sense to me.
Please attache code the way told in advising topis on top of the beginning of the topics types.
Use code tags to attache code. Using tablets, helpers can't read inlude files.

The problem in the code you posted is that you start the music every time pitch or roll are >= 45. If the music is already playing then you should not start it again. Like this:

// Head Balance Team Final Code

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include <pcmConfig.h>
#include <pcmRF.h>
#include <TMRpcm.h>
#include <SD.h>
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"

#define LIS3DH_CLK 8
#define LIS3DH_MISO 7
#define LIS3DH_MOSI 6
#define LIS3DH_CS 5
#define SD_ChipSelectPin 10

TMRpcm tmrpcm;
boolean playing = false;

void setup(void) {
  // put your setup code here, to run once:
  while (!Serial) delay(10);
  Serial.println("LIS3DH test!");

  if (! lis.begin(0x18)){
    Serial.println("Couldnt start");
    while(1) yield();
  Serial.println("LIS3DH found!");

  Serial.print("Range = "); 
  Serial.print(2<< lis.getRange());

  tmrpcm.speakerPin = 9;
    Serial.println("SD fail");

void loop() {
  // put your main code here, to run repeatedly:
  int pitch, roll, yaw;;           // get x y z data at once

  sensors_event_t event; 

  pitch = 180* atan(event.acceleration.x/sqrt(sq(event.acceleration.y)+sq(event.acceleration.z)))/M_PI;
  roll = 180 * atan(event.acceleration.y/sqrt(sq(event.acceleration.x)+sq(event.acceleration.z)))/M_PI;
  yaw = 180* atan(event.acceleration.z/sqrt(sq(event.acceleration.x)+sq(event.acceleration.y)))/M_PI;

  pitch = abs(pitch);
  roll = abs(roll);
  yaw = abs(yaw);

  /* Display the results (acceleration is measured in deg) */
  Serial.print("\t\tX: ");
  Serial.print(" \tY: "); 
  Serial.print(" \tZ: ");
  Serial.print(yaw); yyyh


  if (pitch >= 45 || roll >= 45){
    if (!tmrpcm.isPlaying()){"ocean.wav");
    if (tmrpcm.isPlaying()){

Reading an accelerometer and controlling a DF Player is nothing for a single Arduino. Using two Arduinos is just complicating things unnecessary. In fact, the second Arduino (the one with the DF Player) would basically just be a command relay...

I donno'. I regularly toss in multiple processors when building stuff. Also, the mp3 players I've used, pretty much run a standard arduino 'till its tongue is hanging out.

Just do one wire digital to digital. When its time to play the song, set the output to 1, then the other Arduino "sees" it and starts playing. Easy peasy!

-jim lee