Guys, i have a project, the project is analog to digital weight display. I use slide potentiometer as the analog input, but the result its not same with the analog display weight, can you help me to accurate it?
This is my code.
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
const byte numRows= 4; //baris pada keypad
const byte numCols= 4; //Kolom pada keypad
/*keymap mendefinisikan tombol ditekan sesuai
dengan baris dan kolom seperti muncul pada keypad*/
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows]= {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
LiquidCrystal_I2C lcd1(0x3F ,2,1,0,4,5,6,7,3, POSITIVE); //Inisialisasi LCD 1
LiquidCrystal_I2C lcd2(0x27 ,2,1,0,4,5,6,7,3, POSITIVE); //Inisialisasi LCD 2
const byte pot=A0; //inisial pin slide potentiometer
int i = 0;
char input[7];
float result;
void setup() {
Serial.begin(9600); //
lcd1.begin(16,2);
lcd1.clear();
lcd2.begin(16,2);
lcd2.clear();
pinMode(pot, INPUT);
}
void loop(){
float data = analogRead(pot);
float konversi =(((data*(1024/1024))/100)-0.90);
char customKey = myKeypad.getKey();// inisialisasi keyped sebagai tampilan character
if (customKey && customKey != 'B' && customKey != '#'){
lcd1.setCursor(i,0);
lcd1.print(customKey);
input[i]=customKey;
i++;
i<7;
}
if(i == 7){
lcd2.clear();
clearData();
}
if (customKey == 'B')
{
lcd2.setCursor(4,1);
long result=atof(input);
result*=konversi;
lcd2.println(result);
lcd2.setCursor(0,1);
lcd2.print("Rp.");
}
if (customKey== '#')
{
lcd2.clear();
lcd1.clear();
clearData();
}
lcd1.setCursor(5,1);
lcd1.print("Kg");
lcd1.setCursor(7,0);
lcd1.print("|");
lcd1.setCursor(8,1);
lcd1.print("Berat/kg");
lcd1.setCursor(7,1);
lcd1.print("|");
lcd1.setCursor(0,1);
lcd1.print(konversi);
lcd1.setCursor(8,0);
lcd1.print("Harga/kg");
lcd2.setCursor(2,0);
lcd2.print("Total Harga");
}
void clearData()
{
while(i !=0)
{ // This can be used for any array size,
input[i--] = 0; //clear array for new data
}
return;
}
this is the image
it shown 1 kg in analog, but in lcd, it show a half of it, how to accurate it?