Random Stuff We Make!
Published © GPL3+

Blynk - RGB Backlight

This project is about an IoT setup for a TV RGB backlight that can be controlled by Blynk App.

BeginnerFull instructions provided3 hours500

Things used in this project

Hardware components

Adafruit NeoPixel Digital RGB LED Strip 144 LED, 1m White
Adafruit NeoPixel Digital RGB LED Strip 144 LED, 1m White
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
5V 2.5A Switching Power Supply
Digilent 5V 2.5A Switching Power Supply
×1
UTSOURCE Electronic Parts
UTSOURCE Electronic Parts
×1

Software apps and online services

Arduino IDE
Arduino IDE
Blynk
Blynk

Story

Read more

Schematics

Schematics

Schematics

Code

Code

Arduino
Upload it in Arduino
/*
 * V0 To activate the Fx Effects
 * V1 rgb color pallet
 * V3 Select Effect from Drop Down Menu
 * V4 Button to Select custom rgb color to FX Effects
 * V5 Google Assistant Commands
 */


#include<ESP8266WiFi.h>
#include<BlynkSimpleEsp8266.h>

#define FASTLED_INTERRUPT_RETRY_COUNT 0
#define FASTLED_ALLOW_INTERRUPTS 0
#include "FastLED.h"
#include <EEPROM.h>
#include <FastLED_GFX.h>
#include <FastLEDMatrix.h>
#include <string.h>

//#include <Fonts/FreeSans9pt7b.h>

//#define NUM_LEDS 95
#define NUM_LEDS 64
CRGB leds[NUM_LEDS];
#define PIN 4
#define LED_PIN 5

#define COLOR_ORDER    GRB
#define CHIPSET        WS2812B

#define MATRIX_WIDTH   8 // width of FastLED matrix
#define MATRIX_HEIGHT  8 // height of matrix
#define MATRIX_TYPE    (MTX_MATRIX_TOP + MTX_MATRIX_LEFT + MTX_MATRIX_COLUMNS + MTX_MATRIX_PROGRESSIVE) // matrix layout flags, add together as needed

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MATRIX DECLARATION:
// Parameter 1 = width of FastLED matrix
// Parameter 2 = height of matrix
// Parameter 3 = matrix layout flags, add together as needed:
//   MTX_MATRIX_TOP, MTX_MATRIX_BOTTOM, MTX_MATRIX_LEFT, MTX_MATRIX_RIGHT:
//     Position of the FIRST LED in the matrix; pick two, e.g.
//     MTX_MATRIX_TOP + MTX_MATRIX_LEFT for the top-left corner.
//   MTX_MATRIX_ROWS, MTX_MATRIX_COLUMNS: LEDs are arranged in horizontal
//     rows or in vertical columns, respectively; pick one or the other.
//   MTX_MATRIX_PROGRESSIVE, MTX_MATRIX_ZIGZAG: all rows/columns proceed
//     in the same order, or alternate lines reverse direction; pick one.
//   See example below for these values in action.


// Example for NeoPixel Shield.  In this application we'd like to use it
// as a 5x8 tall matrix, with the USB port positioned at the top of the
// Arduino.  When held that way, the first pixel is at the top right, and
// lines are arranged in columns, progressive order.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

cFastLEDSingleMatrix<MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_TYPE> matrix;

BlynkTimer timer;
//Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS,LED_PIN,NEO_GRB+NEO_KHZ800);

char auth[]="dt_xxbAWr10e8dN309kPv9ZW_B7Ae9H1";

char ssid[]="SKYNET";
char pass[]="pioneer#";


int r=0,g=0,b=0,rfx = 0,gfx = 0, bfx = 0;
int pinstate1 = 0;
 int selectedEffect = 0;
int tempFlag = 0;
int fxColorFlag = 0;

int x    = matrix.width();
//int pass = 0;

int textFlag = 0;
String dispText;
int lenString = 0;


CRGB colors[] = {
  CRGB(255, 0, 0),
  CRGB(0, 255, 0),
  CRGB(0, 0, 255)
};

