Skip to content

Commit

Permalink
Code Changes
Browse files Browse the repository at this point in the history
Renamed files to correspond to library name, made the 3 LED pins
optional parameters to the constructor.
  • Loading branch information
Arnd authored and Arnd committed Dec 14, 2016
1 parent 2414ac6 commit 60c0ddd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
18 changes: 11 additions & 7 deletions Examples/StandardColors/StandardColors.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/*******************************************************************************************************************
** This program defines the RotaryEncoder class header. **
** This program defines the RotaryEncoder class header. It is published on GitHub and full description of the **
** class as well as the public function descriptions can be found at the GitHub wiki pages located at: **
** **
** https://github.com/SV-Zanshin/RotaryEncoder/wiki **
** **
** This program demonstrates the Rotary Encoder class which controls a commonly used rotary encoder with a clear **
** knob using 3 colored LEDs (Red, Green and Blue) along with a pushbutton. These are available from sources such **
Expand All @@ -24,17 +27,18 @@
** **
** Vers. Date Developer Comments **
** ====== ========== =================== ======================================================================== **
** 1.0.0 2016-12-14 [email protected] Changed include from "Encoder" to "RotaryEncoder", added comments **
** 1.0.0 2016-12-13 [email protected] Initial coding **
** **
*******************************************************************************************************************/
#include <Encoder.h> // Include Encoder library //
#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 PUSHBUTTON_PIN = 7; // Pin for pushbutton connector pin //
const uint8_t RED_PIN = 11; // Red LED PWM pin. This is grounded//
const uint8_t GREEN_PIN = 10; // Green LED PWM pin. Grounded //
const uint8_t BLUE_PIN = 9; // Blue LED PWM pin. Grounded //
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 //
//----------------------------------//
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 //
Expand All @@ -43,8 +47,8 @@ void setup() { //
Serial.begin(115200); // Initialize Serial I/O at speed //
delay(1000); // Wait 1 second for initialization //
Serial.println("Starting Encoder Program..."); // //
} // of method setup() //----------------------------------//

} // of method setup() // //
//----------------------------------//
void loop(){ // Main program infinite loop //
static int last = Encoder.GetEncoderValue(); // Store previous Encoder value //
uint8_t presses = Encoder.GetButton(); // See how often button was pressed //
Expand Down
16 changes: 10 additions & 6 deletions Encoder.cpp → RotaryEncoder.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************************************************
** This program defines the RotaryEncoder class. See the "Encoder.h" file for program documentation **
*******************************************************************************************************************/
#include "Encoder.h" // Include the header file //
#include "RotaryEncoder.h" // Include the header file //
EncoderClass* EncoderClass::ClassPtr; // Declare Class Reference pointer //
/*******************************************************************************************************************
** The class constructor stores the pin values as part of the initializer and then uses an indirect method to **
Expand All @@ -10,23 +10,27 @@ EncoderClass* EncoderClass::ClassPtr; //
** pointer to the class with an offset to the appropriate function to call the correct function. **
*******************************************************************************************************************/
EncoderClass::EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, // Class constructor //
const uint8_t PushbuttonPin, const uint8_t RedPin, // //
const uint8_t GreenPin, const uint8_t BluePin ) : // //
const uint8_t PushbuttonPin, // //
const uint8_t RedPin=255,const uint8_t GreenPin=255,// //
const uint8_t BluePin=255 ) : // //
_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 //
analogWrite(RedPin,255);analogWrite(GreenPin,255);analogWrite(BluePin,255); // Ensure LEDs are off at start //
if (RedPin!=255) analogWrite(RedPin,255); // If the LED pins are defined, //
if (GreenPin!=255) analogWrite(GreenPin,255); // then turn them off at the start //
if (BluePin!=255) analogWrite(BluePin,255); // //
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 //
_EncoderValue = 0; // Reset in case it was changed //
ClassPtr = this; // pointer to current instance //
ClassPtr = this; // pointer to current instance //
attachInterrupt(digitalPinToInterrupt(LeftPin),RotateISR,CHANGE); // Attach static internal function //
attachInterrupt(digitalPinToInterrupt(RightPin),RotateISR,CHANGE); // Attach static internal function //
attachInterrupt(digitalPinToInterrupt(PushbuttonPin),PushButtonISR,RISING); // Attach static internal function //
SetFade(true); // turn on fader and interrupt //
if (RedPin|GreenPin|BluePin==255) SetFade(false); // If no LEDs, turn off fader //
else SetFade(true); // turn on fader and interrupt //
} // of class constructor // //
ISR(TIMER0_COMPA_vect) {EncoderClass::TimerISR();} // Call the ISR every millisecond //
static void EncoderClass::PushButtonISR(){ClassPtr->PushButtonHandler();} // Redirect to real handler function//
Expand Down
9 changes: 5 additions & 4 deletions Encoder.h → RotaryEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@
** **
** Vers. Date Developer Comments **
** ====== ========== =================== ======================================================================== **
** 1.0.1 2016-12-14 [email protected] Allowed defaults for LEDs on class constructer **
** 1.0.b3 2016-12-13 [email protected] Made fading start only after the maximum setting was reached **
** 1.0.b2 2016-12-12 [email protected] Fixed interrupt calls **
** 1.0.b1 2016-12-04 [email protected] Initial coding **
** **
*******************************************************************************************************************/
#include "Arduino.h" // Arduino data type definitions //
#ifndef Encoder_h // Guard code definition //
#define Encoder_h // Define the name inside guard code//
#ifndef RotaryEncoder_h // Guard code definition //
#define RotaryEncoder_h // Define the name inside guard code//
class EncoderClass { //----------------------------------//
public: // Publicly visible class members //
EncoderClass(const uint8_t LeftPin, const uint8_t RightPin, // Class constructor //
const uint8_t PushbuttonPin, const uint8_t RedPin, // //
const uint8_t GreenPin,const uint8_t BluePin); // //
const uint8_t PushbuttonPin, const uint8_t RedPin=255, // //
const uint8_t GreenPin=255,const uint8_t BluePin=255); // //
uint8_t GetButton(); // Returns number of button pushes //
int16_t GetEncoderValue(); // Return the encoder value //
void SetEncoderValue(const int16_t NewValue = 0); // Set the encoder value //
Expand Down

0 comments on commit 60c0ddd

Please sign in to comment.