2

I am trying to create an Arduino program that combines a pulse sensor (that measures the Beats Per Minutes) and a music shield. I am experiencing some difficulties with the coding because the pulse sensor has an extensive interrupt routine that occurs every 2ms while the music shield has its own interrupt. Through research I have found that the Arduino Uno cannot have two interrupt routines at the same time.

Is it possible to code around this issue in order to have both the interrupt routines worK?

I am attaching the code below:

//Include Music Shield Library
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// Define everything needed for the Music Shield 
#define BREAKOUT_RESET  9     // VS1053 reset pin (output)
#define BREAKOUT_CS     10    // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8     // VS1053 Data/command select pin (output)
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7       // VS1053 chip select pin (output)
#define SHIELD_DCS    6       // VS1053 Data/command select pin (output)
#define CARDCS 4              // Card chip select pin
#define DREQ 3                // VS1053 Data request, ideally an Interrupt pin

//Explain what musicplayer means
Adafruit_VS1053_FilePlayer musicPlayer = 
  // create breakout-example object!
  //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
  // create shield-example object!
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);

//Variables for the Pulse Sensor 
int pulsePin = 0;                   // Pulse Sensor purple wire connected to analog pin 0
//These variables are volatile because they are used during the interrupt service routine!
volatile int BPM;                   // used to hold the pulse rate
volatile int Signal;                // holds the incoming raw data
volatile int IBI = 600;             // holds the time between beats, must be seeded! 
volatile boolean Pulse = false;     // true when pulse wave is high, false when it's low
volatile boolean QS = false;        // becomes true when Arduoino finds a beat.

//Define integer range for the switchcase map function 
const int sensorMin = 70;           // sensor minimum, dicovered through research MAY CHANGE
const int sensorMax = 230;          // sensor maximum, discovered through research MAY CHANGE

//Now we are going to set up the sensor and the shield. 
//The swtichcase does not need setup because we are not using the serial monitor for it.
void setup(){
Serial.begin(115200);
  Serial.println("Adafruit VS1053 Simple Test");
  interruptSetup();                 // sets up to read Pulse Sensor signal every 2mS 
//Now we setup the rest of the shield. 

  if (! musicPlayer.begin()) {      // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));

  SD.begin(CARDCS);    // initialise the SD card

  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(05,05);

  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);  // DREQ int

}                  
//Void setup is done, now we tell the arduino what to do with all this information. 

void loop(){
   // read the sensor:
  int sensorReading = analogRead(A0);
  //A0 should be BPM
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 4);

//In our map range 0=70-110, 1=110-150, 2=150-190, 3=190-230, 4=end(DEAD)
// do something different depending on the 
// range value:
//Track 1 is resting music, track 2 is medium beat, track 3 is fast beat, and track 4 is continous beeps. 
  switch (range) {
  case 0:    // 70BPM - 110 BPM
    Serial.println(F("Playing track 001"));
    musicPlayer.playFullFile("track001.mp3"); 
  case 1:    // 110 BPM - 150 BPM
    Serial.println(F("Playing track 002"));
    musicPlayer.startPlayingFile("track002.mp3");
  case 2:    // 150 BPM - 190 BPM
    Serial.println(F("Playing track 003"));
    musicPlayer.startPlayingFile("track003.mp3");
  case 3:    // 190 BPM - 230 BPM 
    Serial.println(F("Dangerous"));
    musicPlayer.startPlayingFile("track004.mp3");
  case 4:    // DEAD
    Serial.println(F("DEAD"));
    musicPlayer.startPlayingFile("track005.mp3");
    break;
  } 
  delay(1);        //This is the delay of the switchcase. 
                   //The Music Shield Requests 100, so this MAY CHANGE
}




volatile int rate[10];                    // array to hold last ten IBI values
volatile unsigned long sampleCounter = 0;          // used to determine pulse timing
volatile unsigned long lastBeatTime = 0;           // used to find IBI
volatile int P =512;                      // used to find peak in pulse wave, seeded
volatile int T = 512;                     // used to find trough in pulse wave, seeded
volatile int thresh = 512;                // used to find instant moment of heart beat, seeded
volatile int amp = 100;                   // used to hold amplitude of pulse waveform, seeded
volatile boolean firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = false;      // used to seed rate array so we startup with reasonable BPM


