Potentiometer as calculator (Solved)

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);
  
 }

What is "button"? Is it a momentary switch? How is the button wired? A button switch usually reads high (1023)or low (0), not some value in between.

Most people use digital inputs to read buttons, and they add code to prevent malfunctions due to switch bounce.

The LCD shield uses a resistive divider method to read several buttons on an analog pin. But good luck is needed to achieve this level of accuracy… :slight_smile:

if (analogRead(button) == 743)

With resistor tolerances, noise and drift, what are the chances that it will read exactly 743? Slim.

  if (analogRead(button) == 743)
   {
   newvalue = (variable+oldvalue);
  lcd.setCursor (10,1);
  lcd.print(newvalue);
  delay(200);
  }

when the button is pressed, you are adding some thing called 'variable' to your' oldvalue' to get 'newvalue'

but what is 'variable' ?

Answer:

  int variable = pot;
int pot = A5;

It looks to me as though you are adding the pin number that the pot is connected to,

I don't see that the pot ever gets read.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can I suggest you read the pot ONCE at the start of the loop and save it to a variable, then use that variable through your code.

In other words take a snapshot of your inputs and act on them, then read then again and act, etc etc.

Naming pins would be easier to track by using variable names like

int buttonPin = A0;
int potPin = A5;

Tom... :slight_smile:

  if (analogRead(button) == 743)

And let me assure you, that is going to work something of the order of one time in a thousand, and only then if you are lucky! :cold_sweat:

groundFungus:
What is "button"? Is it a momentary switch? How is the button wired? A button switch usually reads high (1023)or low (0), not some value in between.

it is connected to pin A0, it reads 743 analog input, sorry, forgot to mention that

jremington:
Most people use digital inputs to read buttons, and they add code to prevent malfunctions due to switch bounce.

Indeed, but I'm using an LCD shield, this shield has 5 buttons, every button will give a different analog reading, (select = 743, left = 505, up = 144, down = 330, right = 0, none = 1023), So, to read an input, I have to specify which button is pressed.

You have missed the point. Write a sketch that just displays the readings from the analog input, and watch the values jump around. Your program throws the dice tens or hundreds of times before landing on the right number. What you should be testing for is a range, e.g. x>743-5 and x<743+5.

There are dozens of factors that can cause the reading to change, temperature, supply voltage, phase of the moon… if you keep doing it your way, it will fail mysteriously when any of those change. Except for the moon - that is just an argumentative device.

Hi,
Google

arduino lcd shield

This is a good place to start;

It has some sample code to show how to read the buttons.

Do you have this LCD?

Tom.... :slight_smile:
PS. Can you tell us what values you get for each button please?

JohnLincoln:

  if (analogRead(button) == 743)

{
  newvalue = (variable+oldvalue);
 lcd.setCursor (10,1);
 lcd.print(newvalue);
 delay(200);
 }



when the button is pressed, you are adding some thing called '**variable**' to your' **oldvalue**' to get **'newvalue**' 


but what is '**variable**' ?







Answer:


int variable = pot;





int pot = A5;





It looks to me as though you are adding the pin number that the pot is connected to,

I used "pot" to attach the A5 and the "variable" is the int that I'm gonna work with, this may be unessesary but I feel okay with it.

Hi,
Tip when coding.

int pot = A5;

edit to;

int potPin = A5;

Putting Pin on the end of your pin named identifies it as a Pin and not a varable or other parameter.

Tom... :slight_smile:
PS. Can you tell us what values you get for each button please?

[quote author=aarg date=1598305199 link=msg=4717267]
You have missed the point. Write a sketch that just displays the readings from the analog input, and watch the values jump around. Your program throws the dice tens or hundreds of times before landing on the right number. What you should be testing for is a range, e.g. x>743-5 and x<743+5.

There are dozens of factors that can cause the reading to change, temperature, supply voltage, phase of the moon... if you keep doing it your way, it will fail mysteriously when any of those change. Except for the moon - that is just an argumentative device.
[/quote]


Wow, too many factors, thanks. Is this the way I'm supposed to add it to the code?:

//Turn the statement into a function

void statement1 ()
{
newvalue = (variable+oldvalue);
lcd.setCursor (10,1);
lcd.print(newvalue);
delay(200)
}

// adding the “>” and "<"conditions.

int more = 743+5
int less = 743-5

if (analogRead(button) =>less )
{
statement1 ();

}

if (analogRead(button) =<more )
{
statement1();

}

/*

using && (and)

if (analogRead(button) =<more && =>less )
{
statement1 ();

}

//send help I don’t know what am I doing right now

*/

[/code]

TomGeorge:
Hi,
Tip when coding.

int pot = A5;

edit to;

int potPin = A5;

Putting Pin on the end of your pin named identifies it as a Pin and not a varable or other parameter.

Tom… :slight_smile:
PS. Can you tell us what values you get for each button please?

1-. "Tip when coding.“OH, I didn’t know that, thanks.
2.- " Can you tell us what values you get for each button please?” Sorry for the delay, I was checking other posts, select = 743 left 505 up = 144 down = 330 right = 0.

Have you come up with some solution to the problem in reply #5?

[quote author=aarg link=msg=4717337 date=1598312560]
Have you come up with some solution to the problem in reply #5?
[/quote]
Not sure what he means with "pot read"

void loop (){
lcd.setCursor (0,0);
lcd.print("increase:");
lcd.setCursor(0,1);
lcd.print("remaining:");
lcd.setCursor(9,0);
lcd.print(analogRead(pot));//reads & print on my lcd the current pot value [/code]

groundFungus:
What is "button"? Is it a momentary switch? How is the button wired? A button switch usually reads high (1023)or low (0), not some value in between.

1.- "What is "button"?" It is a momentary switch
2.- " How is the button wired? A button switch usually reads high (1023)or low (0),"
I'm using an LCD shield, this shield has 5 buttons, every button will give a different analog reading, (select = 743, left = 505, up = 144, down = 330, right = 0, none = 1023), So, to read an input, I have to specify which button is pressed.

Hi,
To read and detect the select button, value 743, you need to;

Read the analog button.
Using if statement look for value between 733 and 753.
If true then the select button has been pressed.

Tom… :slight_smile: