Hi, so I have an Adafruit 8x8 LED Matrix (the 0.8") one, an Arduino Uno and a Pulse sensor from pulsesensor.com
When the IBI varies too much, I want it to use the LED Matrix to output the text I have placed.
This is the code placed in the Interrupt part of the Pulse sensor sequence:
if (IBI - rate[8] <= -90){ //when the difference between the most recent IBI and the one before IBI is less than -90
delay(1000000);
if (IBI - rate[8] <= -90) {
delay(100000);
if (IBI - rate[8] <= -90) {
//do something
}
else {
}
}
else {
}
}
else {
}
// QS FLAG IS NOT CLEARED INSIDE THIS ISR
}
}
However, when I try combining the two together, the LED Matrix just stops and glitches out and the pulse sensor doesn't work at all.
This is my attempt at the main file:
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
// Variables
int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
int blinkPin = 13; // pin to blink led at each beat
int fadePin = 5; // pin to do fancy classy fading blink at each beat
int fadeRate = 0; // used to fade LED on with PWM on fadePin
// Volatile Variables, used in the interrupt service routine!
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
// Regards Serial OutPut -- Set This Up to your needs
static boolean serialVisual = true; // Set to 'false' by Default. Re-set to 'true' to see Arduino Serial Monitor ASCII Visual Pulse
void setup(){
interruptSetup(); // sets up to read Pulse Sensor signal every 2mS
// IF YOU ARE POWERING The Pulse Sensor AT VOLTAGE LESS THAN THE BOARD VOLTAGE,
// UN-COMMENT THE NEXT LINE AND APPLY THAT VOLTAGE TO THE A-REF PIN
// analogReference(EXTERNAL);
matrix.begin(0x70); // pass in the address
}
// Where the Magic Happens
void loop(){
matrix.clear();
serialOutput() ;
if (QS == true){ // A Heartbeat Was Found
// BPM and IBI have been Determined
// Quantified Self "QS" true when arduino finds a heartbeat
serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
QS = false; // reset the Quantified Self flag for next time
}
}
This is what I have inputted into the Interrupt ino file that depends on the main file (the one above):
// 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
if (IBI - rate[8] <= -90){ //when the difference between the most recent IBI and the one before IBI is less than -90
delay(1000000);
if (IBI - rate[8] <= -90) {
delay(100000);
if (IBI - rate[8] <= -90) {
matrix.setBrightness(3);
matrix.setTextSize(1);
matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely
matrix.setTextColor(LED_ON);
for (int8_t x=0; x>=-45; x--) {
matrix.clear();
matrix.setCursor(x,0);
matrix.print("In 2 3 4");
matrix.writeDisplay();
delay(100);
}
for (int8_t x=9; x>=-90; x--) {
matrix.clear();
matrix.setCursor(x,0);
matrix.print("Hold 2 3 4 5 6 7");
matrix.writeDisplay();
delay(100);
}
for (int8_t x=16; x>=-103; x--) {
matrix.clear();
matrix.setCursor(x,0);
matrix.print("Out 2 3 4 5 6 7 8");
matrix.writeDisplay();
delay(100);
}
matrix.setRotation(0);
}
else {
matrix.clear();
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
delay(500);
}
}
else {
matrix.clear();
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
delay(500);
}
}
else {
matrix.clear();
matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);
matrix.writeDisplay();
delay(500);
}
// QS FLAG IS NOT CLEARED INSIDE THIS ISR
}
}
if (Signal < thresh && Pulse == true){ // when the values are going down, the beat is over
// digitalWrite(blinkPin,LOW); // turn off pin 13 LED
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
If you could help that would be so great!!!