1

Basically make a code to guide users through a box breathing exercise (breath in for 4 seconds, hold for 4, release for 4, hold again for 4, repeat) to help relax them.

Right now my code is running APA102's. They're running through 5 "breathing" states; In, ChangingToOut, Out, ChangingToIn, Rest.

My LEDs are just fading in and out in an aqua/green colour.

What I would like to do is to change the colour at every BREATH_HOLD_DELAY. I want the LED display to be easier to follow for people doing the breathing exercise. Right now It's difficult to tell when the "hold breath" part is when the LEDs are at full brightness. I would like the LEDs to fade in green, immediately switch to blue for the 4-second BREATH_HOLD_DELAY, and then immediately switch back to green to fade out and hold at low brightness for 4.

I'm really lost as to how to do this. I've tried doing an "if" statement;

if (BREATH_HOLD_DELAY)

leds[i] = CHSV(HUE2, 255, brightness);

But that just changed the entire LEDs light fade to blue, rather than just the BREATH_HOLD_DELAY parts.

Here's my entire code!

#include <Servo.h>    
 
#define DATA 11                 // Data connected to Digital Pin 11 on the Arduino                                    
#define CLOCK 13                // Clock connected to Digital Pin 13 on the Arduino
#define SERVO_PIN 9              // Pin that the servo is connected to                        
#define LED_TYPE APA102         // This LED strip uses the APA102 chipset.                                  
#define NUM_LEDS 45             // There are 25 LEDs on this strand
#define HUE 65                  // The colour of the LED's
#define HUE2 10                  // The colour of the LED's
#define LOOP_DELAY 20          // in Milliseconds
#define BREATH_HOLD_DELAY 3000
#define SERVO_INITAL_POSITION 0  // define the initial position value to 0

enum breathingStates { In, ChangingToOut, Out, ChangingToIn, Rest };

//Global Variables
CRGB leds[NUM_LEDS];            // the array of LEDs          assigning one after the other
Servo myservo;  // create servo object to control a servo

 
/*====================================================================
  setup(): will run only once
=====================================================================*/

void setup() {
  Serial.begin(115200);
  LEDS.addLeds<LED_TYPE, DATA, CLOCK, RGB>(leds, NUM_LEDS);
  FastLED.clear();
  FastLED.show();
  myservo.attach(SERVO_PIN);
  myservo.write(SERVO_INITAL_POSITION); // Initial position of the servo
}


/*====================================================================
  loop(): will run indefinitely
=====================================================================*/

void loop() {
  static int8_t theta = 0;
  static breathingStates breathingState = Rest;

  switch(breathingState)
  {
      case Rest:
        Serial.println("This is the first time the loop has run");
        // Do any further setup not done in setup function here, but this is redundant
        breathingState = In;
        break;
       
      case In:
        theta++;
        if(theta >= 90){
          breathingState = ChangingToOut;
        }
        break;

      case ChangingToOut:
        delay(BREATH_HOLD_DELAY);
        breathingState = Out;
        theta--;
        break;
     
      case Out :
        theta--;
        if(theta <= -90){
          breathingState = ChangingToIn;
        }
        break;

      case ChangingToIn:
        delay(BREATH_HOLD_DELAY);
        breathingState = In;
        theta++;
        break;

       
      default:
        Serial.println("It should never have come to this... :(");
  }

  // Now that theta is set, we do somethign with it in this section of the loop() function
 
  //Serial.print("The value of theta is: "); Serial.println(theta);

  float thetaInRadians = float(theta) / 57.295;
  double dblBrightness = sin(thetaInRadians);      // LED brightness
  uint8_t brightness = (dblBrightness * 127) + 128;
  Serial.print("The value of brightness is: "); Serial.println(brightness);

  //Set the  hue and brightness of each LED
  for (int i=0; i < NUM_LEDS; i++){
    leds[i] = CHSV(HUE + i, 255, brightness);  // Set the colour and brightness of the LEDs
    
  }
 
  //Show the next frame of the LED animation
  FastLED.show();        


  myservo.write(theta + 180);


  delay(LOOP_DELAY);
   
} ```


Would love any help or guidance. I'm very new to coding and Arduino!
5
  • Is it intentional that the color of the LEDs is slightly different? Or should they all have the same color at a certain point in time?
    – PMF
    May 18 at 14:15
  • All the same colour ideally when the LEDs are holding for 4 seconds at full brightness! I think that's the BREATH_HOLD_DELAY section May 18 at 14:28
  • Your color scheme seems weird, float thetaInRadians = float(theta) / 57.295; uses an angle (from a HSV color scheme?) and then converts that into a brighness. The brighness however is the second parameter in a HSV, which you apparently want to control separately. I would change the switch statement such that in every step, the desired color/brighness is selected and then, in a separate step, you fade to that. That way, you can control the desired target color/brighness independently of how fast the colors change.
    – PMF
    May 18 at 14:34
  • is it possible to put something like if (brightness = >255) and change the colour that way? Tell the code that when it hits full brightness it needs to be blue instead of green? May 18 at 14:56
  • Sure, it's all software. You can do whatever you like.
    – PMF
    May 18 at 15:18

0

Your Answer

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

Browse other questions tagged or ask your own question.