#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define outputA 4
#define outputB 5
int S0 = 7;
int S1 = 8;
int S2 = 9;
int S3 = 10;
int muxCh = 0;
int dutyCode = 0;
int aState;
int aLastState;
int outPwm = 9; // the PWM pin the motor is attached
int menu1 = 1;
int menu2 = 1;
int outputBstate;
int outputBLaststate;
int statusoutputB;
int laststatusoutputB;
int count1 = 0;
float convertion = 0;
void setup() {
pinMode (outputA,INPUT);
pinMode (outputB,INPUT);
pinMode (outPwm, OUTPUT);
pinMode (S0, OUTPUT);
pinMode (S1, OUTPUT);
pinMode (S2, OUTPUT);
pinMode (S3, OUTPUT);
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
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);
}
analogWrite(outPwm, dutyCode);
lcd.setCursor(0,0);
int DTC = (dutyCode*100)/255;
lcd.print(DTC);
lcd.setCursor(3,0);
lcd.print("%");
if (DTC < 10) {
lcd.setCursor(1,0);
lcd.print(" ");
}
if (DTC < 100) {
lcd.setCursor(2,0);
lcd.print(" ");
}
Serial.print("dutyCode: ");
Serial.println(dutyCode);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
}