void interruptSetup(){     
  // Initializes Timer2 to throw an interrupt every 2mS.
  TCCR2A = 0x02;     // DISABLE PWM ON DIGITAL PINS 3 AND 11, AND GO INTO CTC MODE
  TCCR2B = 0x06;     // DON'T FORCE COMPARE, 256 PRESCALER 
  OCR2A = 0X7C;      // SET THE TOP OF THE COUNT TO 124 FOR 500Hz SAMPLE RATE
  TIMSK2 = 0x02;     // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
  sei();             // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED      
} 


// THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE. 
// Timer 2 makes sure that we take a reading every 2 miliseconds
ISR(TIMER2_COMPA_vect){                         // triggered when Timer2 counts to 124
  cli();                                      // disable interrupts while we do this
  Signal = analogRead(pulsePin);              // read the Pulse Sensor 
  sampleCounter += 2;                         // keep track of the time in mS with this variable
  int N = sampleCounter - lastBeatTime;       // monitor the time since the last beat to avoid noise

    //  find the peak and trough of the pulse wave
  if(Signal < thresh && N > (IBI/5)*3){       // avoid dichrotic noise by waiting 3/5 of last IBI
    if (Signal < T){                        // T is the trough
      T = Signal;                         // keep track of lowest point in pulse wave 
    }
  }

  if(Signal > thresh && Signal > P){          // thresh condition helps avoid noise
    P = Signal;                             // P is the peak
  }                                        // keep track of highest point in pulse wave

  //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  // signal surges up in value every time there is a pulse
  if (N > 250){                                   // avoid high frequency noise
    if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){        
      Pulse = true;                               // set the Pulse flag when we think there is a pulse
      IBI = sampleCounter - lastBeatTime;         // measure time between beats in mS
      lastBeatTime = sampleCounter;               // keep track of time for next pulse

      if(secondBeat){                        // if this is the second beat, if secondBeat == TRUE
        secondBeat = false;                  // clear secondBeat flag
        for(int i=0; i<=9; i++){             // seed the running total to get a realisitic BPM at startup
          rate[i] = IBI;                      
        }
      }

      if(firstBeat){                         // if it's the first time we found a beat, if firstBeat == TRUE
        firstBeat = false;                   // clear firstBeat flag
        secondBeat = true;                   // set the second beat flag
        sei();                               // enable interrupts again
        return;                              // IBI value is unreliable so discard it
      }   


      // keep a running total of the last 10 IBI values
      word runningTotal = 0;                  // clear the runningTotal variable    

      for(int i=0; i<=8; i++){                // shift data in the rate array
        rate[i] = rate[i+1];                  // and drop the oldest IBI value 
        runningTotal += rate[i];              // add up the 9 oldest IBI values
      }

      rate[9] = IBI;                          // add the latest IBI to the rate array
      runningTotal += rate[9];                // add the latest IBI to runningTotal
      runningTotal /= 10;                     // average the last 10 IBI values 
      BPM = 60000/runningTotal;               // how many beats can fit into a minute? that's BPM!
      QS = true;                              // set Quantified Self flag 
      // QS FLAG IS NOT CLEARED INSIDE THIS ISR
    }                       
  }

  if (Signal < thresh && Pulse == true){   // when the values are going down, the beat is over
    Pulse = false;                         // reset the Pulse flag so we can do it again
    amp = P - T;                           // get amplitude of the pulse wave
    thresh = amp/2 + T;                    // set thresh at 50% of the amplitude
    P = thresh;                            // reset these for next time
    T = thresh;
  }

  if (N > 2500){                           // if 2.5 seconds go by without a beat
    thresh = 512;                          // set thresh default
    P = 512;                               // set P default
    T = 512;                               // set T default
    lastBeatTime = sampleCounter;          // bring the lastBeatTime up to date        
    firstBeat = true;                      // set these to avoid noise
    secondBeat = false;                    // when we get the heartbeat back
  }

  sei();                                   // enable interrupts when youre done!
}// end isr
2

I wrote most of the SFEMP3Shield library. Where it appears that AdaFruit based much of theirs on. That said I have used Timer1 library with mine just fine to invoke the filling of the VS1053.

Where I am suspicious that AdaFruit's Library's did not Interrupt lock out the SPI packets. As I did on the SFEMP3Shield library, as to keep other things from messing up the frame. Where a brief skim of your code, initially looks fine.

You have not described your symptoms. Where as using 2ms IRQ's and noting the VS1053 library reads the SdCard in 500byte chunks I image these reads are blocking and corrupting your 2ms ISR.

You can try to slow them down, just to see if they stop getting messed up.

You can try to using the polling method of the VS1053, instead of interrupts.

You can try to use the more feature mature SFEMP3Shield library and its documentation noting that it also uses the more mature SdFat. As the stock SD library may be part of your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.