Skip to content

Commit 60c0ddd

Browse files
ArndArnd
Arnd
authored and
Arnd
committed
Code Changes
Renamed files to correspond to library name, made the 3 LED pins optional parameters to the constructor.
1 parent 2414ac6 commit 60c0ddd

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

Examples/StandardColors/StandardColors.ino

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/*******************************************************************************************************************
2-
** This program defines the RotaryEncoder class header. **
2+
** This program defines the RotaryEncoder class header. It is published on GitHub and full description of the **
3+
** class as well as the public function descriptions can be found at the GitHub wiki pages located at: **
4+
** **
5+
** https://github.com/SV-Zanshin/RotaryEncoder/wiki **
36
** **
47
** This program demonstrates the Rotary Encoder class which controls a commonly used rotary encoder with a clear **
58
** knob using 3 colored LEDs (Red, Green and Blue) along with a pushbutton. These are available from sources such **
@@ -24,17 +27,18 @@
2427
** **
2528
** Vers. Date Developer Comments **
2629
** ====== ========== =================== ======================================================================== **
30+
** 1.0.0 2016-12-14 [email protected] Changed include from "Encoder" to "RotaryEncoder", added comments **
2731
** 1.0.0 2016-12-13 [email protected] Initial coding **
2832
** **
2933
*******************************************************************************************************************/
30-
#include <Encoder.h> // Include Encoder library //
34+
#include <RotaryEncoder.h> // Include Encoder library //
3135
//----------------------------------//
3236
const uint8_t ROTARY_PIN_1 = 2; // Pin for left rotary encoder pin //
3337
const uint8_t ROTARY_PIN_2 = 3; // Pin for right rotary encoder pin //
3438
const uint8_t PUSHBUTTON_PIN = 7; // Pin for pushbutton connector pin //
35-
const uint8_t RED_PIN = 11; // Red LED PWM pin. This is grounded//
36-
const uint8_t GREEN_PIN = 10; // Green LED PWM pin. Grounded //
37-
const uint8_t BLUE_PIN = 9; // Blue LED PWM pin. Grounded //
39+
const uint8_t RED_PIN = 11; // Red LED PWM pin. Ground = FULL //
40+
const uint8_t GREEN_PIN = 10; // Green LED PWM pin. Ground = FULL //
41+
const uint8_t BLUE_PIN = 9; // Blue LED PWM pin. Ground = FULL //
3842
//----------------------------------//
3943
EncoderClass Encoder(ROTARY_PIN_1, ROTARY_PIN_2, PUSHBUTTON_PIN, // Instantiate class defining all //
4044
RED_PIN, GREEN_PIN, BLUE_PIN); // of the pins that are used //
@@ -43,8 +47,8 @@ void setup() { //
4347
Serial.begin(115200); // Initialize Serial I/O at speed //
4448
delay(1000); // Wait 1 second for initialization //
4549
Serial.println("Starting Encoder Program..."); // //
46-
} // of method setup() //----------------------------------//
47-
50+
} // of method setup() // //
51+
//----------------------------------//
4852
void loop(){ // Main program infinite loop //
4953
static int last = Encoder.GetEncoderValue(); // Store previous Encoder value //
5054
uint8_t presses = Encoder.GetButton(); // See how often button was pressed //

Encoder.cpp renamed to RotaryEncoder.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************************************************************
22
** This program defines the RotaryEncoder class. See the "Encoder.h" file for program documentation **
33
*******************************************************************************************************************/
4-
#include "Encoder.h" // Include the header file //
4+
#include "RotaryEncoder.h" // Include the header file //
55
EncoderClass* EncoderClass::ClassPtr; // Declare Class Reference pointer //
66
/*******************************************************************************************************************
77
** The class constructor stores the pin values as part of the initializer and then uses an indirect method to **
@@ -10,23 +10,27 @@ EncoderClass* EncoderClass::ClassPtr; //
1010
** pointer to the class with an offset to the appropriate function to call the correct function. **
1111
*******************************************************************************************************************/
1212
EncoderClass::EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, // Class constructor //
13-
const uint8_t PushbuttonPin, const uint8_t RedPin, // //
14-
const uint8_t GreenPin, const uint8_t BluePin ) : // //
13+
const uint8_t PushbuttonPin, // //
14+
const uint8_t RedPin=255,const uint8_t GreenPin=255,// //
15+
const uint8_t BluePin=255 ) : // //
1516
_LeftPin(LeftPin), _RightPin(RightPin), _PushbuttonPin(PushbuttonPin), // //
1617
_RedPin(RedPin), _GreenPin(GreenPin), _BluePin(BluePin) { // //
1718
pinMode(RedPin,OUTPUT); pinMode(GreenPin,OUTPUT); pinMode(BluePin,OUTPUT); // Set LED color pins to output //
18-
analogWrite(RedPin,255);analogWrite(GreenPin,255);analogWrite(BluePin,255); // Ensure LEDs are off at start //
19+
if (RedPin!=255) analogWrite(RedPin,255); // If the LED pins are defined, //
20+
if (GreenPin!=255) analogWrite(GreenPin,255); // then turn them off at the start //
21+
if (BluePin!=255) analogWrite(BluePin,255); // //
1922
pinMode(LeftPin, INPUT); // Define encoder pins as input //
2023
pinMode(RightPin, INPUT); // Define encoder pins as input //
2124
pinMode(PushbuttonPin, INPUT); // Define pushbutton pin as input //
2225
digitalWrite(LeftPin, HIGH); // Turn the pull-up resistor on //
2326
digitalWrite(RightPin, HIGH); // Turn the pull-up resistor on //
2427
_EncoderValue = 0; // Reset in case it was changed //
25-
ClassPtr = this; // pointer to current instance //
28+
ClassPtr = this; // pointer to current instance //
2629
attachInterrupt(digitalPinToInterrupt(LeftPin),RotateISR,CHANGE); // Attach static internal function //
2730
attachInterrupt(digitalPinToInterrupt(RightPin),RotateISR,CHANGE); // Attach static internal function //
2831
attachInterrupt(digitalPinToInterrupt(PushbuttonPin),PushButtonISR,RISING); // Attach static internal function //
29-
SetFade(true); // turn on fader and interrupt //
32+
if (RedPin|GreenPin|BluePin==255) SetFade(false); // If no LEDs, turn off fader //
33+
else SetFade(true); // turn on fader and interrupt //
3034
} // of class constructor // //
3135
ISR(TIMER0_COMPA_vect) {EncoderClass::TimerISR();} // Call the ISR every millisecond //
3236
static void EncoderClass::PushButtonISR(){ClassPtr->PushButtonHandler();} // Redirect to real handler function//

