How to get LED strip code to change colours at each state

Hey everyone. I have a code running off different states to give the LEDs a fading effect. I would love each state to be a different colour, but it goes a bit weird every time I try and edit the code. I'm very new to coding and Arduino so I'm not sure if I'm missing something obvious. Would love some guidance, cheers!

The working code is just fading in and out in one consistent colour. It's a sort of pinky, purple. I'm not too concerned with what the exact colour is.

The code is currently running on 3-second intervals (fade in for 3 seconds, hold at full brightness for 3, fade out for 3, hold at almost 0 brightness for 3). I'm looking for help with changing the colours at each point.

So for example:

Fade in PINK for 3 seconds
Hold at full brightness in ORANGE for 3 seconds
Fade out in BLUE for 3 seconds
Hold at almost 0 brightness in GREEN for 3 seconds (though I understand this is a little pointless)

Rather than all of the intervals happening in the same hue (which is currently that pinky, purple I mentioned earlier).

 #include "FastLED.h"            // This sketch uses the FastLED library
 
#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 200                 // The colour of the LED's
#define LOOP_DELAY 20            // in Milliseconds
#define BREATH_HOLD_DELAY 3000

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

//Global Variables
CRGB leds[NUM_LEDS];            // the array of LEDs          assigning one after the other

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

void setup() {
  Serial.begin(115200);
  LEDS.addLeds<LED_TYPE, DATA, CLOCK, RGB>(leds, NUM_LEDS); 
  FastLED.clear();
  FastLED.show();
}


/*====================================================================
  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... :(");
  }
  
  //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();        



  delay(LOOP_DELAY);


  
   
} 

Here's the code I was mucking around with but didn't work! Just made the LEDs stuck on green and completely stop running the fading code. It was just kinda pulsing near full brightness while being green.

#include "FastLED.h"            // This sketch uses the FastLED library
#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 LOOP_DELAY 20            // in Milliseconds
#define BREATH_HOLD_DELAY 3000
#define SERVO_INITAL_POSITION 0               // I 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;
          for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(0, 255, 0);
        }
        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 + 15);



  delay(LOOP_DELAY);


  
   
} } 

Not very descriptive.

oops, sorry.

It either doesn't work at all (LEDs don't turn on) or the fade function won't work (it starts strobing at a rapid rate). The code I posted should be 100% functional. Just want to know what to change in the functioning code to get the colours to change at each state!

Hi forward,

welcome to the arduino-forum. Well done to post the code as a code-section.
There are a few things to learn about how to use the forum to get effective help

You should post the working code and your modified code and a description where you have modified it and a precise description what you observe on the LED-strip

here are more tips

best regards Stefan

there could be much more precision in your description
if "state" means breathing states
In, ChangingToOut, Out, ChangingToIn, Rest

to what color do you want to change?
the posted code that works changes the color slowly
What different behaviour than "changing color slowly" do you want to have ?

to make it easier to analyse you should post a description of what the working code is doing with the colors.

I'm not familiar with HUE-color-values
What can you see if you watch the LEDs ?
What brightness and what colors if the working code is running?

you can go on for hundreds of postings playing a ping-pong game always giving just a little info and loosing the will of more and more users as it is so diffciult to help you.

Or you could go reading

get the best out of this forum take it to your heart and write down all the details

best regards Stefan

Thank you for the advice! I updated my post so hopefully it makes more sense now

There are some lines in the code that mention "color

//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
  }

So what do I have to change to set all LEDs to a different color?

Additionally google is always worth a quick search
https://www.google.de/search?as_q=arduino+fastled+hue+colors

finds links to introductional tutorials how to use colors with fastLED

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.