#include <SimpleRotary.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
SimpleRotary rotary(6,5,7);
#define POT A0
bool lcd_on = false;
bool backLight_on = false;
bool valueIsSet = false;
bool stopCount = false;
const int backLightTimeOut = 15000;
const int LCDLightTimeOut = 30000;
unsigned long LastInputTime = 0;
int NOW_POT_VAL=0;
int OLD_POT_VAL=9999;
float MAPPED_W2V=0.0;
float MAPPED_W2mA=0.0;
int j=0;
int old_j=9999;
unsigned int multiplyer=1;
int max_val=12500;
int min_val=0;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DEMO");
lcd.setCursor(6,0);
lcd.print("STEP: ");
pinMode(POT, INPUT);
}
void loop() {
NOW_POT_VAL=analogRead(POT);
if (NOW_POT_VAL != OLD_POT_VAL){
OLD_POT_VAL=NOW_POT_VAL;
multiplyer=map(NOW_POT_VAL, 0, 1023, 1, 1000);
reset_turnOn_LCDTimer();
upDateLCD_POT();
SerialPrint();
}
byte r;
byte p;
// 0 = not turning, 1 = CW, 2 = CCW
r = rotary.rotate();
p = rotary.push();
if ( r == 1 ) {
Serial.println("CW");
j=j+multiplyer;
if (j>max_val){
j=max_val;
}
}
if ( r == 2 ) {
Serial.println("CCW");
j=j-multiplyer;
if (j<min_val){
j=min_val;
}
}
if(j != old_j){
old_j=j;
MAPPED_W2V = mapF(j, 0.0, 12500.0, 0.00, 10.00);
MAPPED_W2mA = mapF(j, 0.0, 12500.0, 4.00, 20.00);
reset_turnOn_LCDTimer();
upDateLCD_W();
SerialPrint();
upDateLCD_lock(false);
}
if (p == 1){
Serial.println("Value is set");
reset_turnOn_LCDTimer();
upDateLCD_lock(true);
}
if(!stopCount){
turnOff_bLight_LCD();
}
}
float mapF(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void upDateLCD_POT(){
lcd.setCursor(12,0);
lcd.print(" ");
lcd.setCursor(12,0);
lcd.print(multiplyer);
}
void upDateLCD_W(){
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(j);
}
void upDateLCD_lock(bool sw_ch){
switch (sw_ch){
case false:
lcd.setCursor(13,1);
lcd.print(" ");
valueIsSet=false;
break;
case true:
lcd.setCursor(13,1);
lcd.print("SET");
valueIsSet=true;
break;
}
}
void SerialPrint(){
Serial.print(multiplyer);
Serial.print(" ");
Serial.print(j);
Serial.print(" ");
Serial.print(MAPPED_W2V,2);
Serial.print(" ");
Serial.println(MAPPED_W2mA,2);
}
void reset_turnOn_LCDTimer(){
LastInputTime = millis();
if(lcd_on==false){
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("DEMO");
lcd.setCursor(6,0);
lcd.print("STEP: ");
upDateLCD_POT();
upDateLCD_W();
upDateLCD_lock(valueIsSet);
}
if(lcd_on==true && backLight_on==false){
lcd.backlight();
}
lcd_on = true;
backLight_on = true;
stopCount=false;
}
void turnOff_bLight_LCD(){
unsigned long nowTime = millis()-LastInputTime;
if((backLight_on==true)&&(nowTime>=backLightTimeOut)){
lcd.noBacklight();
backLight_on=false;
}
if((lcd_on==true)&&(nowTime>=LCDLightTimeOut)){
lcd.clear();
lcd_on=false;
stopCount = true;
}else if((lcd_on==true)&&(nowTime<LCDLightTimeOut)){
if(nowTime%1000==0){
Serial.print("Elapsed: ");
Serial.println(nowTime/1000);
}
}
}