You are on page 1of 5

#include <LiquidCrystal.

h>
#include <Encoder.h>
//LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
LiquidCrystal lcd(11, 10, 5, 4, 3, 2); // select the pins used on the LCD panel

int lcd_key = 0;
int adc_key_in = 0;
const int pinA = 7;
const int pinB = 8;
long lastPosition = 0;
Encoder encoder(pinA, pinB);

#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5

#define POTENTIOMETER_PIN A1

float gain = 1.0;


float offset = 0.0;
float maxValue = -INFINITY;
float minValue = INFINITY;

int mode = 1;

int read_LCD_buttons(){
adc_key_in = analogRead(0); // read the value from the sensor

if (adc_key_in > 1000) return btnNONE;


// For V1.1 use this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;

return btnNONE;
}

void setup(){
lcd.begin(16, 2);
lcd.clear();
}

void loop(){

lcd_key = read_LCD_buttons(); // read the buttons


switch (lcd_key)
{
case btnRIGHT:{
if(mode==4)
{
maxValue = -INFINITY;
minValue = INFINITY;
delay(100);
lcd.clear();
break;
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("NO USE");
delay(100);
lcd.clear();
break;
}
case btnLEFT:{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("NO USE");
delay(100);
lcd.clear();
break;
}
case btnUP:{
lcd.clear();
if(mode==2)
{
delay(200);
gain=gain+1.0 ;
delay(100);
if (gain >=99.9)
gain=99.9;

}
if(mode==3)
{
delay(200);
offset=offset+0.1 ;
delay(100);
if(offset >= 2.5)
offset=2.5;

}
break;
}
case btnDOWN:{
lcd.clear();
if(mode==2)
{
delay(200);
gain=gain-1.0 ;
delay(100);
if (gain <=0.01)
gain=0.01;

}
if(mode==3)
{
delay(200);
offset=offset-0.1 ;
delay(100);
if(offset <= -2.5)
offset=-2.5;

}
break;
}
case btnSELECT:{
lcd.clear();
if (mode==4)
mode=1;
else
mode=mode+1;
delay(500);
break;
}
case btnNONE:{
if(mode==1)
{

lcd.setCursor(0,0);
lcd.print("Mode:");
lcd.print(mode);

int analogValue = analogRead(POTENTIOMETER_PIN);


float voltage = analogValue * (5.0 / 1023.0);
float measured= (gain*voltage)+offset;
if(measured>=maxValue)
maxValue=measured;
if(measured<=minValue)
minValue=measured;
lcd.setCursor(10,0);
lcd.print(voltage);
lcd.setCursor(0,1);
lcd.print("equation=");
lcd.print(measured);
}
if(mode==2)
{

lcd.setCursor(0,0);
lcd.print("Mode:");
lcd.print(mode);

lcd.setCursor(0,1);
lcd.print("gain=");
lcd.print(gain);
}
if (mode==3)
{
lcd.setCursor(0,0);
lcd.print("Mode:");
lcd.print(mode);

lcd.setCursor(0,1);
lcd.print("offset=");
lcd.print(offset);
}
if(mode==4)
{
lcd.setCursor(0,0);
lcd.print("Mode:");
lcd.print(mode);

lcd.setCursor(7,0);
lcd.print("max=");
lcd.print(maxValue);
lcd.setCursor(7,1);
lcd.print("min=");
lcd.print(minValue);
}
if (mode==2)
{
long newPosition = encoder.read();

if (newPosition != lastPosition) {

if (newPosition > lastPosition) {


gain += 0.5;
delay(10);
if (gain >=99.9)
gain=99.9;
} else {
gain -= 0.5;
delay(10);
if (gain <=0.01)
gain=0.01;
}

lastPosition = newPosition;

}
}
if(mode==3)
{
long newPosition = encoder.read();

if (newPosition != lastPosition) {

if (newPosition > lastPosition) {


offset += 0.05;
delay(10);
if (offset >=2.5)
offset=2.5;
} else {
offset -= 0.05;
delay(10);
if (offset <=-2.5)
offset=-2.5;
}

lastPosition = newPosition;
}
}

break;
}
}

You might also like