• Recent Posts

    Paul Schmidt

    NWSL remaining in business!

    Great news! Northwest Short Line, purveyor to the world of fine gears, motors and other cool stuff, will carry on.

    Owner Dave Rygmyr announced

    Paul Schmidt Yesterday, 08:32 PM Go to last post
    Bryan

    How Old is too Old for N Scale?

    At work, we’ve changed out the twin 4’ fluorescent tube fixtures for 4k LED fixtures.
    They are much easier on the eyes... things are

    Bryan Yesterday, 08:06 PM Go to last post
    NtheBasement

    Tomix Camera Train

    I put together a VHF camera car years (decades?) ago on a flat car, battery powered and crude video res. I looked for 4K cams and most are indeed shaped

    NtheBasement Yesterday, 07:44 PM Go to last post
    Paul Schmidt

    How Old is too Old for N Scale?

    Good advice. It certainly should. That's what's above the Slate Fork, and either a 4000k or 5000k above the workbench. I should double-check.

    Paul Schmidt Yesterday, 05:40 PM Go to last post
    nightfly

    How Old is too Old for N Scale?

    As with UP4-8-8-4, I found everything much easier to see after increasing the lighting in my train room. Then I did the same everywhere around the house;

    nightfly Yesterday, 04:23 PM Go to last post
  • Using LED Strips for Day/Night Cycle

    I just got a basic version my LED day/night cycle up and running, so I thought I'd share with you guys.
    It still needs a lot of tweaking, and after I get the colors right, I'll slow it down as well.
    I also need to figure out how to program it to jump it to a certain point, as well as be able to pause it.
    The Arduino code in it's majority is not mine. I have made several changes to it, but most of it was a copy/paste from another Arduino code. I'll provide the link if I can find it again.

    So without further ado, here it is!

    http://youtu.be/UysoNEStVc8

    (Don't be scared by the Unitrack, I'm still handlaying, this is just my quick and easy testbed layout for trying out numerous things and running trains while life has the modules on hold. )

    Here's a visual of a 2 channel circuit that could be expanded upon.
    Attachment 58451


    Components Required:

    LED Strips - at least one RGB, and I also used warm white and cool white strips.
    12V Power supply - make sure it has enough amperage to power all the LED strips. I used a 20 amp for 6 strips.
    Arduino UNO - I used an UNO, as it supports up to 6 PWM channels. The Mega has 12, if more are needed.
    MOSFETs - I used these: Link - I'm also using these heatsinks with them: Link
    Breadboard - Since I didn't want to have a custom board made, I used this protoboard. Link



    Assembly:


    I used the diagrams at this site to assist in assembling the components. http://bildr.org/2012/03/rfp30n06le-arduino/

    (All references here to the MOSFET's are as viewed from the side with the lettering)
    Make sure everything is powered off before beginning.

    1. Connect all the MOSFETS to the board.

    2. Connect a 10K resistor to the left pin of a MOSFET. Connect the other side of the resistor to the right pin of the same MOSFET. Repeat for all the MOSFETs.

    3. Connect the Arduino pin 9 to the left pin of the MOSFET that will control the red LEDs.
    Repeat for the Arduino pins 10 and 11 to the left pins of the Green and Blue MOSFETs respectively.
    If desired, connect Arduino pins 3 and 5 to the left pins of the Cool White and Warm White MOSFETs respectively.
    Guide:
    Cool White - pin 3
    Warm White - pin 5
    Red - pin 9
    Green - pin 10
    Blue - pin 11

    4. Connect the middle pins of the MOSFETs to the "-" of their respective LED strips.

    5. Connect the Arduino GND to the power supply ground, and to the right pin of all the MOSFETs.

    6. Connect the power supply "+" to the "+" of all the LED strips. This is USUALLY(always check!) the red wire on single color LED strips, and the black wire on RGB strips.

    7. Plug the Arduino's USB cable into your computer USB port, and using the Arduino software, upload the following code to the Arduino. After it uploads, you can either leave the Arduino connected to the computer, or disconnect it and use a different USB power supply to power it.

    Code:
    /*
    * Code for cross-fading 3 LEDs, red, green and blue (RGB) 
    * To create fades, you need to do two things: 
    *  1. Describe the colors you want to be displayed
    *  2. List the order you want them to fade in
    *
    * DESCRIBING A COLOR:
    * A color is just an array of three percentages, 0-100, 
    *  controlling the red, green and blue LEDs
    *
    * Red is the red LED at full, blue and green off
    *   int red = { 100, 0, 0 }
    * Dim white is all three LEDs at 30%
    *   int dimWhite = {30, 30, 30}
    * etc.
    *
    * Some common colors are provided below, or make your own
    * 
    * LISTING THE ORDER:
    * In the main part of the program, you need to list the order 
    *  you want to colors to appear in, e.g.
    *  crossFade(red);
    *  crossFade(green);
    *  crossFade(blue);
    *
    * Those colors will appear in that order, fading out of 
    *    one color and into the next  
    *
    * In addition, there are 5 optional settings you can adjust:
    * 1. The initial color is set to black (so the first color fades in), but 
    *    you can set the initial color to be any other color
    * 2. The internal loop runs for 1020 interations; the 'wait' variable
    *    sets the approximate duration of a single crossfade. In theory, 
    *    a 'wait' of 10 ms should make a crossFade of ~10 seconds. In 
    *    practice, the other functions the code is performing slow this 
    *    down to ~11 seconds on my board. YMMV.
    * 3. If 'repeat' is set to 0, the program will loop indefinitely.
    *    if it is set to a number, it will loop that number of times,
    *    then stop on the last color in the sequence. (Set 'return' to 1, 
    *    and make the last color black if you want it to fade out at the end.)
    * 4. There is an optional 'hold' variable, which pasues the 
    *    program for 'hold' milliseconds when a color is complete, 
    *    but before the next color starts.
    * 5. Set the DEBUG flag to 1 if you want debugging output to be
    *    sent to the serial monitor.
    *
    *    The internals of the program aren't complicated, but they
    *    are a little fussy -- the inner workings are explained 
    *    below the main loop.
    */ 
    
    
    //Input
    int sensorPin = A0;    // select the input pin for the potentiometer for cycle speed
    int sensorValue = 20;  // variable to store the value coming from the sensor
    int knob = 20;
    
    
    // Output
    int cwhPin = 3;   // Cool White LED, connected to digital pin 3
    int wwhPin = 5;   // Warm White LED, connected to digital pin 5
    //int auxPin = 6;   // For other LED (if any) connected to digital pin 6
    int redPin = 9;   // Red LED,   connected to digital pin 9
    int grnPin = 10;  // Green LED, connected to digital pin 10
    int bluPin = 11;  // Blue LED,  connected to digital pin 11
    
    
    // Color arrays
    int white[5]  =     { 100, 100, 100, 100, 100 };
    int red[5]    =     { 100, 0, 0, 0, 0 };
    int green[5]  =     { 0, 100, 0, 0, 0 };
    int blue[5]   =     { 0, 0, 100, 0, 0 };
    int yellow[5] =     { 60, 50, 0, 0, 0 };
    int dimWhite[5] =   { 30, 30, 30, 30, 30 };
    int black[5]  =     { 0, 0, 0, 0, 0 };
    
    
    int dawnone[5] =    { 98, 90, 70, 0, 0};
    int dawntwo[5]  =   { 95, 20, 10, 0, 0 };
    int dawnthree[5] =  { 100, 57, 30, 70, 50 };
    int carbonarc[5]  = { 100, 97, 80 , 70, 100};
    int sun[5] =        { 100, 100, 80, 100, 100 };
    int noon[5] =       { 100, 100, 90, 100, 100 };
    int duskone[5] =    { 97, 85, 56, 0, 0 };
    int dusktwo[5] =    { 20, 40, 70, 0, 0 };
    int blueone[5] =    { 0, 10, 30, 0, 0 };
    int bluetwo[5] =    { 0, 0, 20, 0, 0 };
    // etc.
    
    
    // Set initial color
    int redVal = black[0];
    int grnVal = black[1]; 
    int bluVal = black[2];
    int cwhVal = black[3];
    int wwhVal = black[4];
    
    
    //int wait = sensorValue;      // 10ms internal crossFade delay; increase for slower fades
    int hold = 0;       // Optional hold when a color is complete, before the next crossFade
    int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
    int loopCount = 60; // How often should DEBUG report?
    int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
    int j = 0;          // Loop counter for repeat
    
    
    // Initialize color variables
    int prevR = redVal;
    int prevG = grnVal;
    int prevB = bluVal;
    int prevC = cwhVal;
    int prevW = wwhVal;
    
    
    // Set up the LED outputs
    void setup()
    {
      pinMode(redPin, OUTPUT);   // sets the pins as output
      pinMode(grnPin, OUTPUT);   
      pinMode(bluPin, OUTPUT); 
      pinMode(cwhPin, OUTPUT);
      pinMode(wwhPin, OUTPUT);
      
      if (DEBUG) {           // If we want to see values for debugging...
        Serial.begin(9600);  // ...set up the serial ouput 
      }
    }
    
    
    // Main program: list the order of crossfades
    void loop()
    
    
      // read the value from the sensor:
      {
      crossFade(dawntwo);
      crossFade(dawnone);
      crossFade(dawnthree);
      crossFade(carbonarc);
      crossFade(sun);
      crossFade(noon);
      crossFade(sun);
      crossFade(duskone);
      crossFade(black);
      crossFade(dusktwo);
      crossFade(blueone);
      crossFade(bluetwo);
      crossFade(black);
      crossFade(black);
      
    
    
      if (repeat) { // Do we loop a finite number of times?
        j += 1;
        if (j >= repeat) { // Are we there yet?
          exit(j);         // If so, stop.
        }
      }
      
      sensorValue = analogRead(sensorPin);
      int wait = sensorValue;
    }
    
    
    /* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
    * 
    * The program works like this:
    * Imagine a crossfade that moves the red LED from 0-10, 
    *   the green from 0-5, and the blue from 10 to 7, in
    *   ten steps.
    *   We'd want to count the 10 steps and increase or 
    *   decrease color values in evenly stepped increments.
    *   Imagine a + indicates raising a value by 1, and a -
    *   equals lowering it. Our 10 step fade would look like:
    * 
    *   1 2 3 4 5 6 7 8 9 10
    * R + + + + + + + + + +
    * G   +   +   +   +   +
    * B     -     -     -
    * 
    * The red rises from 0 to 10 in ten steps, the green from 
    * 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
    * 
    * In the real program, the color percentages are converted to 
    * 0-255 values, and there are 1020 steps (255*4).
    * 
    * To figure out how big a step there should be between one up- or
    * down-tick of one of the LED values, we call calculateStep(), 
    * which calculates the absolute gap between the start and end values, 
    * and then divides that gap by 1020 to determine the size of the step  
    * between adjustments in the value.
    */
    
    
    int calculateStep(int prevValue, int endValue) {
      int step = endValue - prevValue; // What's the overall gap?
      if (step) {                      // If its non-zero, 
        step = 1020/step;              //   divide by 1020
      } 
      return step;
    }
    
    
    /* The next function is calculateVal. When the loop value, i,
    *  reaches the step size appropriate for one of the
    *  colors, it increases or decreases the value of that color by 1. 
    *  (R, G, and B are each calculated separately.)
    */
    
    
    int calculateVal(int step, int val, int i) {
    
    
      if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
        if (step > 0) {              //   increment the value if step is positive...
          val += 1;           
        } 
        else if (step < 0) {         //   ...or decrement it if step is negative
          val -= 1;
        } 
      }
      // Defensive driving: make sure val stays in the range 0-255
      if (val > 255) {
        val = 255;
      } 
      else if (val < 0) {
        val = 0;
      }
      return val;
    }
    
    
    /* crossFade() converts the percentage colors to a 
    *  0-255 range, then loops 1020 times, checking to see if  
    *  the value needs to be updated each time, then writing
    *  the color values to the correct pins.
    */
    
    
    void crossFade(int color[5]) {
      // Convert to 0-255
      int R = (color[0] * 255) / 100;
      int G = (color[1] * 255) / 100;
      int B = (color[2] * 255) / 100;
      int C = (color[3] * 255) / 100;
      int W = (color[4] * 255) / 100;
    
    
      int stepR = calculateStep(prevR, R);
      int stepG = calculateStep(prevG, G); 
      int stepB = calculateStep(prevB, B);
      int stepC = calculateStep(prevC, C);
      int stepW = calculateStep(prevW, W);
    
    
      for (int i = 0; i <= 1020; i++) {
        redVal = calculateVal(stepR, redVal, i);
        grnVal = calculateVal(stepG, grnVal, i);
        bluVal = calculateVal(stepB, bluVal, i);
        cwhVal = calculateVal(stepC, cwhVal, i);
        wwhVal = calculateVal(stepW, wwhVal, i);
      
        analogWrite(redPin, redVal);   // Write current values to LED pins (added the "255 - " to each of these three to accomodate common anode LED. ex:  analogWrite(wwhPin, 255 - wwhVal);   ) Andrew Kyle
        analogWrite(grnPin, grnVal);      
        analogWrite(bluPin, bluVal);
        analogWrite(cwhPin, cwhVal);
        analogWrite(wwhPin, wwhVal); 
    
    
        delay(analogRead(sensorPin)); // Pause for 'wait' milliseconds before resuming the loop
        knob = analogRead(sensorPin);
    
    
        if (DEBUG) { // If we want serial output, print it at the 
          if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
            Serial.print("Loop/RGBCW: #");
            Serial.print(i);
            Serial.print(" | ");
            Serial.print(redVal);
            Serial.print(" / ");
            Serial.print(grnVal);
            Serial.print(" / ");
            Serial.print(bluVal);
            Serial.print(" / ");
            Serial.print(cwhVal);
            Serial.print(" / ");
            Serial.println(wwhVal);
            Serial.print(knob);
            Serial.print(" , ");
            Serial.println(sensorPin);
            
          } 
          DEBUG += 1;
        }
      }
      // Update current values for next loop
      prevR = redVal; 
      prevG = grnVal; 
      prevB = bluVal;
      prevC = cwhVal;
      prevW = wwhVal;
      delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
    }
    8. Plug in the main power supply (for the LED strips) and watch the show!

    The color settings in the code are quite basic, as I plan on changing them to more detail soon.
    At that point, I will also slow it down, and re-upload the code here as well.

    (Whew, I think that's everything!)

    Hope this can be of use to someone!
    Also, a big thank you to the members here in the chatbox that have assisted me in getting this project off the ground!
    I couldn't have done it without them!

    Andrew
    Comments 9 Comments
    1. Sugar's Avatar
      Sugar -
      Fantastic Andrew,
      Have you had any advances since posting this?
    1. conrailandrew's Avatar
      conrailandrew -
      Yep, I've put a knob on it that lets me control the rate of the cycle. I have yet to do the tweaking of the exact colors, but that will come soon, then I'll post an updated code.

      Thanks,
      Andrew
    1. Claymore1977's Avatar
      Claymore1977 -
      Looks fantastic sir. Excellent work!

      Have you measured the total current draw for the system?
    1. Moose2013's Avatar
      Moose2013 -
      Moose like the design and effects. Thank you for the article.

      Moose just getting interested in possibilities of electronic / computerized controls over layout, a step beyond DCC locomotives themselves, to the building interior & exterior lighting, street lamps, etc. What you have shown would add a new dimension I hadn't thought about controlling automatically.

      Wow! Moose now must travel to Moose Jaw, Saskatchewan, and drop my jaw at your creativity ... Okay, that was funnier in my big moose head...

      Again, thanks for article!
    1. conrailandrew's Avatar
      conrailandrew -
      Quote Originally Posted by Claymore1977 View Post
      Looks fantastic sir. Excellent work!

      Have you measured the total current draw for the system?
      I did measure it when I first did this, but I don't remember what it was now. It was way less than what I expected.

      Andrew
    1. conrailandrew's Avatar
      conrailandrew -
      Quick update:

      I updated the code tonight to add a randomized lightning storm. Right now, the day/night cycle pauses until the thunderstorm completes, then continues. I plan to fix this in the near future, and will keep this thread updated. There is a 1:50,000 chance of a thunderstorm happening at a point in the loop of the code, at this point only in evenings and nights.

      Also, if anyone wants to use this, I can go go through and make it more user friendly for someone not familiar with writing code.

      Future plans include:
      Allow day time storms, darkening light ambiance to suit
      Adding synchronized storm sounds
      Changing the colors to look more realistic
      Setting time cycle to more closely match real day light levels
      (maybe...) adding a fast clock on a LCD display shield
      Controls to jump to certain times of day

      Video will be uploaded tomorrow.

      Here's the code:
      Code:
      /*
      * Code for cross-fading 3 LEDs, red, green and blue (RGB) 
      * To create fades, you need to do two things: 
      *  1. Describe the colors you want to be displayed
      *  2. List the order you want them to fade in
      *
      * DESCRIBING A COLOR:
      * A color is just an array of three percentages, 0-100, 
      *  controlling the red, green and blue LEDs
      *
      * Red is the red LED at full, blue and green off
      *   int red = { 100, 0, 0 }
      * Dim white is all three LEDs at 30%
      *   int dimWhite = {30, 30, 30}
      * etc.
      *
      * Some common colors are provided below, or make your own
      * 
      * LISTING THE ORDER:
      * In the main part of the program, you need to list the order 
      *  you want to colors to appear in, e.g.
      *  crossFade(red);
      *  crossFade(green);
      *  crossFade(blue);
      *
      * Those colors will appear in that order, fading out of 
      *    one color and into the next  
      *
      * In addition, there are 5 optional settings you can adjust:
      * 1. The initial color is set to black (so the first color fades in), but 
      *    you can set the initial color to be any other color
      * 2. The internal loop runs for 1020 interations; the 'wait' variable
      *    sets the approximate duration of a single crossfade. In theory, 
      *    a 'wait' of 10 ms should make a crossFade of ~10 seconds. In 
      *    practice, the other functions the code is performing slow this 
      *    down to ~11 seconds on my board. YMMV.
      * 3. If 'repeat' is set to 0, the program will loop indefinitely.
      *    if it is set to a number, it will loop that number of times,
      *    then stop on the last color in the sequence. (Set 'return' to 1, 
      *    and make the last color black if you want it to fade out at the end.)
      * 4. There is an optional 'hold' variable, which pasues the 
      *    program for 'hold' milliseconds when a color is complete, 
      *    but before the next color starts.
      * 5. Set the DEBUG flag to 1 if you want debugging output to be
      *    sent to the serial monitor.
      *
      *    The internals of the program aren't complicated, but they
      *    are a little fussy -- the inner workings are explained 
      *    below the main loop.
      */ 
      
      
      
      
      //Input
      int sensorPin = A0;    // select the input pin for the potentiometer for cycle speed
      int sensorValue = 20;  // variable to store the value coming from the sensor
      int knob = 20;
      
      
      
      
      // Output
      int cwhPin = 3;   // Cool White LED, connected to digital pin 3
      int wwhPin = 5;   // Warm White LED, connected to digital pin 5
      //int auxPin = 6;   // For other LED (if any) connected to digital pin 6
      int redPin = 9;   // Red LED,   connected to digital pin 9
      int grnPin = 10;  // Green LED, connected to digital pin 10
      int bluPin = 11;  // Blue LED,  connected to digital pin 11
      
      
      
      
      // Color arrays
      int white[5]  =     { 100, 100, 100, 100, 100 };
      int red[5]    =     { 100, 0, 0, 0, 0 };
      int green[5]  =     { 0, 100, 0, 0, 0 };
      int blue[5]   =     { 0, 0, 100, 0, 0 };
      int yellow[5] =     { 60, 50, 0, 0, 0 };
      int dimWhite[5] =   { 30, 30, 30, 30, 30 };
      int black[5]  =     { 0, 0, 0, 0, 0 };
      
      
      
      
      int dawnone[5] =    { 98, 90, 70, 0, 0};
      int dawntwo[5]  =   { 95, 20, 10, 0, 0 };
      int dawnthree[5] =  { 100, 57, 30, 70, 50 };
      int carbonarc[5]  = { 100, 97, 80 , 70, 100};
      int sun[5] =        { 100, 100, 80, 100, 100 };
      int noon[5] =       { 100, 100, 90, 100, 100 };
      int duskone[5] =    { 97, 85, 56, 0, 0 };
      int dusktwo[5] =    { 20, 40, 70, 0, 0 };
      int blueone[5] =    { 0, 10, 30, 0, 0 };
      int bluetwo[5] =    { 0, 0, 20, 0, 0 };
      // etc.
      
      
      
      
      // Set initial color
      int redVal = black[0];
      int grnVal = black[1]; 
      int bluVal = black[2];
      int cwhVal = black[3];
      int wwhVal = black[4];
      
      
      
      
      //int wait = sensorValue;      // 10ms internal crossFade delay; increase for slower fades
      int hold = 0;       // Optional hold when a color is complete, before the next crossFade
      int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
      int loopCount = 60; // How often should DEBUG report?
      int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
      int j = 0;          // Loop counter for repeat
      
      
      
      
      // Initialize color variables
      int prevR = redVal;
      int prevG = grnVal;
      int prevB = bluVal;
      int prevC = cwhVal;
      int prevW = wwhVal;
      
      
      //Lightning stuff
      int ledPin11 = 11;
      int burst; //short burst of light
      int big;
      int srumble;
      int brumble;
      int mixer;
      int diming;
      int delaymix;
      int dark;
      int lightningtimer;
      int lightningtime;
      int count;
      int wait;
      int lightningOn;
      int lightningSelect = 1; //Set to 0 to disable lightning
      
      
      // Set up the LED outputs
      void setup()
      {
        pinMode(redPin, OUTPUT);   // sets the pins as output
        pinMode(grnPin, OUTPUT);   
        pinMode(bluPin, OUTPUT); 
        pinMode(cwhPin, OUTPUT);
        pinMode(wwhPin, OUTPUT);
        
        if (DEBUG) {           // If we want to see values for debugging...
          Serial.begin(9600);  // ...set up the serial ouput 
        }
      }
      
      
      
      
      // Main program: list the order of crossfades
      void loop()
      
      
      
      
        // read the value from the sensor:
        {
        crossFade(dawntwo);
        crossFade(dawnone);
        crossFade(dawnthree);
        crossFade(carbonarc);
        crossFade(sun);
        crossFade(noon);
        crossFade(sun);
        crossFade(duskone);
        crossFade(black);
        crossFade(dusktwo);
        crossFade(blueone);
        crossFade(bluetwo);
        crossFade(black);
        crossFade(black);
        
      
      
      
      
        if (repeat) { // Do we loop a finite number of times?
          j += 1;
          if (j >= repeat) { // Are we there yet?
            exit(j);         // If so, stop.
          }
        }
        
        sensorValue = analogRead(sensorPin);
        int wait = sensorValue;
      }
      
      
      
      
      /* BELOW THIS LINE IS THE MATH -- YOU SHOULDN'T NEED TO CHANGE THIS FOR THE BASICS
      * 
      * The program works like this:
      * Imagine a crossfade that moves the red LED from 0-10, 
      *   the green from 0-5, and the blue from 10 to 7, in
      *   ten steps.
      *   We'd want to count the 10 steps and increase or 
      *   decrease color values in evenly stepped increments.
      *   Imagine a + indicates raising a value by 1, and a -
      *   equals lowering it. Our 10 step fade would look like:
      * 
      *   1 2 3 4 5 6 7 8 9 10
      * R + + + + + + + + + +
      * G   +   +   +   +   +
      * B     -     -     -
      * 
      * The red rises from 0 to 10 in ten steps, the green from 
      * 0-5 in 5 steps, and the blue falls from 10 to 7 in three steps.
      * 
      * In the real program, the color percentages are converted to 
      * 0-255 values, and there are 1020 steps (255*4).
      * 
      * To figure out how big a step there should be between one up- or
      * down-tick of one of the LED values, we call calculateStep(), 
      * which calculates the absolute gap between the start and end values, 
      * and then divides that gap by 1020 to determine the size of the step  
      * between adjustments in the value.
      */
      
      
      
      
      int calculateStep(int prevValue, int endValue) {
        int step = endValue - prevValue; // What's the overall gap?
        if (step) {                      // If its non-zero, 
          step = 1020/step;              //   divide by 1020
        } 
        return step;
      }
      
      
      
      
      /* The next function is calculateVal. When the loop value, i,
      *  reaches the step size appropriate for one of the
      *  colors, it increases or decreases the value of that color by 1. 
      *  (R, G, and B are each calculated separately.)
      */
      
      
      
      
      int calculateVal(int step, int val, int i) {
      
      
      
      
        if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
          if (step > 0) {              //   increment the value if step is positive...
            val += 1;           
          } 
          else if (step < 0) {         //   ...or decrement it if step is negative
            val -= 1;
          } 
        }
        // Defensive driving: make sure val stays in the range 0-255
        if (val > 255) {
          val = 255;
        } 
        else if (val < 0) {
          val = 0;
        }
        return val;
      }
      
      
      
      
      /* crossFade() converts the percentage colors to a 
      *  0-255 range, then loops 1020 times, checking to see if  
      *  the value needs to be updated each time, then writing
      *  the color values to the correct pins.
      */
      
      
      
      
      void crossFade(int color[5]) {
        // Convert to 0-255
        int R = (color[0] * 255) / 100;
        int G = (color[1] * 255) / 100;
        int B = (color[2] * 255) / 100;
        int C = (color[3] * 255) / 100;
        int W = (color[4] * 255) / 100;
      
      
      
      
        int stepR = calculateStep(prevR, R);
        int stepG = calculateStep(prevG, G); 
        int stepB = calculateStep(prevB, B);
        int stepC = calculateStep(prevC, C);
        int stepW = calculateStep(prevW, W);
      
      
      
      
        for (int i = 0; i <= 1020; i++) {
          redVal = calculateVal(stepR, redVal, i);
          grnVal = calculateVal(stepG, grnVal, i);
          bluVal = calculateVal(stepB, bluVal, i);
          cwhVal = calculateVal(stepC, cwhVal, i);
          wwhVal = calculateVal(stepW, wwhVal, i);
        
          analogWrite(redPin, redVal);   // Write current values to LED pins (added the "255 - " to each of these three to accomodate common anode LED. ex:  analogWrite(wwhPin, 255 - wwhVal);   ) Andrew Kyle
          analogWrite(grnPin, grnVal);      
          analogWrite(bluPin, bluVal);
          analogWrite(cwhPin, cwhVal);
          analogWrite(wwhPin, wwhVal); 
      
      
      
      
          delay(analogRead(sensorPin)); // Pause for 'wait' milliseconds before resuming the loop
          knob = analogRead(sensorPin);
      //lightning
          lightningOn = random(1-50000);
          if (W < 30 && C < 30 && R < 20 && G < 20 && lightningOn == 1 && lightningSelect != 0){
            burst = random(10,100); //Short burst of lightning value
            big = random(500,1000);//long burst of lightning value
            srumble = random(10,40);//lower light lighting value
            brumble = random(10,150);//higher light lightning value
            mixer = random(burst,big);//random pick of short or long bursts of lightning
            dark = random(1000,12000);//random pick of time gap for lightning
            count = random(3,7);//how long storm lasts
            for(int i = 0; i < count; i++){
              burst = random(10,500); //Short burst of lightning value
              big = random(500,1000);//long burst of lightning value
              srumble = random(1,50);//lower light lighting value
              brumble = random(50,1503);//higher light lightning value
              mixer = random(burst,big);//random pick of short or long bursts of lightning
              for(int i = 0; i < mixer; i += diming++){
                diming = random(1,150);
                analogWrite(cwhPin, random(srumble,brumble));//Turn on LED for short or long burst
                analogWrite(wwhPin, random(srumble,brumble));//Turn on LED for short or long burst
                delay(diming);//random number delay to remain at set brightness value
              }
              digitalWrite(cwhPin, LOW);    // turn the LED off
              digitalWrite(wwhPin, LOW);    // turn the LED off
              //delay(dark);
              wait = random(1000, 12000);
              delay(wait);
              count - 1;
              if (count < 0){
                break;
              }
            }
          }
      //end lightning
          if (DEBUG) { // If we want serial output, print it at the 
            if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
              Serial.print("Loop/RGBCW: #");
              Serial.print(i);
              Serial.print(" | ");
              Serial.print(redVal);
              Serial.print(" / ");
              Serial.print(grnVal);
              Serial.print(" / ");
              Serial.print(bluVal);
              Serial.print(" / ");
              Serial.print(cwhVal);
              Serial.print(" / ");
              Serial.println(wwhVal);
              Serial.print(knob);
              Serial.print(" , ");
              Serial.println(sensorPin);
              
            } 
            DEBUG += 1;
          }
        }
        // Update current values for next loop
        prevR = redVal; 
        prevG = grnVal; 
        prevB = bluVal;
        prevC = cwhVal;
        prevW = wwhVal;
        delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
      }
      Andrew
    1. ScottL's Avatar
      ScottL -
      So awesome. Thanks for posting the code.
    1. zosimas's Avatar
      zosimas -
      Wheres the video of thunder storm?
    1. conrailandrew's Avatar
      conrailandrew -
      Quote Originally Posted by zosimas View Post
      Wheres the video of thunder storm?
      Right here:


      There are three storms in that clip, the last one is the most dramatic.
      Also, the reason it cycles through the day so fast is because I have the speed turned all the way up to increase the chances of a thunderstorm coming along. Even so I was pretty lucky to get three thunderstorms in that amount of time. Sometimes it will go for quite a few day/night cycles before spawning another storm.
      This video was taken before some modifications that required it to be a bit darker before storming, and before I added the code that also makes the second white strip flash with the lightning, making the flashes brighter. It's pretty impressive when a thunderstorm happens in the middle of the 'night', it can be rather of a surprise when that first strike hits.

      Andrew