Go Down

Topic: Playground Classic - Meditation Training Software bugs - Anyone help to correct? (Read 1 time) previous topic - next topic

S90JMC

#include <Adafruit_CircuitPlayground.h>
#define FIRST_BREATH_LED 0
#define LAST_BREATH_LED 9
int pulsePin = 7; //Pulse Sensor purple wire connected to analog pin 7
int fadeRate = 0; //used to fade LED
int numPixels = 10;
int breathLED = FIRST_BREATH_LED;
int prevBreathLED = FIRST_BREATH_LED;
bool breathToggle = false;
unsigned long lastBreath = 0;
boolean alive = false; //detecting a heartbeat
unsigned long lastAlive; 
volatile int BPM; //holds analog from Pulse Sensor
volatile int Signal; //holds input from Pulse Sensor
volatile int IBI = 600;
volatile boolean Pulse = false;
volatile boolean QS = false;
volatile int tCoh = 10;
volatile int rCoh = 50;
long randNumber;
long randNumber2;
long randNumber3;
//Serial Output
static boolean serialVisual = false;
unsigned long ledTimes[] = {520, 520, 520, 520, 520, 520, 520, 520, 745, 745}; //times for breath pacer
void setup() {

 Serial.begin(115200);
 CircuitPlayground.begin();
 interruptSetup(); //reads Pulse Sensor every 2ms
 lastBreath = millis();
 randomSeed(analogRead(0));
}

void loop() {

 serialOutput() ;
 if (QS == true) { //found heart beat
   alive = true;
   lastAlive = millis();
   fadeRate = 255; //fades the LEDs when needed
   serialOutputWhenBeatHappens();
   QS = false;
 }
 unsigned long now = millis();
 if (now - lastAlive > 5000) {
   alive = false; //reset values
   tCoh = 10;
   rCoh = 10;
 }
 ledFadeToBeat();
 delay(20);
}

void updateBreathLED() {

 if (breathLED < FIRST_BREATH_LED) {
   breathLED = FIRST_BREATH_LED;
   breathToggle = false;
 }
 else if (breathLED > LAST_BREATH_LED) {
   breathLED = LAST_BREATH_LED;
   breathToggle = true;
 }
 unsigned long timeNow = millis();
 unsigned long timeSince = timeNow - lastBreath;
 unsigned long breathTime = ledTimes[breathLED];
 if (breathToggle) {
   breathTime = ledTimes[9 - breathLED];
 }
 if (timeSince >= breathTime) {
   prevBreathLED = breathLED;
   if (breathToggle) {
     breathLED -= 1;
   }
   else {
     breathLED += 1;
   }
   lastBreath = timeNow;
 }
 for (int i = 1; i <= numPixels - 1; i++) {
   if ((i == FIRST_BREATH_LED ) || (i == LAST_BREATH_LED)) { //the 2 remaining LEDs used for pulse
     continue;
   }
   randNumber = random(256);
   randNumber2 = random(256);
   randNumber3 = random(256);
   if (i == breathLED) {
     CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.strip.Color(randNumber, randNumber2, randNumber3));
     delay(200);
   }
   else if (i == prevBreathLED) {
     CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.strip.Color(randNumber, randNumber2, randNumber3));
     delay(200);
   }
   else {
     CircuitPlayground.strip.setPixelColor(i, CircuitPlayground.strip.Color(0, 0, 0));
   }
 }
}


void ledFadeToBeat() {

 fadeRate -= 15;                         //LED fade val
 fadeRate = constrain(fadeRate, 0, 255); //constrains fading from going negative
 //color mods
 int r, g, b;
 if (tCoh >= 6) {
   r = 1; g = 0; b = 0;
 }
 else if (tCoh >= 3) {
   r = 0; g = 0; b = 1;
 }
 else {
   r = 0; g = 1; b = 0;
 }
 //2 remaining LEDs for pulse
 CircuitPlayground.strip.setPixelColor(0, CircuitPlayground.strip.Color(fadeRate * r, fadeRate * g, fadeRate * b));
 CircuitPlayground.strip.setPixelColor(9, CircuitPlayground.strip.Color(fadeRate * r, fadeRate * g, fadeRate * b));
 //breath LEDs
 updateBreathLED();
 CircuitPlayground.strip.show();

AWOL

Do you have a question?

Can we expect code tags?

Go Up