Skip to content

Commit

Permalink
Corrected fade mechanism
Browse files Browse the repository at this point in the history
Optimized interrupt number of commands
  • Loading branch information
Arnd authored and Arnd committed Dec 21, 2016
1 parent 6e15612 commit 0161394
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
13 changes: 8 additions & 5 deletions Examples/StandardColors/StandardColors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
*******************************************************************************************************************/
#include <RotaryEncoder.h> // Include Encoder library //
//----------------------------------//
const uint8_t ROTARY_PIN_1 = 2; // Pin for left rotary encoder pin //
const uint8_t ROTARY_PIN_2 = 3; // Pin for right rotary encoder pin //
const uint8_t ROTARY_PIN_1 = 0; // Pin for left rotary encoder pin //
const uint8_t ROTARY_PIN_2 = 1; // Pin for right rotary encoder pin //
const uint8_t PUSHBUTTON_PIN = 7; // Pin for pushbutton connector pin //
const uint8_t RED_PIN = 11; // Red LED PWM pin. Ground = FULL //
const uint8_t GREEN_PIN = 10; // Green LED PWM pin. Ground = FULL //
Expand All @@ -46,15 +46,18 @@ EncoderClass Encoder(ROTARY_PIN_1, ROTARY_PIN_2, PUSHBUTTON_PIN, //
RED_PIN, GREEN_PIN, BLUE_PIN); // of the pins that are used //
//----------------------------------//
void setup() { // Start One-Time run section //
Encoder.SetFadeRate(0); // Turn off fading //
Encoder.SetColor(0,0,0); // Set LED full on, allow to fade //
Encoder.SetFadeRate(1); // Set fastest fade rate (default) //
Serial.begin(115200); // Initialize Serial I/O at speed //
delay(1000); // Wait 1 second for initialization //
delay(3000); // Wait 3 seconds for initialization//
Encoder.SetFadeRate(20); // Set slow 20ms per tick fade rate //
Serial.println(F("Starting Encoder Program...")); // //
Serial.println(F("Default clockwise = Green,")); // //
Serial.println(F("Default Counterclockwise = Blue,")); // //
Serial.println(F("Default Pushbutton = Red,")); // //
Serial.println(F("Default Pushbutton = Red,")); // //
Serial.println(F("Default fast fade rate.")); // //
delay(2000); // Give the fade time to work //
Encoder.SetFadeRate(3); // Set fast 3ms fade rate //
} // of method setup() // //
//----------------------------------//
void loop(){ // Main program infinite loop //
Expand Down
50 changes: 35 additions & 15 deletions RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,26 @@ static void EncoderClass::TimerISR() {ClassPtr->TimerHandler();} //
** RGB LEDs of the device. It is also the only place where the actual PWM values for RGB are set. **
*******************************************************************************************************************/
void EncoderClass::TimerHandler() { // //
if (_LEDChanged || !(_RedActual==255&&_GreenActual==255&&_BlueActual==255)){// Only check if LEDs aren't off //
_LEDChanged = false; // Reset the value //
if (_RedActual!=_RedTarget) { // adjust accordingly //
if(_RedActual<_RedTarget) _RedActual++; else _RedActual--; // //
} // of if-then actual and target don't match // //
if (_GreenActual!=_GreenTarget) { // //
if(_GreenActual<_GreenTarget) _GreenActual++; else _GreenActual--; // //
} // of if-then actual and target don't match // //
if (_BlueActual!=_BlueTarget) { // //
if(_BlueTarget<_BlueTarget) _BlueActual++; else _BlueActual--; // //
} // of if-then actual and target don't match // //
if (_FadeMillis!=0 && millis()%_FadeMillis==0 ) { // If we are fading colors, then //
if (_LEDChanged || // Only check if LEDs aren't off //
!(_RedActual==255 && _GreenActual==255 && _BlueActual==255)) { // //
if (millis()%_FadeMillis==0 ) { // If we are fading colors, then //
_LEDChanged = false; // Reset the value //
if (_RedActual!=_RedTarget) { // adjust accordingly //
if(_RedActual<_RedTarget) _RedActual++; else _RedActual--; // //
} // of if-then actual and target don't match // //
if (_GreenActual!=_GreenTarget) { // //
if(_GreenActual<_GreenTarget) _GreenActual++; else _GreenActual--; // //
} // of if-then actual and target don't match // //
if (_BlueActual!=_BlueTarget) { // //
if(_BlueTarget<_BlueTarget) _BlueActual++; else _BlueActual--; // //
} // of if-then actual and target don't match // //
if (_RedTarget !=255&&_RedActual==_RedTarget) _RedTarget++; // Fade Red if max has been reached //
if (_GreenTarget!=255&&_GreenActual==_GreenTarget) _GreenTarget++; // Fade Green " " //
if (_BlueTarget !=255&&_BlueActual==_BlueTarget) _BlueTarget++; // Fade Blue " " //
analogWrite(_RedPin,_RedActual); // show the Red, //
analogWrite(_GreenPin,_GreenActual); // Green, and //
analogWrite(_BluePin,_BlueActual); // Blue values //
} // of if-then we want to fade LED brightness // //
analogWrite(_RedPin,_RedActual); // show the Red, //
analogWrite(_GreenPin,_GreenActual); // Green, and //
analogWrite(_BluePin,_BlueActual); // Blue values //
} // of if-then we need to do something // //
} // of method FaderButtonHandler() // //
/*******************************************************************************************************************
Expand All @@ -83,6 +84,11 @@ void EncoderClass::PushButtonHandler() { //
_RedTarget = _ColorPushButtonR; // Set target color //
_GreenTarget = _ColorPushButtonG; // Set target color //
_BlueTarget = _ColorPushButtonB; // Set target color //
if (_FadeMillis==0) { // Manually set if no fade //
analogWrite(_RedPin,_RedTarget); // show the Red, //
analogWrite(_GreenPin,_GreenTarget); // Green, and //
analogWrite(_BluePin,_BlueTarget); // Blue values //
} // of if fading is turned off // //
} // of if-then we have a valid pushbutton event // //
} // of method PushButtonHandler() // //
/*******************************************************************************************************************
Expand Down Expand Up @@ -110,6 +116,11 @@ void EncoderClass::PushButtonHandler() { //
_BlueTarget = _ColorCCWB; // Set target color //
} // of if-then a CCW turn // //
lastEncoded = encoded; // store the value for next time //
if (_FadeMillis==0) { // Manually set if no fade //
analogWrite(_RedPin,_RedTarget); // show the Red, //
analogWrite(_GreenPin,_GreenTarget); // Green, and //
analogWrite(_BluePin,_BlueTarget); // Blue values //
} // of if fading is turned off // //
} // of method RotateHandler() // //
/*******************************************************************************************************************
** function ButtonPushes() returns number of button pushes since the last call and resets the value **
Expand All @@ -123,9 +134,18 @@ uint8_t EncoderClass::GetButton() { //
** function SetColor() is called to set the RGB values to set when the button is pushed **
*******************************************************************************************************************/
void EncoderClass::SetColor(const uint8_t R,const uint8_t G,const uint8_t B) {// //
_RedActual = R; // set internal values //
_GreenActual = G; // set internal values //
_BlueActual = B; // set internal values //
_RedTarget = R; // set internal values //
_GreenTarget = G; // set internal values //
_BlueTarget = B; // set internal values //
_LEDChanged = true; // Mark that we have a change //
if (_FadeMillis==0) { // Manually set if no fade //
analogWrite(_RedPin,_RedTarget); // show the Red, //
analogWrite(_GreenPin,_GreenTarget); // Green, and //
analogWrite(_BluePin,_BlueTarget); // Blue values //
} // of if fading is turned off // //
} // of method SetColor // //
/*******************************************************************************************************************
** function SetPushButtonColor() is called to set the RGB values to set when the button is pushed **
Expand Down
13 changes: 7 additions & 6 deletions RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
** **
** Vers. Date Developer Comments **
** ====== ========== =================== ======================================================================== **
** 1.0.3 2016-12-21 [email protected] Corrected volatile variables and fixed SetColor() call **
** 1.0.2 2016-12-18 [email protected] Changed SetFade() to SetFadeRate() function to alter the fade speed **
** 1.0.1 2016-12-14 [email protected] Fixed error on condition to turn off LED lights. **
** 1.0.0 2016-12-14 [email protected] Allowed defaults for LEDs on class constructer **
Expand Down Expand Up @@ -93,12 +94,12 @@
volatile bool _LEDChanged = true; // Set when rotate or click changes //
volatile uint8_t _ButtonPresses = 0; // The current number of pushes //
volatile long _EncoderValue = 0; // The current encoder value //
uint8_t _RedActual = 255; // Actual value for the Red LED //
uint8_t _RedTarget = 255; // Target value for the Red LED //
uint8_t _GreenActual = 255; // Actual value for the Green LED //
uint8_t _GreenTarget = 255; // Target value for the Green LED //
uint8_t _BlueActual = 255; // Actual value for the Blue LED //
uint8_t _BlueTarget = 255; // Target value for the Blue LED //
volatile uint8_t _RedActual = 255; // Actual value for the Red LED //
volatile uint8_t _RedTarget = 255; // Target value for the Red LED //
volatile uint8_t _GreenActual = 255; // Actual value for the Green LED //
volatile uint8_t _GreenTarget = 255; // Target value for the Green LED //
volatile uint8_t _BlueActual = 255; // Actual value for the Blue LED //
volatile uint8_t _BlueTarget = 255; // Target value for the Blue LED //
uint8_t _ColorPushButtonR = 0; // Default pushbutton to pure Red //
uint8_t _ColorPushButtonG = 255; // //
uint8_t _ColorPushButtonB = 255; // //
Expand Down

0 comments on commit 0161394

Please sign in to comment.