BLYNK_WRITE(V0)
{

  
  pinstate1 = param.asInt();

  if(pinstate1 == 1)
  {
    pinMode(D5, INPUT);
    pinMode(D4, OUTPUT);
    digitalWrite(D1, HIGH);
    customDelay(50);
     setAll(0, 0, 0);
     textFlag = 0;
     Blynk.virtualWrite(V8,0);
  }

  else
  {
    pinMode(D5, INPUT);
    pinMode(D4, OUTPUT);
    digitalWrite(D1, LOW);
    customDelay(50);
    setAll(0, 0, 0);
  }

  /*for(int i=0;i<NUMPIXELS;i++)
  {
    pixels.setPixelColor(i,pixels.Color(r,g,b));
  }
  pixels.show();*/

    
}

BLYNK_WRITE(V1)
{
  r = param[0].asInt();
  g = param[1].asInt();
  b = param[2].asInt();

  if(pinstate1 == 0 && textFlag == 0)
  {
    pinMode(D5, INPUT);
    pinMode(D4, OUTPUT);
    setAll(r, g, b);
  }
    
 
    
}



BLYNK_WRITE(V3)
{
 
  selectedEffect = param.asInt();
  tempFlag = 1;
  setAll(0, 0, 0);
}


BLYNK_WRITE(V4)
{
  fxColorFlag = param.asInt();
}

BLYNK_WRITE(V5)     // For Google Assistant Command
{
  int tempState = 0;
  tempState = param.asInt();
  setAll(0, 0, 0);             // For initial Turn off
  if(tempState == 0)        // To Switch off All the Lights
  {
    Blynk.virtualWrite(V0,0);
    pinstate1 = 0;
    selectedEffect = 0;
    r =0 ;
    g = 0;
    b = 0;
    setAll(0, 0, 0);
  }

  else if(tempState == 1)   // To turn all the Leds to White Color 
  {
    Blynk.virtualWrite(V0,0);
    pinstate1 = 0;
    setAll(200, 200, 200);
  }

  else if(tempState == 2)   // To turn all the Leds to Magenta Color 
  {
    Blynk.virtualWrite(V0,0);
    pinstate1 = 0;
    setAll(255, 0, 255);
  }

  else if(tempState == 3)   // To turn all the Leds to Golden Color 
  {
    Blynk.virtualWrite(V0,0);
    pinstate1 = 0;
    setAll(255, 255, 0);
  }

  else if(tempState == 4)   // To turn all the Leds to Cyan Color (Mood Light)
  {
    Blynk.virtualWrite(V0,0);
    pinstate1 = 0;
    setAll(255, 255, 0);
  }

  else if(tempState == 5)   // To turn all the Leds to Magenta Light Color (Romantic Lights)
  {
    Blynk.virtualWrite(V0,0);
    pinstate1 = 0;
    setAll(52, 0, 52);
  }

  else if(tempState == 6)   // To turn all the Leds to Strobing Effect (Party Mode)
  {
    Blynk.virtualWrite(V0,1);
    pinstate1 = 1;
    selectedEffect = 3;
  }
  
}


BLYNK_WRITE(V7)                       // It takes String Input
{
  dispText = param.asString();
  Serial.println(dispText);
}

BLYNK_WRITE(V8)
{
  textFlag = param.asInt();
  Serial.println(textFlag);
  if(textFlag == 1)
  {
     setAll(0, 0, 0);
    
     pinMode(D4, INPUT);
     pinMode(D5, OUTPUT);
    pinstate1 = 0;
    Blynk.virtualWrite(V0,0);
  }

  else if(textFlag == 0)
  {
    pinMode(D5, INPUT);
    pinMode(D4, OUTPUT);
     setAll(0, 0, 0);
  }
}

/*BLYNK_WRITE(V6)     // For Google Assistant Command
{
  int tempState = 0;
  tempState = param.asInt();

  if(tempState == 0)        // To Switch off All the Lights
  {
    selectedEffect = 0;
    r =0 ;
    g = 0;
    b = 0;
    setAll(0, 0, 0);
  }

  else if(tempState == 1)   // To turn all the Leds to Magenta Color
  {
    pinstate1 = 0;
    setAll(255, 0, 255);
  }
}*/




void customTextColor(byte redT, byte greenT, byte blueT)
{
  colors[0] = CRGB(redT,greenT,blueT);
}


void setup()
{
  //Serial.begin(9600);

  pinMode(D1, OUTPUT);
  pinMode(D2, INPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, INPUT);
  
  Serial.begin(9600);

  
  FastLED.addLeds<WS2812, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(matrix[0], matrix.Size());
  
  FastLED.setBrightness(250);
  FastLED.clear(true);
  matrix.setTextWrap(false);
  matrix.setTextColor(colors[0]);

  delay(10);
  setAll(0, 0, 0);
  /*pixels.begin();
  for(int i=0;i<NUMPIXELS;i++)
  {
    pixels.setPixelColor(i,pixels.Color(130,0,60));
  }
  pixels.show();*/
  Blynk.begin(auth,ssid,pass);
  timer.setInterval(3*1000, reconnectBlynk); //run every 3s
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,0,106), 8080); to switch to local server
 /* for(int i=0;i<NUMPIXELS;i++)
  {
    pixels.setPixelColor(i,pixels.Color(0,0,0));
  }
  pixels.show();*/
  //Blynk.syncAll();

  ESP.wdtDisable();
  ESP.wdtEnable(WDTO_8S);
}


// *************************
// ** LEDEffect Functions **
// *************************

int RGBLoop(){
  for(int j = 0; j < 3; j++ ) { 
    // Fade IN
    for(int k = 0; k < 256; k++) { 
      switch(j) { 
        case 0: setAll(k,0,0); break;
        case 1: setAll(0,k,0); break;
        case 2: setAll(0,0,k); break;
      }
      showStrip();
      if(customDelay(3) == 0)
      {
        return 0;
      }
    }
    // Fade OUT
    for(int k = 255; k >= 0; k--) { 
      switch(j) { 
        case 0: setAll(k,0,0); break;
        case 1: setAll(0,k,0); break;
        case 2: setAll(0,0,k); break;
      }
      showStrip();
      if(customDelay(3) == 0)
      {
        return 0;
      }
    }
  }
}

int FadeInOut(byte red, byte green, byte blue){
  float r, g, b;
      
  for(int k = 0; k < 256; k=k+1) { 
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll((byte)r,(byte)g,(byte)b);
    wdt_reset();
    showStrip();
    
  }
   if(customDelay(1) == 0)
    {
      return 0;
    }
  for(int k = 255; k >= 0; k=k-2) {
    r = (k/256.0)*red;
    g = (k/256.0)*green;
    b = (k/256.0)*blue;
    setAll((byte)r,(byte)g,(byte)b);
    wdt_reset();
    showStrip();
  }
}

int Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause){
  for(int j = 0; j < StrobeCount; j++) {
    setAll(red,green,blue);
    showStrip();
    if(customDelay(FlashDelay) == 0)
    {
      return 0;
    }
    setAll(0,0,0);
    showStrip();
    if(customDelay(FlashDelay) == 0)
    {
      return 0;
    }
  }
 
    if(customDelay(EndPause) == 0)
    {
      return 0;
    }
}

int HalloweenEyes(byte red, byte green, byte blue, 
                   int EyeWidth, int EyeSpace, 
                   boolean Fade, int Steps, int FadeDelay,
                   int EndPause){
  randomSeed(analogRead(0));
  
  int i;
  int StartPoint  = random( 0, NUM_LEDS - (2*EyeWidth) - EyeSpace );
  int Start2ndEye = StartPoint + EyeWidth + EyeSpace;
  
  for(i = 0; i < EyeWidth; i++) {
    setPixel(StartPoint + i, red, green, blue);
    setPixel(Start2ndEye + i, red, green, blue);
  }
  
  showStrip();
  
  if(Fade==true) {
    float r, g, b;
  
    for(int j = Steps; j >= 0; j--) {
      r = j*(red/Steps);
      g = j*(green/Steps);
      b = j*(blue/Steps);
      
      for(i = 0; i < EyeWidth; i++) {
        setPixel(StartPoint + i, r, g, b);
        setPixel(Start2ndEye + i, r, g, b);
      }
      
      showStrip();
      if(customDelay(FadeDelay) == 0)
      {
        return 0;
      }
    }
  }
  
  setAll(0,0,0); // Set all black
  
  if(customDelay(EndPause) == 0)
  {
    return 0;
  }
}

