Skip to content

Instantly share code, notes, and snippets.

@ArduinoBasics
Created March 17, 2017 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArduinoBasics/f4074cda4a029f01f261ed70071d5e33 to your computer and use it in GitHub Desktop.
Save ArduinoBasics/f4074cda4a029f01f261ed70071d5e33 to your computer and use it in GitHub Desktop.
Comet effect for Jason Brown
//Comet Effect Written for Jason Brown
//Author: Scott C
//Date: 17th March 2017
//Arduino IDE version 1.8.1
//======================================================================
#include "FastLED.h" //Make sure to install the FastLED library into your Arduino IDE
//The total number of LEDs being used is 144
#define NUM_LEDS 144
// The data pin for the NeoPixel strip is connected to digital Pin 6 on the Arduino
#define DATA_PIN 6
// The button will be connected to digital pin 9 on the Arduino. Uses Internal pullup resistor.
#define BUTTON 9
//Initialise the LED array, the LED Hue (ledh) array, and the LED Brightness (ledb) array.
CRGB leds[NUM_LEDS];
byte ledh[NUM_LEDS];
byte ledb[NUM_LEDS];
const int LEDSpeed = 20; //Speed of the LED animation effect. Make sure not to exceed the maxLEDSpeed value.
int maxLEDSpeed = 50; //Identifies the maximum speed of the LED animation sequence
int LEDposition=0; //Identifies the LED position in the strip that the comet is currently at. The maximum position is NUM_LEDS-1 (eg. 143)
int oldPosition=0; //Holds the previous position of the comet.
byte hue = 0; //Stores the Leading LED's hue value (colour)
byte sat = 255; //Stores the Leading LED's saturation value
byte tailHue = 0; //Stores the Comet tail hue value
int tailLength = 8; //determines the length of the tail.
byte hueRange = 20; //Colour variation of the tail (greater values have greater variation
byte intensity = 200; //The default brightness of the leading LED
byte tailbrightness= intensity / 2; //Affects the brightness and length of the tail
int animationDelay = 0; //The greater the animation delay, the slower the LED sequence. Calculated from LEDSpeed and MaxSpeed.
unsigned long waitTime = 60000; //The wait time for each comet is currently set for 1 minute (or 60000 milliseconds).
unsigned long endTime; //The time when we no longer have to wait for the next comet
int cometNumber = 3; //Used to choose which comet colour to show (***Don't change this variable***)
//===================================================================================================================================================
// setup() : Is used to initialise the LED strip
//===================================================================================================================================================
void setup() {
delay(2000); //Delay for two seconds to power the LEDS before starting the data signal on the Arduino
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); //initialise the LED strip
pinMode(BUTTON, INPUT_PULLUP); //Connect button to digital pin 9, and use the internal pullup resistor
selectNextComet(); //Select the next comet colour
}
//===================================================================================================================================================
// loop() :
//===================================================================================================================================================
void loop(){
showLED(LEDposition, hue, sat, intensity);
//Adjust the hue of the tail so that it is a different colour from the leading LED. To reduce variation in tail colour, reduce the hueRange.
if(hue>(254-hueRange)){
tailHue = random((hue-hueRange),hue);
} else {
tailHue = random(hue, (hue+hueRange));
}
tailbrightness = random(50, 100); //Randomly select the brightness of the trailing LED (provides sparkling tail)
leds[LEDposition]=CHSV((tailHue),sat,tailbrightness); //Set the colour, saturation and brightness of the trailing LED
fadeLEDs(tailLength); //Fade the tail so that the tail brightness dwindles down to nothingness.
setDelay(LEDSpeed); //
LEDposition++;
if(LEDposition>(NUM_LEDS-1)){
for(int i=0; i<50; i++){
showLED(LEDposition, hue, sat, intensity);
fadeLEDs(tailLength);
setDelay(LEDSpeed);
}
LEDposition=0;
selectNextComet(); //Select the next comet colour
waitForNextComet(); //wait for the next comet (either 60 seconds or button press)
}
}
//===================================================================================================================================================
// showLED() : is used to illuminate the LEDs
//===================================================================================================================================================
void showLED(int pos, byte LEDhue, byte LEDsat, byte LEDbright){
leds[pos] = CHSV(LEDhue,LEDsat,LEDbright);
FastLED.show();
}
//===================================================================================================================================================
// fadeLEDs(): This function is used to fade the LEDs back to black (OFF)
//===================================================================================================================================================
void fadeLEDs(int fadeVal){
for (int i = 0; i<NUM_LEDS; i++){
leds[i].fadeToBlackBy( fadeVal );
}
}
//===================================================================================================================================================
// setDelay() : is where the speed of the LED animation sequence is controlled. The speed of the animation is controlled by the LEDSpeed variable.
// and cannot go faster than the maxLEDSpeed variable.
//===================================================================================================================================================
void setDelay(int LSpeed){
animationDelay = maxLEDSpeed - abs(LSpeed);
delay(animationDelay);
}
//===================================================================================================================================================
//selectNextComet() : This is where we select either the Blue, the Pink or the White comet
//===================================================================================================================================================
void selectNextComet(){
cometNumber++;
if(cometNumber>3){
cometNumber=1;
}
switch(cometNumber){
case 1: { //Blue Comet
hue = 160;
sat = 255;
hueRange=20;
break;
}
case 2: { //Pink Comet
hue = 224;
sat = 120;
hueRange=10;
break;
}
default: { //White Comet
hue = 0;
sat = 0;
hueRange = 0;
break;
}
}
}
//===================================================================================================================================================
// waitForNextComet() : Is where we either wait for 60 seconds for another comet to come, or initiate another comet with a button press.
// The button will only be "ACTIVE" while we are waiting for the next comet. It has no effect while a comet is currently running
//===================================================================================================================================================
void waitForNextComet(){
endTime = millis() + waitTime;
while(millis()<endTime){
if(digitalRead(BUTTON)==LOW){
break;
}
}
}
@bygerardvisuals
Copy link

bygerardvisuals commented Mar 26, 2023

Hello Jason!

I really love your effect but I have some doubts about how to choose custom color. Which kind of chart do you use to select a custom color?

Thanks a lot in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment