Skip to content

Commit

Permalink
Added HW debounce
Browse files Browse the repository at this point in the history
When HW debounce is used the internal pullup resistors should be turned off. This option added to the instantiation.
  • Loading branch information
SV-Zanshin committed Oct 5, 2017
1 parent edee4f6 commit 7a6f8d8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
17 changes: 14 additions & 3 deletions Examples/StandardColors/StandardColors.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,41 @@
** **
** Vers. Date Developer Comments **
** ====== ========== ============================= ============================================================== **
** 1.0.2 2017-10-03 https://github.com/SV-Zanshin Added optional HW-Debounce to instantiation call **
** 1.0.1 2016-12-21 https://github.com/SV-Zanshin Added extra output and program code **
** 1.0.0 2016-12-14 https://github.com/SV-Zanshin Changed include name "RotaryEncoder", added more comments **
** 1.0.0b 2016-12-13 https://github.com/SV-Zanshin Initial coding **
** **
*******************************************************************************************************************/
#include <RotaryEncoder.h> // Include Encoder library //
//----------------------------------//

/* //----------------------------------//
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 //
const uint8_t BLUE_PIN = 9; // Blue LED PWM pin. Ground = FULL //
*/
const uint8_t ROTARY_PIN_1 = 6; // Pin for left rotary encoder pin //
const uint8_t ROTARY_PIN_2 = 7; // Pin for right rotary encoder pin //
const uint8_t PUSHBUTTON_PIN = 2; // Pin for pushbutton connector pin //
const uint8_t RED_PIN = 30; // Red LED PWM pin. Ground = FULL //
const uint8_t GREEN_PIN = 8; // Green LED PWM pin. Ground = FULL //
const uint8_t BLUE_PIN = 9; // Blue LED PWM pin. Ground = FULL //
//----------------------------------//
EncoderClass Encoder(ROTARY_PIN_1, ROTARY_PIN_2, PUSHBUTTON_PIN, // Instantiate class defining all //
RED_PIN, GREEN_PIN, BLUE_PIN); // of the pins that are used //
RED_PIN, GREEN_PIN, BLUE_PIN, true); // of the pins that are used //
// Using HW debounce, internal pull-//
// ups disabled //
//----------------------------------//
void setup() { // Start One-Time run section //
Serial.println(F("Starting Encoder Program...")); // //
Encoder.SetFadeRate(0); // Turn off fading //
Encoder.SetColor(0,0,0); // Set LED full on, allow to fade //
Serial.begin(115200); // Initialize Serial I/O at speed //
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,")); // //
Expand Down
9 changes: 6 additions & 3 deletions RotaryEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ EncoderClass* EncoderClass::ClassPtr; //
EncoderClass::EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, // Class constructor //
const uint8_t PushbuttonPin, // //
const uint8_t RedPin=255,const uint8_t GreenPin=255,// //
const uint8_t BluePin=255 ) : // //
const uint8_t BluePin=255, // //
const bool HWDebounce=false) : // //
_LeftPin(LeftPin), _RightPin(RightPin), _PushbuttonPin(PushbuttonPin), // //
_RedPin(RedPin), _GreenPin(GreenPin), _BluePin(BluePin) { // //
pinMode(RedPin,OUTPUT); pinMode(GreenPin,OUTPUT); pinMode(BluePin,OUTPUT); // Set LED color pins to output //
Expand All @@ -22,8 +23,10 @@ EncoderClass::EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, //
pinMode(LeftPin, INPUT); // Define encoder pins as input //
pinMode(RightPin, INPUT); // Define encoder pins as input //
pinMode(PushbuttonPin, INPUT); // Define pushbutton pin as input //
digitalWrite(LeftPin, HIGH); // Turn the pull-up resistor on //
digitalWrite(RightPin, HIGH); // Turn the pull-up resistor on //
if (!HWDebounce) { // If SW debounce then enable pullup//
digitalWrite(LeftPin, HIGH); // Turn the pull-up resistor on //
digitalWrite(RightPin, HIGH); // Turn the pull-up resistor on //
} // of if-then hardware or software debounce // //
_EncoderValue = 0; // Reset in case it was changed //
ClassPtr = this; // pointer to current instance //
attachInterrupt(digitalPinToInterrupt(LeftPin),RotateISR,CHANGE); // Attach static internal function //
Expand Down
4 changes: 3 additions & 1 deletion RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
** **
** Vers. Date Developer Comments **
** ====== ========== ============================= ============================================================== **
** 1.0.4 2017-10-03 https://github.com/SV-Zanshin Added optional hardware debounce switch on instantiation **
** 1.0.3 2016-12-21 https://github.com/SV-Zanshin Corrected volatile variables and fixed SetColor() call **
** 1.0.2 2016-12-18 https://github.com/SV-Zanshin Changed SetFade() to SetFadeRate() function to alter fade speed**
** 1.0.1 2016-12-14 https://github.com/SV-Zanshin Fixed error on condition to turn off LED lights. **
Expand All @@ -62,7 +63,8 @@
public: // Publicly visible class members //
EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, // Class constructor //
const uint8_t PushbuttonPin, const uint8_t RedPin=255, // //
const uint8_t GreenPin=255,const uint8_t BluePin=255); // //
const uint8_t GreenPin=255,const uint8_t BluePin=255, // //
const bool HWDebounce=false); // //
uint8_t GetButton(); // Returns number of button pushes //
int16_t GetEncoderValue(); // Return the encoder value //
static void TimerISR(); // Interim ISR calls real handler //
Expand Down

0 comments on commit 7a6f8d8

Please sign in to comment.