int CylonBounce(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay){

  for(int i = 0; i < NUM_LEDS-EyeSize-2; i++) {
    setAll(0,0,0);
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue); 
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
    showStrip();
    if(customDelay(SpeedDelay) == 0)
    {
      return 0;
    }
  }

  if(customDelay(ReturnDelay) == 0)
  {
    return 0;
  }

  for(int i = NUM_LEDS-EyeSize-2; i > 0; i--) {
    setAll(0,0,0);
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue); 
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
    showStrip();
    wdt_reset();
    if(customDelay(SpeedDelay) == 0)
    {
      return 0;
    }
  }
  
  if(customDelay(ReturnDelay) == 0)
  {
    return 0;
  }
}

int NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay){
  RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
  LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
  OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
  CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
  LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
  RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
  OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
  CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  if(customDelay(1) == 0)
  {
    return 0;
  }
}

//NewKITT(r, g, b, 8, 10, 50);
// used by NewKITT
int CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i =((NUM_LEDS-EyeSize)/2); i>0; i--) {
    setAll(0,0,0);
    
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue); 
      wdt_reset();
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
    
    setPixel(NUM_LEDS-i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(NUM_LEDS-i-j, red, green, blue); 
      wdt_reset();
    }
    setPixel(NUM_LEDS-i-EyeSize-1, red/10, green/10, blue/10);
    wdt_reset();
    showStrip();
    if(customDelay(SpeedDelay) == 0)
    {
      return 0;
    }
  }
  if(customDelay(ReturnDelay) == 0)
  {
    return 0;
  }
}

// used by NewKITT
int OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i = 1; i<=((NUM_LEDS-EyeSize)/2); i++) {
    setAll(0,0,0);
    
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue); 
      wdt_reset();
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
    
    setPixel(NUM_LEDS-i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(NUM_LEDS-i-j, red, green, blue); 
      wdt_reset();
    }
    setPixel(NUM_LEDS-i-EyeSize-1, red/10, green/10, blue/10);
    
    wdt_reset();
    showStrip();
    if(customDelay(SpeedDelay) == 0)
    {
      return 0;
    }
  }
  if(customDelay(ReturnDelay) == 0)
  {
    return 0;
  }
}

// used by NewKITT
int LeftToRight(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i = 0; i < NUM_LEDS-EyeSize-2; i++) {
    setAll(0,0,0);
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue);
      wdt_reset(); 
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
    
    wdt_reset();
    showStrip();
    if(customDelay(SpeedDelay) == 0)
    {
      return 0;
    }
  }
  if(customDelay(ReturnDelay) == 0)
  {
    return 0;
  }
}

// used by NewKITT
int RightToLeft(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i = NUM_LEDS-EyeSize-2; i > 0; i--) {
    setAll(0,0,0);
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue);
      wdt_reset(); 
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);

    wdt_reset();
    showStrip();
    if(customDelay(SpeedDelay) == 0)
    {
      return 0;
    }
  }
  if(customDelay(ReturnDelay) == 0)
  {
    return 0;
  }
}

int Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
  setAll(0,0,0);
  
  for (int i=0; i<Count; i++) {
     setPixel(random(NUM_LEDS),red,green,blue);
     
     wdt_reset();
     showStrip();
     if(customDelay(SpeedDelay) == 0)
      {
        return 0;
      }
     if(OnlyOne) { 
       setAll(0,0,0); 
     }
   }
  
  if(customDelay(SpeedDelay) == 0)
  {
    return 0;
  }
}

