Hello all,
Basically I have merged code for the Pulse Sensor Amped (PulseSensor.com – World Famous Electronics llc.) and the NeoPixel sheild (Adafruit NeoPixel Shield for Arduino - 40 RGB LED Pixel Matrix : ID 1430 : $27.95 : Adafruit Industries, Unique & fun DIY electronics and kits)
The idea: that the LED matrix dims and brightens (as a meditation breath guide) whilst altering its colour based as the heart rate (BPM) becomes more relaxed (can only be 3 different colours)
The problem: The merged code has never worked and sometimes causes an error message and sometimes not. When I run this code to the arduino the Neopixel sheild perfroms the dimming and brightening loop fine. The issue is that it does not seem to do any of the Baseline BPM calculations or the Switch Case which should be changin the colour of the LEDs.
Error Message: Sometimes it does not compile and says "InterruptSetup" was not declared on this scope.
Any help or advice on how to amend this issue would be greatly appreciated, as I cant work out why it wont take the average BPM in the setup and why the colours wont change when the BPM drops.
#include <LiquidCrystal.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 6
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 64
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 0;
int BRIGHTNESS = 60;
// Variables Pulse
int pulsePin = 0; // Pulse Sensor purple wire connected to analog pin 0
int blinkPin = 13; // pin to blink led at each beat
int fadePin = 8; // pin to do fancy classy fading blink at each beat
int fadeRate = 0; // used to fade LED on with PWM on fadePin
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// 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.
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 = 525; // 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
int baseline_average = 0;
int bpm_sum = 0;
void setup() {
pixels.setBrightness(BRIGHTNESS);
pixels.begin(); // This initializes the NeoPixel library.
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(255, 0, 0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
pinMode(blinkPin, OUTPUT); // pin that will blink to your heartbeat!
pinMode(fadePin, OUTPUT); // pin that will fade to your heartbeat!
Serial.begin(115200); // we agree to talk fast!
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);
for (int i = 0; i < 50; i++)
{
if (QS == true) // A Heartbeat Was Found
{
// BPM and IBI have been Determined
// Quantified Self "QS" true when arduino finds a heartbeat
fadeRate = 255; // Makes the LED Fade Effect Happen, Set 'fadeRate' Variable to 255 to fade LED with pulse
serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
QS = false; // reset the Quantified Self flag for next time
bpm_sum = bpm_sum + BPM;
}
}
baseline_average = bpm_sum / 50;
}
void loop() {
int b = 60;
serialOutput();
if (QS == true) // A Heartbeat Was Found
{
fadeRate = 255;
serialOutputWhenBeatHappens(); // A Beat Happened, Output that to serial.
QS = false; // reset the Quantified Self flag for next time
}
// Exhale
while (b > 1) {
BRIGHTNESS--;
b--;
pixels.setBrightness(BRIGHTNESS);
pixels.show();
delay(50);
}
//Cases which amend LED color
switchLED();
//Inhale
while (b < 60) {
BRIGHTNESS++;
b++;
pixels.setBrightness(BRIGHTNESS);
pixels.show();
delay(100);
}
}
void switchLED() {
switch (category_bpm()) {
case 1:
{
for (int z = 0; z < NUMPIXELS; z++) {
pixels.setPixelColor(z, pixels.Color(255, 0, 0));
pixels.show();
}
}
break;
case 2:
{
for (int z = 0; z < NUMPIXELS; z++) {
pixels.setPixelColor(z, pixels.Color(255, 200, 0));
pixels.show();
}
}
break;
case 3:
{ for (int z = 0; z < NUMPIXELS; z++) {
pixels.setPixelColor(z, pixels.Color(0, 255, 0));
pixels.show();
}
}
break;
default:
{ for (int z = 0; z < NUMPIXELS; z++) {
pixels.setPixelColor(z, pixels.Color(0, 0, 255));
pixels.show();
}
}
break;
}
}
//Compare Baseline BPM against current BPM for any difference
int category_bpm() {
if (sqrt((baseline_average - BPM) * (baseline_average - BPM)) < 2) return 1;
else if (sqrt((baseline_average - BPM) * (baseline_average - BPM)) > 2 && sqrt((baseline_average - BPM) * (baseline_average - BPM)) < 4) return 2;
else if (sqrt((baseline_average - BPM) * (baseline_average - BPM)) < 5) return 3;
}
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
}
void serialOutput()
{
sendDataToSerial('S', Signal);
}
void serialOutputWhenBeatHappens()
{
sendDataToSerial('B', BPM);
sendDataToSerial('Q', IBI);
}
void sendDataToSerial(char symbol, int data )
{
Serial.print(symbol);
Serial.println(data);
}
ISR(TIMER2_COMPA_vect)
// REMOVED FOR MAXIMUM ALLOWED LENGTH ON FORUM