[Solved] Using pots to change numbers then display and add the numbers

I am trying to make a little calculator that uses two potientometers to input a couple numbers
Then the arduino nano adds them together and displays them.

I got as far as getting the two inputs on the LCD on the first line and the output (sum) on the second line
It is a 2 by 16 display.

But the numbers were so bouncy that I couldn't even tell if they were adding properly.

The range of 0 to 256 is fine for the pot inputs this is just something for me to build on.

Does anyone have any advice? It seems so simple but it is kicking my butt. I always seem to have a
Bracket in the wrong place or something broken when I change anything. I erased it all and went back
To just debouncing a button and adding on a display but with constants. Can't get it to work with pots.

Or if anyone has any code that does something similar I would like to see it.

Thank you

Peter

Program the sketch so one pot increments a variable when it is set to > 600 and decrements it when < 400.

The variable stays constant if set <550 but >450.

The other pot can do the same.

Incrementing and decrementing can occur at a certain rate, maybe once every 500ms.

Maybe if >700 or <300 the incrementing and decrementing can occur at a 100ms rate.

//**********************************************************************
//
//  Version   YY/MM/DD     Comments
//  1.00      18/12/01     Running code
//
//  Adjust a potentiometer to set a variable to a given value.
//
//**********************************************************************

//i/o
const int firstNumberPot = A0;

//sram
int firstNumberPotReading;
int firstValue;

//timing stuff
unsigned long firstValueMillis;

//**********************************************************************
void setup()
{
  Serial.begin(9600);

} //END of setup()

//**********************************************************************
void loop()
{
  //*********************************************
  //adjust the first variable
  firstNumberPotReading = analogRead(firstNumberPot);

  //don't adjust
  if (firstNumberPotReading > 450 && firstNumberPotReading < 550)
  {
    //do nothing
  }

  //slow increment
  else if (firstNumberPotReading > 550 && firstNumberPotReading < 700)
  {
    if (millis() - firstValueMillis >= 500)
    {
      //restart timer
      firstValueMillis = millis();
      
      firstValue++;
      Serial.println(firstValue);
    }
  }

  //slow decrement
  else if (firstNumberPotReading < 400 && firstNumberPotReading > 300)
  {
    if (millis() - firstValueMillis >= 500)
    {
      //restart timer
      firstValueMillis = millis();
      
      firstValue--;
      Serial.println(firstValue);
    }
  }

  //fast increment
  else if (firstNumberPotReading > 700)
  {
    if (millis() - firstValueMillis >= 100)
    {
      //restart timer
      firstValueMillis = millis();
      
      firstValue++;
      Serial.println(firstValue);
    }
  }

  //fast decrement
  else if (firstNumberPotReading < 300)
  {
    if (millis() - firstValueMillis >= 100)
    {
      //restart timer
      firstValueMillis = millis();
      
      firstValue--;
      Serial.println(firstValue);
    }
  }

  //*********************************************

} //END of loop()


//**********************************************************************

you could try reading and updating less frequently by perhaps doing so only every half second

you could average the values from the pot. I like leaky integration which favors the newest values. the average needs to be a float, but you would use the integer value

a[n] = a [n-1] + (s [n] - a [n-1]) / N

you may already be reducing the resolution of the ADC measurement, such as dividing it by 4 and taking the integer result which allows fractional value jitter that won't the integer value

Ok, I will see if I can get the code going. Thank you for taking the time to write that down larryd. gciurpita I will try to wrap my head around that one if it still bounces a lot I will try it.

This is the hot mess I have. I commented out a bunch of things and just made it add two intergers together to sanity check my math.

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7,8,9,10,11,12);

int a = 2;
int b = 5;

void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
int potPin1;
int potPin2;

pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
}

void loop()
{

lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
lcd.print("A= "); // Prints A: to LCD
lcd.print(a); // Prints value on Potpin1 to LCD
delay (500);
lcd.setCursor(8,0); // Sets the cursor to col 1 and row 0
lcd.print("B= "); // Prints B: to LCD
lcd.print(b); // Prints value on Potpin1 to LCD
delay (500);

lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
lcd.print("A + B= ");
lcd.print(a + b);

//lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
//lcd.print("A: "); // Prints Sensor Val: to LCD
//lcd.print(analogRead(potPin1)); // Prints value on Potpin1 to LCD
//lcd.setCursor(8,0); // Sets the cursor to col 1 and row 0
//lcd.print("B: "); // Prints Sensor Val: to LCD
//lcd.print(analogRead(potPin2)); // Prints value on Potpin1 to LCD
//lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0
//lcd.print("A+B: "); // Prints A+B: to LCD
//lcd.print(analogRead(potPin1) + (potPin2)); // Prints value on Potpin1 + potpin2 to LCD
//delay (0);

}

And are you happy with the way it works?

No not really. Well the two lines added up and displayed properly when I just used integers so I guess that is something. The display works. But the using pots to input the numbers does not work. Research continues.

Did you try the sketch posted in reply #1?

Add a second potentiometer with appropriate variables to your lcd code.

I uploaded it to my Nano. Just working on adding my LCD to it...compiled and it is on board now. I will see if I can mix the two together

I think I broke everything. I have to go back to buttons and an lcd then work up to potientometers. I will save your code for later larryd thank you for the help. It just doesn't make sense to me yet though.

Potentiometer to 1/2 scale # stays unchanged.
Potentiometer to full scale # increases.
Potentiometer to zero scale #decreases.

Happy New Year

//**********************************************************************
//
//  Version   YY/MM/DD     Comments
//  1.01      18/12/01     Running code
//
//  Adjust two potentiometers to clculate a sum value.
//
//**********************************************************************

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
//   LCD pins:  RS  EN DB4 DB5 DB6 DB7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//i/o
const byte firstNumberPot  = A0;
const byte secondNumberPot = A1;

//sram
int firstNumberPotReading;
int firstValue;
int secondNumberPotReading;
int secondValue;

//timing stuff
unsigned long firstValueMillis;
unsigned long secondValueMillis;
unsigned long printMillis;


//**********************************************************************
void setup()
{
  Serial.begin(9600);

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  //         0123456789111111
  //                   012345
  lcd.print("Pot.  Calculator");

  delay(2000);

} //END of setup()

//**********************************************************************
void loop()
{
  //*********************************************
  //adjust the first variable
  firstNumberPotReading = analogRead(firstNumberPot);

  //don't adjust
  if (firstNumberPotReading > 450 && firstNumberPotReading < 550)
  {
    //do nothing
  }

  //slow increment
  else if (firstNumberPotReading > 550 && firstNumberPotReading < 700)
  {
    if (millis() - firstValueMillis >= 500)
    {
      //restart timer
      firstValueMillis = millis();

      firstValue++;
    }
  }

  //slow decrement
  else if (firstNumberPotReading < 400 && firstNumberPotReading > 300)
  {
    if (millis() - firstValueMillis >= 500)
    {
      //restart timer
      firstValueMillis = millis();

      firstValue--;
    }
  }

  //fast increment
  else if (firstNumberPotReading > 700)
  {
    if (millis() - firstValueMillis >= 100)
    {
      //restart timer
      firstValueMillis = millis();

      firstValue++;
    }
  }

  //fast decrement
  else if (firstNumberPotReading < 300)
  {
    if (millis() - firstValueMillis >= 100)
    {
      //restart timer
      firstValueMillis = millis();

      firstValue--;
    }
  }

  //*********************************************
  //adjust the second variable
  secondNumberPotReading = analogRead(secondNumberPot);

  //don't adjust
  if (secondNumberPotReading > 450 && secondNumberPotReading < 550)
  {
    //do nothing
  }

  //slow increment
  else if (secondNumberPotReading > 550 && secondNumberPotReading < 700)
  {
    if (millis() - secondValueMillis >= 500)
    {
      //restart timer
      secondValueMillis = millis();

      secondValue++;
    }
  }

  //slow decrement
  else if (secondNumberPotReading < 400 && secondNumberPotReading > 300)
  {
    if (millis() - secondValueMillis >= 500)
    {
      //restart timer
      secondValueMillis = millis();

      secondValue--;
    }
  }

  //fast increment
  else if (secondNumberPotReading > 700)
  {
    if (millis() - secondValueMillis >= 100)
    {
      //restart timer
      secondValueMillis = millis();

      secondValue++;
    }
  }

  //fast decrement
  else if (secondNumberPotReading < 300)
  {
    if (millis() - secondValueMillis >= 100)
    {
      //restart timer
      secondValueMillis = millis();

      secondValue--;
    }
  }

  //*********************************************
  if (millis() - printMillis >= 500)
  {
    //restart timer
    printMillis = millis();

    Serial.print(firstValue);
    Serial.print(" + ");
    Serial.print(secondValue);
    Serial.print(" = ");
    Serial.println(firstValue + secondValue);

    lcd.setCursor(0, 0); 
    //clear the line
    //                   111111
    //         0123456789012345
    lcd.print("                ");

    lcd.setCursor(0, 0); 
    lcd.print(firstValue);
    lcd.print(" + ");
    lcd.print(secondValue);
    lcd.print(" = ");
    lcd.print(firstValue + secondValue);
  }
} //END of loop()