int TwinkleRandom(int Count, int SpeedDelay, boolean OnlyOne) {
  setAll(0,0,0);
  
  for (int i=0; i<Count; i++) {
     setPixel(random(NUM_LEDS),random(0,255),random(0,255),random(0,255));
     wdt_reset();
     showStrip();
     if(customDelay(SpeedDelay) == 0)
      {
        return 0;
      }
     if(OnlyOne) { 
       setAll(0,0,0); 
     }
   }
  
  if(customDelay(SpeedDelay) == 0)
  {
    return 0;
  }
}

int Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,red,green,blue);

  wdt_reset();
  showStrip();
  if(customDelay(SpeedDelay) == 0)
  {
    return 0;
  }
  setPixel(Pixel,0,0,0);
}

int SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
  setAll(red,green,blue);
  
  int Pixel = random(NUM_LEDS);
  setPixel(Pixel,0xff,0xff,0xff);

  wdt_reset();
  showStrip();
  if(customDelay(SparkleDelay) == 0)
  {
    return 0;
  }
  setPixel(Pixel,red,green,blue);
  showStrip();
  if(customDelay(SpeedDelay) == 0)
  {
    return 0;
  }
}

int RunningLights(byte red, byte green, byte blue, int WaveDelay) {
  int Position=0;
  
  for(int i=0; i<NUM_LEDS*2; i++)
  {
      Position++; // = 0; //Position + Rate;
      for(int i=0; i<NUM_LEDS; i++) {
        // sine wave, 3 offset waves make a rainbow!
        //float level = sin(i+Position) * 127 + 128;
        //setPixel(i,level,0,0);
        //float level = sin(i+Position) * 127 + 128;
        setPixel(i,((sin(i+Position) * 127 + 128)/255)*red,
                   ((sin(i+Position) * 127 + 128)/255)*green,
                   ((sin(i+Position) * 127 + 128)/255)*blue);
        wdt_reset();
      }

      wdt_reset();
      showStrip();
      if(customDelay(WaveDelay) == 0)
      {
        return 0;
      }
  }
}

int colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue);

      wdt_reset();
      showStrip();
      if(customDelay(SpeedDelay) == 0)
      {
        return 0;
      }
  }
}

int rainbowCycle(int SpeedDelay) {
  byte *c;
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< NUM_LEDS; i++) {
      c=Wheel(((i * 256 / NUM_LEDS) + j) & 255);
      setPixel(i, *c, *(c+1), *(c+2));
    }

    wdt_reset();
    showStrip();
    if(customDelay(SpeedDelay) == 0)
    {
      return 0;
    }
  }
}

// used by rainbowCycle and theaterChaseRainbow
byte * Wheel(byte WheelPos) {
  static byte c[3];
  
  if(WheelPos < 85) {
   c[0]=WheelPos * 3;
   c[1]=255 - WheelPos * 3;
   c[2]=0;
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   c[0]=255 - WheelPos * 3;
   c[1]=0;
   c[2]=WheelPos * 3;
  } else {
   WheelPos -= 170;
   c[0]=0;
   c[1]=WheelPos * 3;
   c[2]=255 - WheelPos * 3;
  }

  return c;
}

int theaterChase(byte red, byte green, byte blue, int SpeedDelay) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    wdt_reset();
    for (int q=0; q < 3; q++) {
      wdt_reset();
      for (int i=0; i+q < NUM_LEDS; i=i+3) {
        setPixel(i+q, red, green, blue);    //turn every third pixel on
        wdt_reset();
      }

      wdt_reset();
      showStrip();
     
      if(customDelay(SpeedDelay) == 0)
      {
        return 0;
      }
     
      for (int i=0; i+q < NUM_LEDS; i=i+3) {
        setPixel(i+q, 0,0,0);        //turn every third pixel off
        wdt_reset();
      }
    }
  }
}

int theaterChaseRainbow(int SpeedDelay) {
  byte *c;
  
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    wdt_reset();
    for (int q=0; q < 3; q++) {
      wdt_reset();
        for (int i=0; i+q < NUM_LEDS; i=i+3) {
          c = Wheel( (i+j) % 255);
          setPixel(i+q, *c, *(c+1), *(c+2));    //turn every third pixel on
          wdt_reset();
        }

        wdt_reset();
        showStrip();
       
        if(customDelay(SpeedDelay) == 0)
        {
          return 0;
        }
       
        for (int i=0; i+q < NUM_LEDS; i=i+3) {
          setPixel(i+q, 0,0,0);        //turn every third pixel off
          wdt_reset();
        }
    }
  }
}