Encoder.h renamed to RotaryEncoder.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,20 @@
3737
** **
3838
** Vers. Date Developer Comments **
3939
** ====== ========== =================== ======================================================================== **
40+
** 1.0.1 2016-12-14 [email protected] Allowed defaults for LEDs on class constructer **
4041
** 1.0.b3 2016-12-13 [email protected] Made fading start only after the maximum setting was reached **
4142
** 1.0.b2 2016-12-12 [email protected] Fixed interrupt calls **
4243
** 1.0.b1 2016-12-04 [email protected] Initial coding **
4344
** **
4445
*******************************************************************************************************************/
4546
#include "Arduino.h" // Arduino data type definitions //
46-
#ifndef Encoder_h // Guard code definition //
47-
#define Encoder_h // Define the name inside guard code//
47+
#ifndef RotaryEncoder_h // Guard code definition //
48+
#define RotaryEncoder_h // Define the name inside guard code//
4849
class EncoderClass { //----------------------------------//
4950
public: // Publicly visible class members //
5051
EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, // Class constructor //
51-
const uint8_t PushbuttonPin, const uint8_t RedPin, // //
52-
const uint8_t GreenPin,const uint8_t BluePin); // //
52+
const uint8_t PushbuttonPin, const uint8_t RedPin=255, // //
53+
const uint8_t GreenPin=255,const uint8_t BluePin=255); // //
5354
uint8_t GetButton(); // Returns number of button pushes //
5455
int16_t GetEncoderValue(); // Return the encoder value //
5556
void SetEncoderValue(const int16_t NewValue = 0); // Set the encoder value //

0 commit comments

Comments
 (0)