#include <LiquidCrystal_I2C.h>
#include <Arduino.h>
// -----------------------------------------------------------------------------
// constants
const int PinCLK = 2; // Used for generating interrupts using CLK signal
const int PinDT = 4; // Used for reading DT signal
const int PinSW = 8; // Used for the push button switch
volatile int virtualPosition = 0;
#define outputA 2
#define outputB 4
LiquidCrystal_I2C lcd(0x27, 20, 4);
int dutyCode = 0;
int outPwm = 11;
int aState;
int aLastState;
int dutypercen = 0;
//MULTIPLEXER
int s0 = 7;
int s1 = 8;
int s2 = 9;
int s3 = 10;
void isr () {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
// If interrupts come faster than 5ms, assume it's a bounce and ignore
if (interruptTime - lastInterruptTime > 5) {
if (!digitalRead(PinDT))
virtualPosition = (virtualPosition + 1);
else
virtualPosition = virtualPosition - 1;
}
lastInterruptTime = interruptTime;
} // ISR
void setup() {
Serial.begin(9600);
pinMode(PinCLK,INPUT);
pinMode(PinDT, INPUT);
pinMode(PinSW, INPUT);
attachInterrupt(0, isr, FALLING); // interrupt 0 is always connected to pin 2 on Arduino UNO
Serial.println("Start");
pinMode(outPwm, OUTPUT);
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0); // Mengatur kursor di baris pertama
lcd.print("LAB ELDA KEL.5");
delay(3000);
lcd.clear();
lcd.setCursor(0, 0); // Mengatur kursor di baris pertama
lcd.print("DutyPercen:");
lcd.setCursor(0, 1); // Mengatur kursor di baris pertama
lcd.print("DutyCycle:");
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
//PMOS
digitalWrite(s0, LOW);
digitalWrite(s1, HIGH);
digitalWrite(s2, HIGH);
digitalWrite(s3, HIGH);
}
void loop() {
int lastCount = 0;
while (true) {
if (!(digitalRead(PinSW))) { // check if pushbutton is pressed
virtualPosition = 0; // if YES, then reset counter to ZERO
while (!digitalRead(PinSW)) {} // wait til switch is released
delay(10); // debounce
Serial.println("Reset"); // Using the word RESET instead of COUNT here to find out a buggy encoder
}
if (virtualPosition != lastCount) {
lastCount = virtualPosition;
Serial.print("Count = ");
Serial.println(virtualPosition);
}
} // while
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
dutyCode ++;
} else {
dutyCode --;
}
if(dutyCode < 0) {dutyCode = 0;}
if(dutyCode > 255) {dutyCode = 255;}
dutypercen = (dutyCode * 100 ) / 255;
analogWrite(outPwm, dutyCode);
Serial.print("Position: ");
Serial.print(dutypercen);
Serial.print(" ");
Serial.println(dutyCode);
lcd.setCursor(11 , 0);
lcd.print(dutypercen);
lcd.print("% ");
lcd.setCursor(10 , 1);
lcd.print(dutyCode);
lcd.print(" ");
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}