I'm building a heart-rate monitor with a built in accelerometer as a research project. I'm using the Yún board for this because of the built-in SD compatibility. I have not yet started implementing the heart rate part, since I've got stuck on the accelerometer. I can measure properly, but I can't seem to time the measurement. Since I want to be able to later do an FFT on the heart rate, and would like to only use one file for the data I would like for the measurement to happen at regular intervals.
I have managed to use timers to blink a LED and print some stuff, and I have managed to use all the components I want to use in the code together (timers, accelerometer, SD-card), but not quickly enough (due to the filesystem being slow in the connection) which made me use the QueueArray class as a FIFO.
When I load this sketch below to the board nothing happens. The sketch seems to upload correctly according to the IDE, but only the ON LED is lighting up, as opposed to when the measurement works and all LEDs, except WiFi, light up. Does anyone have any idea as to what might be going on?
// include queue library header.
#include <QueueArray.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include <FileIO.h>
// declare a string message.
const String msg = "Happy Hacking!";
MPU6050 accelgyro;
#define ledPin 13
int timer1_counter;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t mx, my, mz;
int time;
//String DataString;
// create a queue of characters.
QueueArray <int> queue;
File dataFile;
// startup point entry (runs once).
void setup () {
Wire.begin();
// start serial communication.
Serial.begin (9600);
Bridge.begin();
pinMode(ledPin, OUTPUT);
// set the printer of the queue.
queue.setPrinter (Serial);
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
// Set timer1_counter to the correct value for our interrupt interval
//timer1_counter = 64886; // preload timer 65536-16MHz/256/100Hz
//timer1_counter = 64286; // preload timer 65536-16MHz/256/50Hz
timer1_counter = 3286; // preload timer 65536-16MHz/256/10Hz
TCNT1 = timer1_counter; // preload timer
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
}
//Function to be triggered by the timer
ISR(TIMER1_OVF_vect) // interrupt service routine
{
interrupts();
TCNT1 = timer1_counter; // preload timer
digitalWrite(ledPin, digitalRead(ledPin) ^ 1);
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz);
int DataArray[10]={time,ax,ay,az,gx,gy,gz,mx,my,mz};
for (int i = 0; i < 10; i++)
queue.push (DataArray[i]);
}
// loop the main sketch.
void
loop () {
// pop all the message's characters from the queue.
Serial.println(millis());
while (!queue.isEmpty ()){
Serial.println (queue.pop());
}
}