/* INDUSTRIAL INTERNET OF THINGS | PRAKTEK LCD ENCODER | KELAS XI | MEKATRONIKA | SMKN 2 CIMAHI | FAUZI NUGROHO */
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int counter = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27 for a 16 chars and 2 line display
#define PIN_A 2 // CLK
#define PIN_B 4 // DT
#define PIN_BUTTON 5 // SW
#define DEBONCE_TO 150
volatile bool putarCCW = false;
volatile bool putarCW = false;
unsigned long debounceTime = 0;
bool dataCW = false;
bool dataCCW = false;
void checkEncoder() {
if ((!putarCCW)&&(!putarCW)) {
int pinA = digitalRead(PIN_A);
delayMicroseconds(1500);
int pinB = digitalRead(PIN_B);
if (pinA == pinB){
if (dataCW) {
putarCCW = true;
} else {
putarCW = true;
}
} else {
if (dataCCW) {
putarCW = true;
} else {
putarCCW = true;
}
}
}
}
void setup() {
Serial.begin(115200);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.clear(); // Clear the LCD screen
pinMode(PIN_A, INPUT_PULLUP);
pinMode(PIN_B, INPUT_PULLUP);
pinMode(PIN_BUTTON, INPUT_PULLUP);
attachInterrupt(PIN_B, checkEncoder, CHANGE);
Serial.println("Siap membaca data encoder: ");
lcd.clear();
lcd.setCursor(0, 0); // Set the cursor to the first column and first row
lcd.print(" SIAP PUTAR "); // Print some text
lcd.setCursor(0,1);
lcd.print("KANAN ATAU KIRI!");
}
void loop() {
static int value = 0;
if (!digitalRead(PIN_BUTTON)) {
value = 0;
Serial.print("Reset: ");
Serial.println(value);
lcd.clear();
lcd.setCursor(0, 0); // Set the cursor to the first column and first row
lcd.print(" DATA DIRESET ! "); // Print some text
lcd.setCursor(0,1);
lcd.print(value);
}
if (putarCW) {
value++;
Serial.print("Memutar ke Arah Jarum jam / CW: ");
Serial.println(value);
lcd.clear();
lcd.setCursor(0, 0); // Set the cursor to the first column and first row
lcd.print(">>>> JARUM JAM !"); // Print some text
lcd.setCursor(0,1);
lcd.print(value);
putarCW = false;
dataCCW = true;
debounceTime = millis();
}
if (putarCCW) {
value--;
Serial.print("Memutar ke Arah Kebalikan Jarum Jam / CCW: ");
Serial.println(value);
lcd.clear();
lcd.setCursor(0, 0); // Set the cursor to the first column and first row
lcd.print("<<<< JARUM JAM !"); // Print some text
lcd.setCursor(0,1);
lcd.print(value);
putarCCW = false;
dataCW = true;
debounceTime = millis();
}
if ((millis()-debounceTime) > DEBONCE_TO) {
dataCW = false;
dataCCW = false;
}
}