/* PulseLed.cpp - Pulse Width Modulation LED Class Implementation This class is used to control an LED connected to one of the PWM output pins to provide a fading effect during turn on and turn off. On the Arduino Nano, these include pins 3, 5, 6, 9, 10, and 11. This class is derived from the "PlainLed" class. The "on", "off", and "toggle" functions override their counterparts in the base class. New functions include "setMax", "setFading","setUpTime", "setDownTime", and "update". Retained from the base class are "begin", "isOn", and "isOff". */ #include "PulseLed.h" /* setMax - Set the Maximum Intensity for the LED void setMax (byte max) Establishes the the highest value to be set for the LED using the "analogWrite" method. This is the value that will be used when the LED has reached the fully on state. */ void PulseLed::setMax (byte max) { _max = max; _valueInc = _max / DIVISIONS; } /* getMax - Obtain the Maximum Intensity for the LED byte getMax () Retrieves the maximum intensity value set for the LED. */ byte PulseLed::getMax () { return _max; } /* setFading - Set Fading Enabled void setFading (bool enabled) The fading effect can be enabled or disabled using this method. The specied boolean value "enabled" determines the disposition of this option. True will enable fading, false will disable fading. With fading disabled, the LED will be set to full on or full off without intermediate values. */ void PulseLed::setFading (bool enabled) { _enabled = enabled; } /* isFading - Is Fading Enabled bool isFading () Returns true if fading is enabled, false otherwise. */ bool PulseLed::isFading () { return _enabled; } /* setUpTime - Set Fade Up Time void setUpTime (int upTime) The fade up time indicates how long it should take to achieve full intensity when turning the LED on. The specified parameter "upTime" is the number of milliseconds this fade up should take. */ void PulseLed::setUpTime (int upTime) { _upTime = upTime; _upTimeInc = _upTime / DIVISIONS; } /* getUpTime - Get Fade Up Time int getUpTime () Retrieves the fade up time. The value returned is in milliseconds. */ int PulseLed::getUpTime () { return _upTime; } /* setDownTime - Set Fade Down Time void setDownTime (int downTime) The fade down time indicates how long it should take to achieve full off or zero intensity of the LED when turning off. The parameter specified as "downTime" provides a value representing the number of milliseconds that this fade down should take. */ void PulseLed::setDownTime (int downTime) { _downTime = downTime; _downTimeInc = _downTime / DIVISIONS; } /* getDownTime - Get Fade Down Time int getDownTime () Retrieves the fade down time. The value returned is in milliseconds. */ int PulseLed::getDownTime () { return _downTime; } /* on - Turn the LED On void on () The LED is turned on either by fading up or by going to maximum intesity. Fading occurs if it is enabled and the fade up time is not 0. */ void PulseLed::on () { _fading = _enabled && (_upTime != 0); _fadeUp = true; turnOn(); _isOn = true; } /* off - Turn the LED Off void off () The LED is turned off either by fading down or by going to 0 intensity. Fading occurs if it is enabled and the fade down time is not 0. */ void PulseLed::off () { _fading = _enabled && (_downTime != 0); _fadeUp = false; turnOff(); _isOn = false; } /* toggle - Change the State of the LED void toggle () If the LED is on, it is turned off. If the LED is off, it is turned on. */ void PulseLed::toggle () { if (_isOn) { off(); } else { on(); } } /* turnOn - Conditional Turn On of LED void turnOn () If fading is engaged, this function begins the fade up process. Otherwise, the LED is set directly to maximum intesity. */ void PulseLed::turnOn () { if (_fading) { changeFade(); _timer = millis(); } else { analogWrite(_pin, _max); } } /* turnOff - Conditional Turn Off of LED void turnOff () If fading is engaged, this function begins the fade down process. Otherwise, the LED is set directly to 0 intensity. */ void PulseLed::turnOff () { if (_fading) { changeFade(); _timer = millis(); } else { analogWrite(_pin, 0); } } /* changeFade - Change PWM Value to the LED void changeFade () This function is used when it is time to adjust the PWM value to produce the fading effect. If fading is not indicated, then nothing happens. Otherwise "analogWrite" is called to set the new value. Following this, a new PWM value is calculated for either fading up or fading down. The proper increment is applied until intensity limits have been reached. When fading up, if the value has reached the maximum intensity value for the LED, then fading is stopped. The LED will be at full intensity. When fading down, if the value has reached 0 intensity, then fading is stopped and the LED will be fully off. */ void PulseLed::changeFade () { if (_fading) { analogWrite(_pin, _value); if (_fadeUp) { if (_value == _max) { _fading = false; } else { if (_value >= _max - 10) { _value = _max; } else { _value += _valueInc; } } } else { if (_value == 0) { _fading = false; } else { if (_value <= 10) { _value = 0; } else { _value -= _valueInc; } } } } } /* update - Time Based Update void update () Checks to see if a change to the PWM value is required. If not, it returns immediately without any further action. If the time interval for fading up or down has expired, the timer is set to the current time and "changeFade" is executed. */ void PulseLed::update () { if (_fading) { unsigned long now = millis(); if (_fadeUp) { if (now - _timer >= _upTimeInc) { _timer = now; changeFade(); } } else { if (now - _timer >= _downTimeInc) { _timer = now; changeFade(); } } } }