// this code and design is in the public domain
// designed by Peterthinks Sept 25th 2021
#include <LiquidCrystal_I2C.h>
// include the LCD library
LiquidCrystal_I2C lcd(0x27,20,3);
// declare globally the type, name and assign a value to the variables.
// I used longs for +, - and * with floats for division
// so the decimal points show without rounding off and
// the numbers do not roll over into the negatives as they
// would with intergers
long sensorValue3 = 0;
float FsensorValue1 = 0;
float FsensorValue3 = 0;
long sensorValue1 = 0;
// sensorValue2 controls which operation (+,-,*,/) is being performed
// an interger is big enough for this
int sensorValue2 = 0;
// run the setup once
void setup()
{
// start the LCD and tell the arduino
// how many characters and rows the LCD has
lcd.begin(16, 2);
// turn on the LCD backlight
lcd.backlight();
// declare pinmodes as inputs for the potientiometer wiper pins
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}
// begin the loop
void loop()
{
// read the values coming into the arduino and fill the variables
// A0 and A2 are read twice to fill the intergers and float variables
sensorValue1 = analogRead(A0);
sensorValue2 = analogRead(A1);
sensorValue3 = analogRead(A2);
FsensorValue1 = analogRead(A0);
FsensorValue3 = analogRead(A2);
// begin the else/if statements
// if sensorValue2 is less than or equal to 256 addition is performed
// on the user input long variables
if (sensorValue2 <= 256)
{
// map the value of A0 and A2 from the raw 0 to 1023 to 0 to 10000
sensorValue1 = map(analogRead (A0), 0, 1023, 0, 100);
sensorValue3 = map(analogRead (A2), 0, 1023, 0, 100);
// set the cursor at the first character of the first line
lcd.setCursor(0, 0);
// print sensorValue3 to the LCD starting at the first character on the first line
lcd.print(sensorValue3);
// print a + symbol with a space on either side of it beside sensorValue3
lcd.print(" + ");
// print sensorValue1 to the LCD after the + symbol and spaces
lcd.print(sensorValue1);
// print some blank spaces to clear anything to the right if a longer
// equation was previously shown on the LCD
// this is used instead of lcd.clear(); to prevent flicker on the LCD
lcd.print(" ");
// set the cursor at the first character of the second line
lcd.setCursor(0, 1);
// add the two longs together
lcd.print(sensorValue3+sensorValue1);
// print some blank spaces to clear anything to the right if a longer
// answer was previously shown on the LCD
lcd.print(" ");
}
// second of the else/if statements
// if sensorValue2 is greater than 256 and less than or equal to 512
// subtraction is performed on the user input long variables
else if (sensorValue2 > 256 && sensorValue2 <= 512)
{
sensorValue1 = map(analogRead (A0), 0, 1023, 0, 100);
sensorValue3 = map(analogRead (A2), 0, 1023, 0, 100);
lcd.setCursor(0, 0);
lcd.print(sensorValue3);
lcd.print(" - ");
lcd.print(sensorValue1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(sensorValue3-sensorValue1);
lcd.print(" ");
}
// third of the else/if statements
// if sensorValue2 is greater than 512 and less than or equal to 768
// multiplication is performed on the user input long variables
else if (sensorValue2 > 512 && sensorValue2 <= 768)
{
sensorValue1 = map(analogRead (A0), 0, 1023, 0, 100);
sensorValue3 = map(analogRead (A2), 0, 1023, 0, 100);
lcd.setCursor(0, 0);
lcd.print(sensorValue3);
lcd.print(" * ");
lcd.print(sensorValue1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(sensorValue3*sensorValue1);
lcd.print(" ");
}
// last of the else/if statements
// if sensorValue2 is any other value
// division is performed on the user input float variables
// FsensorValue1 and FsensorValue3. note that "else" is used
// instead of "if else" since this is a catch all to prevent
// stepping out of the loop and breaking the program
else
{
sensorValue1 = map(analogRead (A0), 0, 1023, 0, 100);
sensorValue3 = map(analogRead (A2), 0, 1023, 0, 100);
// only the long data versions are used to display the equation
// since the decimal of the float is only needed in the answer
lcd.setCursor(0, 0);
lcd.print(sensorValue3);
lcd.print(" / ");
lcd.print(sensorValue1);
lcd.print(" ");
lcd.setCursor(0, 1);
FsensorValue1 = sensorValue1;
FsensorValue3 = sensorValue3;
// the ,6 at the end of the division equation forces six decimal
// places on the float answer. otherwise it only shows two decimal places
lcd.print(FsensorValue3/FsensorValue1,6);
lcd.print (" ");
}
// and go back up to the top of the loop
}