/*
SMETER FT897/857/817 by IU3BRB Alberto
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int analogInput = A0; //analog pin A0
float VoltageOut = 0.0;
float VoltageIn = 0.0;
int refresh = 10; //refresh rate
int i = 0; //variable used for clearing our byte 'graph'
int value = 0; //variable to store value
byte graph1[8] =
{
B00000,
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B00000,
};
byte graph2[8] =
{
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
B00000,
};
byte graph3[8] =
{
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
B11111,
B00000,
};
byte graph4[8] =
{
B00000,
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
B00000,
};
byte graph5[8] =
{
B00000,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B00000,
};
byte graph6[8] =
{
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B00000,
};
byte graph0[8] =
{
B00000,
B01010,
B01010,
B00000,
B10001,
B01110,
B00000,
B00000,
};
void setup()
{
//lcd.begin(16,2);
lcd.init();
lcd.backlight();
pinMode(analogInput, INPUT); //setting analog pin mode to input
lcd.createChar(1, graph1);
lcd.createChar(2, graph2);
lcd.createChar(3, graph3);
lcd.createChar(4, graph4);
lcd.createChar(5, graph5);
lcd.createChar(6, graph6);
lcd.createChar(0, graph0);
lcd.begin(16, 2); //setting up the LCD screens rows and colums
lcd.print(" IU3BRB FT897/D");
lcd.setCursor(0, 1);
lcd.print("S");
}
void loop(){
value = analogRead(analogInput); //reading the value on A1
value = map(value, 0, 1023, 0, 11);
//VoltageOut = (value * 4.84) / 1024.0;
VoltageIn = (value * 4.84) /11;
//VoltageOut / (R2/(R1+R2));
lcd.setCursor(13, 2); //printing the result to the LCD display
lcd.print(value);
delay(refresh); //refreshing the screen
voltmetergraph(); //calling my function voltmetergraph
}
void voltmetergraph()
{
if (VoltageIn >= 0.417){lcd.setCursor(1, 1);lcd.print("_");}else {LcdClearByte(1);}
if (VoltageIn >= 0.834){lcd.setCursor(2, 1);lcd.print((char)1);}else {LcdClearByte(2);}
if (VoltageIn >= 1.251){lcd.setCursor(3, 1);lcd.print((char)1);}else {LcdClearByte(3);}
if (VoltageIn >= 1.668){lcd.setCursor(4, 1);lcd.print((char)2);}else {LcdClearByte(4);}
if (VoltageIn >= 2.085){lcd.setCursor(5, 1);lcd.print((char)2);}else {LcdClearByte(5);}
if (VoltageIn >= 2.502){lcd.setCursor(6, 1);lcd.print((char)3);}else {LcdClearByte(6);}
if (VoltageIn >= 2.919){lcd.setCursor(7, 1);lcd.print((char)4);}else {LcdClearByte(7);}
if (VoltageIn >= 3.336){lcd.setCursor(8, 1);lcd.print((char)5);}else {LcdClearByte(8);}
if (VoltageIn >= 3.753){lcd.setCursor(9, 1);lcd.print((char)6);}else {LcdClearByte(9);}
if (VoltageIn >= 4.17){lcd.setCursor(10, 1);lcd.print((char)0);}else {LcdClearByte(10);}
if (VoltageIn >= 4.587){lcd.setCursor(11, 1);lcd.print((char)0);}else {LcdClearByte(11);}
}
void LcdClearByte(int c) //clearbyte function
{
lcd.setCursor(c,1);
for (i = 0; i < 16; i = i + 1) {lcd.print(" ");
}
}