//**********************************************************************

Thank you larryd! I will try that! And this is what I have sofar for two pots going to serial if you are interested

/*
Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255 and uses
the result to set the pulse width modulation (PWM) of an output pin.
Also prints the results to the Serial Monitor.

The circuit:

  • potentiometer connected to analog pin 0.
    Center pin of the potentiometer goes to the analog pin.
    side pins of the potentiometer go to +5V and ground
  • LED connected from digital pin 9 to ground

created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.

*/

// These constants won't change. They're used to give names to the pins used:
const int analogInPin1 = A1; // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A2; // Analog input pin that the second potentiometer is attached to
const int analogOutPin1 = 9; // Analog output pin that the LED is attached to
const int analogOutPin2 = 8; // Analog output pin that the LED is attached to

int sensorValue1 = 0; // value read from pot1
int outputValue1 = 0; // value output to the PWM (analog out)
int sensorValue2 = 0; // value read from pot2
int outputValue2 = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop() {
// read the analog in value of pot1:
sensorValue1 = analogRead(analogInPin1);
// read the analog in value of pot2:
sensorValue2 = analogRead(analogInPin2);
// map pot1 to the range of the analog out:
outputValue1 = map(sensorValue1, 0, 1023, 1, 20);
// map pot2 to the range of the analog out:
outputValue2 = map(sensorValue2, 0, 1023, 1, 20);
// change the analog out value of pot1:
analogWrite(analogOutPin1, outputValue1);
// change the analog out value of pot2:
analogWrite(analogOutPin2, outputValue2);

// print the results to the Serial Monitor for pot 1:
Serial.print("sensor 1 = ");
Serial.print(sensorValue1);
Serial.print("\t output 1 = ");
Serial.println(outputValue1);

// print the results to the Serial Monitor for pot 2:
Serial.print("sensor 2 = ");
Serial.print(sensorValue2);
Serial.print("\t output 2 = ");
Serial.println(outputValue2);

// wait 2 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(1000);
}

NO

  • LED connected from digital pin 9 to ground

YES

  • LED connected from digital pin 9 to a 220 ohm resistor connected to ground

FYI

larryd:
Potentiometer to 1/2 scale # stays unchanged.
Potentiometer to full scale # increases.
Potentiometer to zero scale #decreases.

Happy New Year

//**********************************************************************

//
//  Version   YY/MM/DD     Comments
//  1.01      18/12/01     Running code
//
//  Adjust two potentiometers to clculate a sum value.
//
//**********************************************************************

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
//   LCD pins:  RS  EN DB4 DB5 DB6 DB7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

//i/o
const byte firstNumberPot  = A0;
const byte secondNumberPot = A1;

//sram
int firstNumberPotReading;
int firstValue;
int secondNumberPotReading;
int secondValue;

//timing stuff
unsigned long firstValueMillis;
unsigned long secondValueMillis;
unsigned long printMillis;

//**********************************************************************
void setup()
{
 Serial.begin(9600);

// set up the LCD's number of columns and rows:
 lcd.begin(16, 2);

//         0123456789111111
 //                   012345
 lcd.print("Pot.  Calculator");

delay(2000);

} //END of setup()

//**********************************************************************
void loop()
{
 //*********************************************
 //adjust the first variable
 firstNumberPotReading = analogRead(firstNumberPot);

//don't adjust
 if (firstNumberPotReading > 450 && firstNumberPotReading < 550)
 {
   //do nothing
 }

//slow increment
 else if (firstNumberPotReading > 550 && firstNumberPotReading < 700)
 {
   if (millis() - firstValueMillis >= 500)
   {
     //restart timer
     firstValueMillis = millis();

firstValue++;
   }
 }

//slow decrement
 else if (firstNumberPotReading < 400 && firstNumberPotReading > 300)
 {
   if (millis() - firstValueMillis >= 500)
   {
     //restart timer
     firstValueMillis = millis();

firstValue--;
   }
 }

//fast increment
 else if (firstNumberPotReading > 700)
 {
   if (millis() - firstValueMillis >= 100)
   {
     //restart timer
     firstValueMillis = millis();

firstValue++;
   }
 }

//fast decrement
 else if (firstNumberPotReading < 300)
 {
   if (millis() - firstValueMillis >= 100)
   {
     //restart timer
     firstValueMillis = millis();

firstValue--;
   }
 }

//*********************************************
 //adjust the second variable
 secondNumberPotReading = analogRead(secondNumberPot);

//don't adjust
 if (secondNumberPotReading > 450 && secondNumberPotReading < 550)
 {
   //do nothing
 }

//slow increment
 else if (secondNumberPotReading > 550 && secondNumberPotReading < 700)
 {
   if (millis() - secondValueMillis >= 500)
   {
     //restart timer
     secondValueMillis = millis();

secondValue++;
   }
 }

//slow decrement
 else if (secondNumberPotReading < 400 && secondNumberPotReading > 300)
 {
   if (millis() - secondValueMillis >= 500)
   {
     //restart timer
     secondValueMillis = millis();

secondValue--;
   }
 }

//fast increment
 else if (secondNumberPotReading > 700)
 {
   if (millis() - secondValueMillis >= 100)
   {
     //restart timer
     secondValueMillis = millis();

secondValue++;
   }
 }

//fast decrement
 else if (secondNumberPotReading < 300)
 {
   if (millis() - secondValueMillis >= 100)
   {
     //restart timer
     secondValueMillis = millis();

secondValue--;
   }
 }

//*********************************************
 if (millis() - printMillis >= 500)
 {
   //restart timer
   printMillis = millis();

Serial.print(firstValue);
   Serial.print(" + ");
   Serial.print(secondValue);
   Serial.print(" = ");
   Serial.println(firstValue + secondValue);

lcd.setCursor(0, 0);
   //clear the line
   //                   111111
   //         0123456789012345
   lcd.print("                ");

lcd.setCursor(0, 0);
   lcd.print(firstValue);
   lcd.print(" + ");
   lcd.print(secondValue);
   lcd.print(" = ");
   lcd.print(firstValue + secondValue);
 }
} //END of loop()

//**********************************************************************

Thank you larryd!

I ended up with this and it does work! Thanks for the help! I will add other parts of your suggested code as I build my monster.

