Skip to content

Commit e939263

Browse files
ArndArnd
Arnd
authored and
Arnd
committed
Changed SetFade() to SetFadeRate()
Changed function from boolean to accepting a fade rate in 1/2 milliseconds. Each value equates to 500 microseconds delay.
1 parent 9637f37 commit e939263

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

RotaryEncoder.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ EncoderClass::EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, //
2929
attachInterrupt(digitalPinToInterrupt(LeftPin),RotateISR,CHANGE); // Attach static internal function //
3030
attachInterrupt(digitalPinToInterrupt(RightPin),RotateISR,CHANGE); // Attach static internal function //
3131
attachInterrupt(digitalPinToInterrupt(PushbuttonPin),PushButtonISR,RISING); // Attach static internal function //
32-
if (RedPin==255&&GreenPin==255&&BluePin==255) SetFade(false); // If no LEDs, turn off fader //
33-
else SetFade(true); // turn on fader and interrupt //
32+
if (RedPin==255&&GreenPin==255&&BluePin==255) SetFadeRate(0); // If no LEDs, turn off fader //
33+
else SetFadeRate(1); // turn on fader to max speed //
3434
} // of class constructor // //
35+
/*******************************************************************************************************************
36+
** Define the 5 ISR (Interrupt Service Routines). These definitions are done as static functions which can be set **
37+
** directly as part of the Arduino IDE inside a class definition. They, in turn, redirect the interrupt to a class**
38+
** member function where the actual interrupt is handled **
39+
*******************************************************************************************************************/
3540
ISR(TIMER0_COMPA_vect) {EncoderClass::TimerISR();} // Call the ISR every millisecond //
41+
ISR(TIMER0_COMPB_vect) {EncoderClass::TimerISR();} // Call the ISR every millisecond //
3642
static void EncoderClass::PushButtonISR(){ClassPtr->PushButtonHandler();} // Redirect to real handler function//
3743
static void EncoderClass::RotateISR() {ClassPtr->RotateHandler();} // Redirect to real handler function//
3844
static void EncoderClass::TimerISR() {ClassPtr->TimerHandler();} // Redirect to real handler function//
@@ -42,7 +48,7 @@ static void EncoderClass::TimerISR() {ClassPtr->TimerHandler();} //
4248
** RGB LEDs of the device. It is also the only place where the actual PWM values for RGB are set. **
4349
*******************************************************************************************************************/
4450
void EncoderClass::TimerHandler() { // //
45-
if (_LEDChanged||!(_RedActual==255&&_GreenActual==255&&_BlueActual==255)){ // only check if we need to //
51+
if (_LEDChanged || !(_RedActual==255&&_GreenActual==255&&_BlueActual==255)){// Only check if LEDs aren't off //
4652
_LEDChanged = false; // Reset the value //
4753
if (_RedActual!=_RedTarget) { // adjust accordingly //
4854
if(_RedActual<_RedTarget) _RedActual++; else _RedActual--; // //
@@ -53,7 +59,7 @@ void EncoderClass::TimerHandler() { //
5359
if (_BlueActual!=_BlueTarget) { // //
5460
if(_BlueTarget<_BlueTarget) _BlueActual++; else _BlueActual--; // //
5561
} // of if-then actual and target don't match // //
56-
if (_Fade) { // If we are fading colors, then //
62+
if (_FadeMillis!=0 && millis()%_FadeMillis==0 ) { // If we are fading colors, then //
5763
if (_RedTarget !=255&&_RedActual==_RedTarget) _RedTarget++; // Fade Red if max has been reached //
5864
if (_GreenTarget!=255&&_GreenActual==_GreenTarget) _GreenTarget++; // Fade Green " " //
5965
if (_BlueTarget !=255&&_BlueActual==_BlueTarget) _BlueTarget++; // Fade Blue " " //
@@ -173,22 +179,31 @@ void EncoderClass::SetEncoderValue(const int16_t NewValue = 0) { //
173179
_EncoderValue = NewValue; // Set the new value //
174180
} // of method SetEncoderValue() // //
175181
/*******************************************************************************************************************
176-
** function SetFade() is called to turn the fade functionality on or off. The fade is done by turning on the **
177-
** Timer0 interrupt. Timer 0 is used by the millis() function and is an 8-bit register with a clock divisor of 64 **
178-
** which triggers it to overflow at 976.5625Hz. The millis() function uses the TIMER0_OVF_vect so we can't use **
179-
** that, so we set the TIMER0_COMPA_vect set to 0x01 which trigger when the value is equal to 1. This gives us a **
180-
** pretty quick trigger rate which suffices to light and fade the LED lights **
182+
** function SetFadeRate() is called to turn the fade functionality on or off and adjust the rate at which the fade**
183+
** occurs. The fade is done by accessing the Timer0 interrupt, which is used by the millis() function and is an **
184+
** 8-bit register with a clock divisor of 64 which triggers it to overflow at a rate of 976.5625Hz, or roughly **
185+
** every millisecond. We set the TIMER0_COMPA_vect to 0x01 which triggers when the value is equal to 64. This **
186+
** then gives us an identical trigger speed but different trigger point to the millis() function which triggers **
187+
** when the Timer0 overflows. The same setup is done for the TIMER0_COMPB_vect but that is set to trigger halfway **
188+
** along the full range of 255 at 192, thus giving an interrupt rate of 2 times per milli second. The FadeSpeed **
189+
** equates to how many milliseconds ther are between incremental fades, the fastest is 1 which is every 1/2 milli-**
190+
** second, 2 is every millisecond, etc. Each time the trigger is reached all of the LED values which are not "off"**
191+
** are dimmed by 1/255 of the total value. A setting of 10 would fade the LEDs from full on to OFF 1/255 of their **
192+
** brightness in 1.28 seconds **
181193
*******************************************************************************************************************/
182-
void EncoderClass::SetFade(const bool FadeState) { // //
183-
_Fade = FadeState; // Set the private variable to value//
184-
if (FadeState) { // If turning on, set the ISR //
194+
void EncoderClass::SetFadeRate(const uint8_t FadeSpeed) { // //
195+
_FadeMillis = FadeSpeed; // Set the private variable to value//
196+
if (FadeSpeed) { // If turning on, set the ISR //
185197
cli(); // Disable interrupts //
186-
OCR0A = 0x01; // Comparison register setup to 1 //
198+
OCR0A = 0x40; // Comparison register A to 64 //
199+
OCR0B = 0xC0; // Comparison register B to 192 //
187200
TIMSK0 |= _BV(OCIE0A); // TIMER0_COMPA trigger on 0x01 //
201+
TIMSK0 |= _BV(OCIE0B); // TIMER0_COMPB trigger on 0x80 //
188202
sei(); // Enable interrupts //
189203
} else { // If turning off, unset the ISR //
190204
cli(); // Disable interrupts //
191205
TIMSK0 &= ~_BV(OCIE0A); // TIMER0_COMPA trigger off //
206+
TIMSK0 &= ~_BV(OCIE0B); // TIMER0_COMPB trigger off //
192207
sei(); // Enable interrupts //
193208
} // of if-then-else we need to turn fading on or off // //
194-
} // of method SetColor // //
209+
} // of method SetFadeRate // //

RotaryEncoder.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@
2626
** voltage for the red is 2.0V and the green/blue LEDs have 3.3V and with a 25mA current that equates to resistors**
2727
** at 120 and 68 Ohms respectively. **
2828
** **
29-
** The pushbutton has a pull-down resistor of 4.7KOhm to reduce bounce **
29+
** The pushbutton should have a pull-down resistor of 4.7KOhm to reduce bounce. The sample breadboard schematic **
30+
** used for the examples for this library can be located at in Github at the following address: **
31+
** https://github.com/SV-Zanshin/RotaryEncoder/blob/master/Images/RotaryEncoder.png **
32+
** **
33+
** Although programming for the Arduino and in c/c++ is new to me, I'm a professional programmer and have learned,**
34+
** over the years, that it is much easier to ignore superfluous comments than it is to decipher non-existent ones;**
35+
** so both my comments and variable names tend to be verbose. The code is written to fit in the first 80 spaces **
36+
** and the comments start after that and go to column 117 - allowing the code to be printed in A4 landscape mode. **
37+
** There are several parts of code which can be somewhat optimized, but in order to make the c++ code more under- **
38+
** standable by non-programmers some performance has been sacrificed for legibility and maintainability. **
3039
** **
3140
** This program is free software: you can redistribute it and/or modify it under the terms of the GNU General **
3241
** Public License as published by the Free Software Foundation, either version 3 of the License, or (at your **
@@ -37,6 +46,7 @@
3746
** **
3847
** Vers. Date Developer Comments **
3948
** ====== ========== =================== ======================================================================== **
49+
** 1.0.2 2016-12-18 [email protected] Changed SetFade() to SetFadeRate() function to alter the fade speed **
4050
** 1.0.1 2016-12-14 [email protected] Fixed error on condition to turn off LED lights. **
4151
** 1.0.0 2016-12-14 [email protected] Allowed defaults for LEDs on class constructer **
4252
** 1.0.b3 2016-12-13 [email protected] Made fading start only after the maximum setting was reached **
@@ -57,7 +67,7 @@
5767
static void TimerISR(); // Interim ISR calls real handler //
5868
void SetEncoderValue(const int16_t NewValue = 0); // Set the encoder value //
5969
void SetLEDState(const bool Status); // Turns encoder LEDs on or off //
60-
void SetFade(const bool FadeState); // Sets the fader state //
70+
void SetFadeRate(uint8_t FadeMillis); // Sets the fader state and speed //
6171
void SetColor(const uint8_t R, const uint8_t G, const uint8_t B);// Sets the LED colors //
6272
void SetPushButtonColor(const uint8_t R, const uint8_t G, // Sets the RGB values displayed //
6373
const uint8_t B); // when the pushbutton is pressed //
@@ -78,7 +88,7 @@
7888
uint8_t _RedPin; // //
7989
uint8_t _GreenPin; // //
8090
uint8_t _BluePin; // //
81-
bool _Fade = true; // Default to fade to dark //
91+
uint8_t _FadeMillis = 1; // 1=fast, 0=Off //
8292
bool _LEDOn = true; // Default to display LED lights //
8393
volatile bool _LEDChanged = true; // Set when rotate or click changes //
8494
volatile uint8_t _ButtonPresses = 0; // The current number of pushes //

0 commit comments

Comments
 (0)