Fading a light on and off (OOP)
Well, any real lamp does not turn on and off in 3 picoseconds, so how would you get it to fade in and out a little more realistically?
Let's say we need to turn a light on, so from complete darkness, either we pulse it on for a short time, then pulse it on a little longer and keep
doing this until it is all the way on forever, or we use the analogWrite() method on PWM capable pin and write the values 0 through 255 to it
sequentially and at 255 it is all the way on. Now, the first part is doing the same thing, but we have to do all the math as well, so I am going to stick to the
simpler 2nd choice. All we need is to know how often do we need to update the light (less than 50 milliseconds prevents unwanted flashing), how long do we need
the fading to take, 1 second, 10 seconds or 2 days...well remember, we only have to move from 0 to 255, so, if your time gets too long, then we update the time before we do
another update, see the math in the constructor.
The now and before in the loop() function is only to demo the Toggling from Off to On every few seconds, you would have your input or sensor making this
choice on when you turn on or off instead.
The file attached at the bottom also contains some Serial debugging lines, commented out, so just ignore them or use them!
//// FadeLite, by Speed//// FadeLite on an analog pin, using a PWM pin with analogWrite// #define BAUD 9600 // ledpin, needs to be a PWM capable one: 3,5,6,9,10 or 11 #define PIN 3 /*// time in milliseconds, good values for turning a lamp on and off#define FADETIME 500#define UPDATETIME 20*/// time in milliseconds, better values for a demo #define FADETIME 2000 #define UPDATETIME 50 class FadeLite { // Class Member Variables // These are initialized at startup int ledPin; // the number of the LED pin long int fadeTime; // how long to spend fading long int stepSize; // size of steps to take to fade long int updateTime; // how often to update // These maintain the current state int ledState; // ledState used to set the LED, 0 off, 1 on, 2 fading to on, 3 fading to off int ledValue; // 0 thru 255 for full off thru full on unsigned long previousMillis; // will store last time LED was updated void Output() { analogWrite( ledPin, ledValue ); } // Outout public: // Constructor - creates a Flasher // and initializes the member variables and state FadeLite( int pin, long int fTime ) { ledPin = pin; pinMode( ledPin, OUTPUT ); updateTime = UPDATETIME; fadeTime = fTime; stepSize = int( 256 * ( (1.0 * UPDATETIME ) / ( 1.0 * fadeTime ) ) ); if ( stepSize < 1 ) { stepSize = 1; updateTime = fadeTime / 255; } // if FullOff(); previousMillis = millis(); } // FadeLite constructor // I used this to send some serial data to the PC. Nice to use when you need // to see what the math looks like when you want to increase the updatetime since // your steps are already 255 and you want the fading to take longer than UPDATETIME * 255! void FullOn() { ledValue = 255; ledState = 1; Output(); } // FullOn void FullOff() { ledValue = 0; ledState = 0; Output(); } // FullOff void Reset() { FullOff(); previousMillis = millis(); } // Reset bool IsOn() { return ledState == 1; } // IsOn() bool IsOff() { return ledState == 0; } // IsOff() void Toggle() { if ( ledState == 0 ) { //FullOn();// TurnOn(); } else { if ( ledState == 1 ) { //FullOff(); // TurnOff(); } // if } // if } // Toggle() void TurnOn() { ledState = 2; } // TurnOn() void TurnOff() { ledState = 3; } // TurnOff() void Update() { // check to see if it's time to change the state of the LED unsigned long now = millis(); if ( now - previousMillis >= updateTime ) { previousMillis = now; switch ( ledState ) { case 0: break; case 1: break; case 2: ledValue += stepSize; if ( ledValue >= 255 ) { ledValue = 255; ledState = 1; } // if on break; case 3: ledValue -= stepSize; if ( ledValue <= 0 ) { ledValue = 0; ledState = 0; } // if of break; }; // switch Output(); // Update the actual LED } // if, time for change yet? } // Update }; // FadeLite FadeLite lite1( PIN, FADETIME ); unsigned long int now; unsigned long int before; void setup() { before = millis();
// lite1.TurnOn();
} // setup void loop() { lite1.Update(); now = millis(); if ( now - before > 5000 ) { lite1.Toggle(); before = now; } // if } // loop