/*
*

  • Pot Addition Calculator for small numbers. A starting point for more complicated things :slight_smile:
  • Modified from example code in Arduino IDE
    Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255 and uses
the result to set the pulse width modulation (PWM) of an output pin.
Also prints the results to the Serial Monitor and LCD

The circuit:

  • 2 potentiometers connected to analog pins 0 and 1.
    Center pin of the potentiometers go to the analog pins.
    side pins of the potentiometers go to +5V and ground
  • LED connected from digital pin 9 to ground (commented out)
  • 16 by 2 LCD

created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe

Modified again by Peter Liwyj Dec 30 2019
Also includes code by larryd from the Arduino forums

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
// LCD pins: RS EN DB4 DB5 DB6 DB7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// These constants won't change. They're used to give names to the pins used:
const int analogInPin1 = A1; // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A2; // Analog input pin that the second potentiometer is attached to
//const int analogOutPin1 = 9; // Analog output pin that the LED is attached to
//const int analogOutPin2 = 8; // Analog output pin that the LED is attached to

int sensorValue1 = 0; // value read from pot1
int outputValue1 = 0; // value output to the PWM (analog out)
int sensorValue2 = 0; // value read from pot2
int outputValue2 = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);

lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print("Pot. Calculator"); // Send "Pot. calculator" to LCD
delay(2000); // Screen delay
}

void loop() {
// read the analog in value of pot1:
sensorValue1 = analogRead(analogInPin1);
// read the analog in value of pot2:
sensorValue2 = analogRead(analogInPin2);
// map pot1 to the range of the analog out:
outputValue1 = map(sensorValue1, 0, 1023, 1, 20);
// map pot2 to the range of the analog out:
outputValue2 = map(sensorValue2, 0, 1023, 1, 20);
// change the analog out value of pot1:
// analogWrite(analogOutPin1, outputValue1);
// change the analog out value of pot2:
// analogWrite(analogOutPin2, outputValue2);

// print the results to the Serial Monitor for pot 1:
Serial.print("sensor 1 = ");
Serial.print(sensorValue1);
Serial.print("\t output 1 = ");
Serial.println(outputValue1);

// print the results to the Serial Monitor for pot 2:
Serial.print("sensor 2 = ");
Serial.print(sensorValue2);
Serial.print("\t output 2 = ");
Serial.println(outputValue2);

lcd.setCursor(0, 0); //clear the line
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(sensorValue1);
lcd.print(" + ");
lcd.print(sensorValue2);
lcd.print(" = ");
lcd.print(sensorValue1 + sensorValue2);

delay(1000);
}

I confused sensor with output. This is fixed. Returns a value from 1 to 20 and adds them both. This seems to be enough to compensate for the bounce that was making my readings go insane!
And a Capacitor across my breadboard supply +- rail took the rest of the crazy away. I noticed that the breadboard behaved much better on the computer USB cable as opposed to batteries with a small buck converter. I added a100uf 16 volt cap.

/*
*

  • Pot Addition Calculator for small numbers. A starting point for more complicated things :slight_smile:
  • Modified from example code in Arduino IDE
    Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 20 and uses
the result to set the pulse width modulation (PWM) of an output pin.
Also prints the results to the Serial Monitor and LCD

The circuit:

  • 2 potentiometers connected to analog pins 0 and 1.
    Center pin of the potentiometers go to the analog pins.
    side pins of the potentiometers go to +5V and ground
  • LED connected from digital pin 9 to ground (commented out)
  • 16 by 2 LCD

created 29 Dec. 2008
modified 9 Apr 2012
by Tom Igoe

Modified again by Peter Liwyj Dec 30 2019
Also includes code by larryd from the Arduino forums

This example code is in the public domain.

*/
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
// LCD pins: RS EN DB4 DB5 DB6 DB7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// These constants won't change. They're used to give names to the pins used:
const int analogInPin1 = A1; // Analog input pin that the potentiometer is attached to
const int analogInPin2 = A2; // Analog input pin that the second potentiometer is attached to
//const int analogOutPin1 = 9; // Analog output pin that the LED is attached to
//const int analogOutPin2 = 8; // Analog output pin that the LED is attached to

int sensorValue1 = 0; // value read from pot1
int outputValue1 = 0; // value output to the PWM (analog out)
int sensorValue2 = 0; // value read from pot2
int outputValue2 = 0; // value output to the PWM (analog out)

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);

lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.print("Pot. Calculator"); // Send "Pot. calculator" to LCD
delay(2000); // Screen delay
}

void loop() {
// read the analog in value of pot1:
sensorValue1 = analogRead(analogInPin1);
// read the analog in value of pot2:
sensorValue2 = analogRead(analogInPin2);
// map pot1 to the range of the analog out:
outputValue1 = map(sensorValue1, 0, 1023, 1, 20);
// map pot2 to the range of the analog out:
outputValue2 = map(sensorValue2, 0, 1023, 1, 20);
// change the analog out value of pot1:
// analogWrite(analogOutPin1, outputValue1);
// change the analog out value of pot2:
// analogWrite(analogOutPin2, outputValue2);

// print the results to the Serial Monitor for pot 1:
Serial.print("sensor 1 = ");
Serial.print(sensorValue1);
Serial.print("\t output 1 = ");
Serial.println(outputValue1);

// print the results to the Serial Monitor for pot 2:
Serial.print("sensor 2 = ");
Serial.print(sensorValue2);
Serial.print("\t output 2 = ");
Serial.println(outputValue2);

lcd.setCursor(0, 0); //clear the line
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print(outputValue1);
lcd.print(" + ");
lcd.print(outputValue2);
lcd.print(" = ");
lcd.print(outputValue1 + outputValue2);

delay(1000);
}

larryd:
NO

  • LED connected from digital pin 9 to ground

YES

  • LED connected from digital pin 9 to a 220 ohm resistor connected to ground

FYI

Resistor... yes thanks!

Adding 10uF capacitors from A0 to GND and A1 to GND may help also.

larryd:
Adding 10uF capacitors from A0 to GND and A1 to GND may help also.

Thanks, I will try that too. So far the massive division from over 1000 down to 40 seems to be taking care of the bounce. With the numbers I need to calculate with at work I don't need that high a range anyway. hitting over 40 MPA in my equipment would probably turn me into a surprised pink mist anyway. Calculations don't matter after that LOL!