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);
} }