Skip to content

Commit 0161394

Browse files
ArndArnd
Arnd
authored and
Arnd
committed
Corrected fade mechanism
Optimized interrupt number of commands
1 parent 6e15612 commit 0161394

File tree

3 files changed

+50
-26
lines changed

3 files changed

+50
-26
lines changed

Examples/StandardColors/StandardColors.ino

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
*******************************************************************************************************************/
3636
#include <RotaryEncoder.h> // Include Encoder library //
3737
//----------------------------------//
38-
const uint8_t ROTARY_PIN_1 = 2; // Pin for left rotary encoder pin //
39-
const uint8_t ROTARY_PIN_2 = 3; // Pin for right rotary encoder pin //
38+
const uint8_t ROTARY_PIN_1 = 0; // Pin for left rotary encoder pin //
39+
const uint8_t ROTARY_PIN_2 = 1; // Pin for right rotary encoder pin //
4040
const uint8_t PUSHBUTTON_PIN = 7; // Pin for pushbutton connector pin //
4141
const uint8_t RED_PIN = 11; // Red LED PWM pin. Ground = FULL //
4242
const uint8_t GREEN_PIN = 10; // Green LED PWM pin. Ground = FULL //
@@ -46,15 +46,18 @@ EncoderClass Encoder(ROTARY_PIN_1, ROTARY_PIN_2, PUSHBUTTON_PIN, //
4646
RED_PIN, GREEN_PIN, BLUE_PIN); // of the pins that are used //
4747
//----------------------------------//
4848
void setup() { // Start One-Time run section //
49+
Encoder.SetFadeRate(0); // Turn off fading //
4950
Encoder.SetColor(0,0,0); // Set LED full on, allow to fade //
50-
Encoder.SetFadeRate(1); // Set fastest fade rate (default) //
5151
Serial.begin(115200); // Initialize Serial I/O at speed //
52-
delay(1000); // Wait 1 second for initialization //
52+
delay(3000); // Wait 3 seconds for initialization//
53+
Encoder.SetFadeRate(20); // Set slow 20ms per tick fade rate //
5354
Serial.println(F("Starting Encoder Program...")); // //
5455
Serial.println(F("Default clockwise = Green,")); // //
5556
Serial.println(F("Default Counterclockwise = Blue,")); // //
56-
Serial.println(F("Default Pushbutton = Red,")); // //
57+
Serial.println(F("Default Pushbutton = Red,")); // //
5758
Serial.println(F("Default fast fade rate.")); // //
59+
delay(2000); // Give the fade time to work //
60+
Encoder.SetFadeRate(3); // Set fast 3ms fade rate //
5861
} // of method setup() // //
5962
//----------------------------------//
6063
void loop(){ // Main program infinite loop //

RotaryEncoder.cpp

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,26 @@ static void EncoderClass::TimerISR() {ClassPtr->TimerHandler();} //
4848
** RGB LEDs of the device. It is also the only place where the actual PWM values for RGB are set. **
4949
*******************************************************************************************************************/
5050
void EncoderClass::TimerHandler() { // //
51-
if (_LEDChanged || !(_RedActual==255&&_GreenActual==255&&_BlueActual==255)){// Only check if LEDs aren't off //
52-
_LEDChanged = false; // Reset the value //
53-
if (_RedActual!=_RedTarget) { // adjust accordingly //
54-
if(_RedActual<_RedTarget) _RedActual++; else _RedActual--; // //
55-
} // of if-then actual and target don't match // //
56-
if (_GreenActual!=_GreenTarget) { // //
57-
if(_GreenActual<_GreenTarget) _GreenActual++; else _GreenActual--; // //
58-
} // of if-then actual and target don't match // //
59-
if (_BlueActual!=_BlueTarget) { // //
60-
if(_BlueTarget<_BlueTarget) _BlueActual++; else _BlueActual--; // //
61-
} // of if-then actual and target don't match // //
62-
if (_FadeMillis!=0 && millis()%_FadeMillis==0 ) { // If we are fading colors, then //
51+
if (_LEDChanged || // Only check if LEDs aren't off //
52+
!(_RedActual==255 && _GreenActual==255 && _BlueActual==255)) { // //
53+
if (millis()%_FadeMillis==0 ) { // If we are fading colors, then //
54+
_LEDChanged = false; // Reset the value //
55+
if (_RedActual!=_RedTarget) { // adjust accordingly //
56+
if(_RedActual<_RedTarget) _RedActual++; else _RedActual--; // //
57+
} // of if-then actual and target don't match // //
58+
if (_GreenActual!=_GreenTarget) { // //
59+
if(_GreenActual<_GreenTarget) _GreenActual++; else _GreenActual--; // //
60+
} // of if-then actual and target don't match // //
61+
if (_BlueActual!=_BlueTarget) { // //
62+
if(_BlueTarget<_BlueTarget) _BlueActual++; else _BlueActual--; // //
63+
} // of if-then actual and target don't match // //
6364
if (_RedTarget !=255&&_RedActual==_RedTarget) _RedTarget++; // Fade Red if max has been reached //
6465
if (_GreenTarget!=255&&_GreenActual==_GreenTarget) _GreenTarget++; // Fade Green " " //
6566
if (_BlueTarget !=255&&_BlueActual==_BlueTarget) _BlueTarget++; // Fade Blue " " //
67+
analogWrite(_RedPin,_RedActual); // show the Red, //
68+
analogWrite(_GreenPin,_GreenActual); // Green, and //
69+
analogWrite(_BluePin,_BlueActual); // Blue values //
6670
} // of if-then we want to fade LED brightness // //
67-
analogWrite(_RedPin,_RedActual); // show the Red, //
68-
analogWrite(_GreenPin,_GreenActual); // Green, and //
69-
analogWrite(_BluePin,_BlueActual); // Blue values //
7071
} // of if-then we need to do something // //
7172
} // of method FaderButtonHandler() // //
7273
/*******************************************************************************************************************
@@ -83,6 +84,11 @@ void EncoderClass::PushButtonHandler() { //
8384
_RedTarget = _ColorPushButtonR; // Set target color //
8485
_GreenTarget = _ColorPushButtonG; // Set target color //
8586
_BlueTarget = _ColorPushButtonB; // Set target color //
87+
if (_FadeMillis==0) { // Manually set if no fade //
88+
analogWrite(_RedPin,_RedTarget); // show the Red, //
89+
analogWrite(_GreenPin,_GreenTarget); // Green, and //
90+
analogWrite(_BluePin,_BlueTarget); // Blue values //
91+
} // of if fading is turned off // //
8692
} // of if-then we have a valid pushbutton event // //
8793
} // of method PushButtonHandler() // //
8894
/*******************************************************************************************************************
@@ -110,6 +116,11 @@ void EncoderClass::PushButtonHandler() { //
110116
_BlueTarget = _ColorCCWB; // Set target color //
111117
} // of if-then a CCW turn // //
112118
lastEncoded = encoded; // store the value for next time //
119+
if (_FadeMillis==0) { // Manually set if no fade //
120+
analogWrite(_RedPin,_RedTarget); // show the Red, //
121+
analogWrite(_GreenPin,_GreenTarget); // Green, and //
122+
analogWrite(_BluePin,_BlueTarget); // Blue values //
123+
} // of if fading is turned off // //
113124
} // of method RotateHandler() // //
114125
/*******************************************************************************************************************
115126
** function ButtonPushes() returns number of button pushes since the last call and resets the value **
@@ -123,9 +134,18 @@ uint8_t EncoderClass::GetButton() { //
123134
** function SetColor() is called to set the RGB values to set when the button is pushed **
124135
*******************************************************************************************************************/
125136
void EncoderClass::SetColor(const uint8_t R,const uint8_t G,const uint8_t B) {// //
137+
_RedActual = R; // set internal values //
138+
_GreenActual = G; // set internal values //
139+
_BlueActual = B; // set internal values //
126140
_RedTarget = R; // set internal values //
127141
_GreenTarget = G; // set internal values //
128142
_BlueTarget = B; // set internal values //
143+
_LEDChanged = true; // Mark that we have a change //
144+
if (_FadeMillis==0) { // Manually set if no fade //
145+
analogWrite(_RedPin,_RedTarget); // show the Red, //
146+
analogWrite(_GreenPin,_GreenTarget); // Green, and //
147+
analogWrite(_BluePin,_BlueTarget); // Blue values //
148+
} // of if fading is turned off // //
129149
} // of method SetColor // //
130150
/*******************************************************************************************************************
131151
** function SetPushButtonColor() is called to set the RGB values to set when the button is pushed **

RotaryEncoder.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
** **
4747
** Vers. Date Developer Comments **
4848
** ====== ========== =================== ======================================================================== **
49+
** 1.0.3 2016-12-21 [email protected] Corrected volatile variables and fixed SetColor() call **
4950
** 1.0.2 2016-12-18 [email protected] Changed SetFade() to SetFadeRate() function to alter the fade speed **
5051
** 1.0.1 2016-12-14 [email protected] Fixed error on condition to turn off LED lights. **
5152
** 1.0.0 2016-12-14 [email protected] Allowed defaults for LEDs on class constructer **
@@ -93,12 +94,12 @@
9394
volatile bool _LEDChanged = true; // Set when rotate or click changes //
9495
volatile uint8_t _ButtonPresses = 0; // The current number of pushes //
9596
volatile long _EncoderValue = 0; // The current encoder value //
96-
uint8_t _RedActual = 255; // Actual value for the Red LED //
97-
uint8_t _RedTarget = 255; // Target value for the Red LED //
98-
uint8_t _GreenActual = 255; // Actual value for the Green LED //
99-
uint8_t _GreenTarget = 255; // Target value for the Green LED //
100-
uint8_t _BlueActual = 255; // Actual value for the Blue LED //
101-
uint8_t _BlueTarget = 255; // Target value for the Blue LED //
97+
volatile uint8_t _RedActual = 255; // Actual value for the Red LED //
98+
volatile uint8_t _RedTarget = 255; // Target value for the Red LED //
99+
volatile uint8_t _GreenActual = 255; // Actual value for the Green LED //
100+
volatile uint8_t _GreenTarget = 255; // Target value for the Green LED //
101+
volatile uint8_t _BlueActual = 255; // Actual value for the Blue LED //
102+
volatile uint8_t _BlueTarget = 255; // Target value for the Blue LED //
102103
uint8_t _ColorPushButtonR = 0; // Default pushbutton to pure Red //
103104
uint8_t _ColorPushButtonG = 255; // //
104105
uint8_t _ColorPushButtonB = 255; // //

0 commit comments

Comments
 (0)