Greetings.
Recently I created a calculator with a potentiometer, Arduino and LCD shield, it is pretty simple, rotate the potentiometer to display any value, then, press the button and the value will be added, the problem is, when I press the button, the only value displayed on the screen is 19, if I press it again, the same value will be added and the sum will be displayed, (19 + 19 = 38) regardless of the potentiometer value, 19 will always be added.
What I used for this project:
Arduino IDE 1.8.13
Arduino UNO board
10k ohm potentiometer
The library and shield;
https://wiki.dfrobot.com/LCD_KeyPad_Shield_For_Arduino_SKU__DFR0009
This is my coding so far (old):
int button = A0;
int pot = A5;
int buttonstate = (analogRead(button));
int oldvalue = 0;
int newvalue = 0;
int variable = pot;
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup ()
{
pinMode(button, INPUT);
Serial.begin (9600);
lcd.begin(16,2);
}
void loop (){
lcd.setCursor (0,0);
lcd.print("increase:");
lcd.setCursor(0,1);
lcd.print("remaining:");
lcd.setCursor(9,0);
lcd.print(analogRead(pot));
if (analogRead(button) == 743)
{
newvalue = (variable+oldvalue);
lcd.setCursor (10,1);
lcd.print(newvalue);
delay(200);
}
if (analogRead(button) != 743)
{
oldvalue = newvalue;
}
Serial.println(newvalue);
}
Functional code (New):
int buttonPin = A0;
int buttonVal = 0;
int potPin = A5;
int oldvalue = 0;
int newvalue = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup ()
{
pinMode(buttonPin, INPUT);
pinMode(potPin,INPUT);
Serial.begin (9600);
lcd.begin(16,2);
}
void loop (){
buttonVal = analogRead(buttonPin);
int potVal = analogRead(potPin);
//First thing the code will do, read the pot and create variable
lcd.setCursor (0,0);
lcd.print("increase:");
lcd.setCursor(0,1);
lcd.print("remaining:");
lcd.setCursor(9,0);
lcd.print(potVal);
delay(300);
if (buttonVal < 753 && buttonVal > 733) // Sets a range for reading value.
//Analog signals aren't accurate, various factors make them unstable, therefore I have to set a range for reading these values.
{
newvalue = (potVal +oldvalue); //Creates a new value based on the addition of 0 + potVal
lcd.setCursor (10,1);
lcd.print(newvalue); // Prints newvalue
delay(200);
lcd.setCursor (10,1);
}
if (buttonVal > 753 || buttonVal < 733)
{
oldvalue = newvalue;
}
Serial.println (buttonVal);
}