/* Arduino New Rotary Encoder Debounce
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
// Rotary Encoder Module connections
const int PinSW=3; // Rotary Encoder Switch
const int PinDT=4; // DATA signal
const int PinCLK=2; // CLOCK signal
// Variables to debounce Rotary Encoder
long TimeOfLastDebounce = 0;
int DelayofDebounce = 20.00;
// Store previous Pins state
int PreviousCLK;
int PreviousDATA;
int displaycounter=0; // Store current counter value
// Library used for LED MATRIX 8x8
//#include <MD_Parola.h>
//#include <MD_MAX72xx.h>
//#include <SPI.h>
//#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
/* PAROLA_HW, ///< Use the Parola style hardware modules.
GENERIC_HW, ///< Use 'generic' style hardware modules commonly available.
ICSTATION_HW, ///< Use ICStation style hardware module.
FC16_HW ///< Use FC-16 style hardware module.
*/
// 8x8 LED Matrix connections
//#define MAX_DEVICES 2
//#define CLK_PIN 13
//#define DATA_PIN 11
//#define CS_PIN 10
// Hardware SPI connection
//MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
void setup() {
// Put current pins state in variables
Serial.begin(9600);
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
// Set the Switch pin to use Arduino PULLUP resistors
pinMode(PinSW, INPUT_PULLUP);
// Start and setup the LED MATRIX at startup
//P.begin();
//P.setTextAlignment(PA_RIGHT);
//P.print(displaycounter);
}
void loop() {
// If enough time has passed check the rotary encoder
if ((millis() - TimeOfLastDebounce) > DelayofDebounce) {
check_rotary(); // Rotary Encoder check routine below
PreviousCLK=digitalRead(PinCLK);
PreviousDATA=digitalRead(PinDT);
TimeOfLastDebounce=millis(); // Set variable to current millis() timer
}
// Check if Rotary Encoder switch was pressed
if (digitalRead(PinSW) == LOW) {
displaycounter=0; // Reset counter to zero
//P.print(displaycounter);
}
}
// Check if Rotary Encoder was moved
void check_rotary() {
if ((PreviousCLK == 0) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
displaycounter++;
//P.print(displaycounter);
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
displaycounter--;
//P.print(displaycounter);
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
displaycounter++;
//P.print(displaycounter);
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
displaycounter--;
//P.print(displaycounter);
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 1) && (PreviousDATA == 1)) {
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 1)) {
displaycounter++;
//P.print(displaycounter);
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 0) && (digitalRead(PinDT) == 0)) {
displaycounter--;
//P.print(displaycounter);
Serial.println(displaycounter);
}
}
if ((PreviousCLK == 0) && (PreviousDATA == 0)) {
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 0)) {
displaycounter++;
//P.print(displaycounter);
Serial.println(displaycounter);
}
if ((digitalRead(PinCLK) == 1) && (digitalRead(PinDT) == 1)) {
displaycounter--;
// P.print(displaycounter);
Serial.println(displaycounter);
}
}
}