#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
uint8_t band_encoder_CLK = 2;
uint8_t band_encoder_DT = 4;
uint8_t band_encoder_SW = 5;
uint8_t c_encoder_CLK = 3;
uint8_t c_encoder_DT = 6;
uint8_t c_encoder_SW = 7;
uint8_t band_counter = 0;
uint8_t c_counter = 0;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize encoder pins
pinMode(band_encoder_CLK, INPUT);
pinMode(band_encoder_DT, INPUT);
pinMode(band_encoder_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(band_encoder_CLK), readBandEncoder, FALLING);
pinMode(c_encoder_CLK, INPUT);
pinMode(c_encoder_DT, INPUT);
pinMode(c_encoder_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(c_encoder_CLK), readCEncoder, FALLING);
}
void readBandEncoder() {
int dtValue = digitalRead(band_encoder_DT);
if (dtValue == HIGH) {
if (band_counter < 9){
band_counter++;
}
}
if (dtValue == LOW) {
if (band_counter > 0){
band_counter--;
}
}
}
void readCEncoder() {
int dtValue = digitalRead(c_encoder_DT);
if (dtValue == HIGH) {
if (c_counter < 127){
c_counter++;
}
}
if (dtValue == LOW) {
if (c_counter > 0){
c_counter--;
}
}
}
// Get the counter value, disabling interrupts.
// This make sure readEncoder() doesn't change the value
// while we're reading it.
int getBandCounter() {
int result;
noInterrupts();
result = band_counter;
interrupts();
return result;
}
int getCCounter() {
int result;
noInterrupts();
result = c_counter;
interrupts();
return result;
}
void resetBandCounter() {
noInterrupts();
band_counter = 0;
interrupts();
}
void resetCCounter() {
noInterrupts();
c_counter = 0;
interrupts();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print(' ');
lcd.setCursor(0, 0);
lcd.print('Band : ' +getBandCounter());
lcd.setCursor(0, 1);
lcd.print(' ');
lcd.setCursor(0, 1);
lcd.print('C : '+getCCounter());
if (digitalRead(band_encoder_SW) == LOW) {
resetBandCounter();
}
if (digitalRead(c_encoder_SW) == LOW) {
resetCCounter();
}
}