int Fire(int Cooling, int Sparking, int SpeedDelay) {
  static byte heat[NUM_LEDS];
  int cooldown;
  
  // Step 1.  Cool down every cell a little
  for( int i = 0; i < NUM_LEDS; i++) {
    cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
    
    if(cooldown>heat[i]) {
      heat[i]=0;
    } else {
      heat[i]=heat[i]-cooldown;
    }
  }
  
  // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  for( int k= NUM_LEDS - 1; k >= 2; k--) {
    heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
    wdt_reset();
  }
    
  // Step 3.  Randomly ignite new 'sparks' near the bottom
  if( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160,255);
    //heat[y] = random(160,255);
    wdt_reset();
  }

  // Step 4.  Convert heat to LED colors
  for( int j = 0; j < NUM_LEDS; j++) {
    setPixelHeatColor(j, heat[j] );
  }

  wdt_reset();
  showStrip();
  if(customDelay(SpeedDelay) == 0)
  {
    return 0;
  }
}

int setPixelHeatColor (int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature/255.0)*191);
 
  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252
 
  // figure out which third of the spectrum we're in:
  if( t192 > 0x80) {                     // hottest
    setPixel(Pixel, 255, 255, heatramp);
  } else if( t192 > 0x40 ) {             // middle
    setPixel(Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixel(Pixel, heatramp, 0, 0);
  }
}

int BouncingColoredBalls(int BallCount, byte colors[][3], boolean continuous) {
  float Gravity = -9.81;
  int StartHeight = 1;
  
  float Height[BallCount];
  float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
  float ImpactVelocity[BallCount];
  float TimeSinceLastBounce[BallCount];
  int   Position[BallCount];
  long  ClockTimeSinceLastBounce[BallCount];
  float Dampening[BallCount];
  boolean ballBouncing[BallCount];
  boolean ballsStillBouncing = true;
  
  for (int i = 0 ; i < BallCount ; i++) {   
    ClockTimeSinceLastBounce[i] = millis();
    Height[i] = StartHeight;
    Position[i] = 0; 
    ImpactVelocity[i] = ImpactVelocityStart;
    TimeSinceLastBounce[i] = 0;
    Dampening[i] = 0.90 - float(i)/pow(BallCount,2);
    ballBouncing[i]=true;
    wdt_reset(); 
  }

  while (ballsStillBouncing) {
    for (int i = 0 ; i < BallCount ; i++) {
      TimeSinceLastBounce[i] =  millis() - ClockTimeSinceLastBounce[i];
      Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i]/1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i]/1000;
  
      if ( Height[i] < 0 ) {                      
        Height[i] = 0;
        ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
        ClockTimeSinceLastBounce[i] = millis();
  
        if ( ImpactVelocity[i] < 0.01 ) {
          if (continuous) {
            ImpactVelocity[i] = ImpactVelocityStart;
          } else {
            ballBouncing[i]=false;
          }
        }
      }
      Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
    }

    ballsStillBouncing = false; // assume no balls bouncing
    for (int i = 0 ; i < BallCount ; i++) {
      setPixel(Position[i],colors[i][0],colors[i][1],colors[i][2]);
      wdt_reset();
      if ( ballBouncing[i] ) {
        ballsStillBouncing = true;
      }
    }

    wdt_reset();
    if(customDelay(1) == 0)
    {
      return 0;
    }
    showStrip();
    setAll(0,0,0);
  }
}

int meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
  
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
    
    
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
    
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        setPixel(i-j, red, green, blue);
...

This file has been truncated, please download it to see its full contents.

Credits

Random Stuff We Make!

Random Stuff We Make!

9 projects • 19 followers
We at RSWM! try to bring Fiction to Reality through projects which are Interactive, Creative & way too simpler in terms of making.

Comments

Add projectSign up / Login