Arduino Calculator with potetionmeter

Dear Arduino Community,

I am writing to ask for your help in this confusing assignment that I have got in my first year of university.

I have been learning Arduino uno using C and C++ languages for 2 weeks now.

Our teacher has given us an assignment. This assignment, requires us to do the following:

This is a special calculator without a keypad, but it is using the rich shield. It uses one button (left button) and
the potentiometer for input, and the four LEDs and the 4-digit display for output.
 The values, operator and result are shown on the display. The value 1 and value 2 are shown as integer values between -10 and 10. The result is shown as a floating point value with one decimal point
(e.g. 1.2 or 1.6). The operator is one of the characters ‘a’ (add), ‘s’ (subtract), ‘t’ (times), ‘d’ (divide)
for respectively ‘+’, ‘-’, ‘*’ and ‘/’; the display does not support these symbols.
 The button is used to switch between four modes: (1) input value 1, (2) input operator, (3) input
value 2 and (4) output result. The user can repeatedly press the button to alternate the modes in a
cyclic fashion (1) → (2) → (3) → (4) → (1) → (2) → etc.
->In modes (1), (2) and (3) the blue LED is on. The other LEDs are off.
->In mode (1) and (3), the potentiometer is used to choose a value between -10 and 10 by turning theEmbedded Systems Oriented Assignment 1
2 / 2
potentiometer. The centre of the potentiometer should be 0. Tip: Use the map() function to map the
potentiometer values to useful integer values.
->In mode (2), the potentiometer is used to choose ‘a’, ‘s’, ‘t’ and ‘d’ by turning the potentiometer.
→ In mode (4),
→ the yellow LED is on when the result does not perfectly fit on the display;
→ the red LED is on when the result cannot be calculated, e.g. 6 / 0. In this case the display
should show "Err ";
→ the green LED is on when the yellow and red LEDs are off;
→ the blue LED is off.
→ The calculator should only react on pressing buttons and not holding buttons.
The interaction sequence of the calculator is described as follows:

  1. After turning on the calculator, the display shows " - - - -". The user must press the button to turn on
    the calculator. After pressing the button, all four LEDs light up for 2 seconds.
  2. Then the display shows the first value. The value is associated to the angle of the potentiometer. By
    turning the potentiometer, the value can be changed. The calculator is in input value 1 mode (1).
  3. After pressing the button again, the value is selected and the calculator goes into input operator
    mode (2). The first operator ‘p’ is shown. The user can switch between the operators using the potentiometer; ‘a’ (0-25%), ‘s’ (25-50%), ‘t’ (50-75%) and ‘d’ (75-100%).
  4. After pressing the button again, the displayed operator is selected, and the calculator goes into the
    input value 2 (3) mode. The value is associated to the angle of the potentiometer.
  5. After pressing the button, the value is selected, and the calculator will calculate the result.
  6. After pressing the button, all LEDs are off and the calculator starts again at step 2.
    The display is limited to 4 digits. Values like 5.000, 0.250 and -4.30 fit on the display. However, values like
    1000.0, 5.2156, -123.0 and -0.3333333 do not fit on the display. Adapt your program so that the following
    requirements are met.
    → The display should show “[–]“ when the result exceeds the 4 digits; e.g. 1000.0 and -123.0. Determine the positive and negative limits of the display. In this case, the yellow LED should turn on to indicate that the result cannot be displayed.
    → Results like 1/3 is 0.33333333 which is shown as 0.333 on the display. In this case the yellow LED
    turns on to indicate that the result is imprecise. TIP: Scale the result by 10x, 100x or 1000x. Compare
    the integer value and the float value of the scaled result. Notice the minus symbol ‘-‘ is a digit.

I have a problem with selecting the values, the display gets stuck with one value and I can’t see the other values when I turn the knob. And The last two tasks I have no idea on how to print this to arduino.
Here’s my code so far.

#include <Display.h>


const int POTPIN = A0;
const int Led_Red = 4;
const int Led_Green = 5;
const int Led_Blue = 6;
const int Led_Yellow = 7;
const int BTN = 8;
int number1;
int number2;
float result;
int state = 0;
int lastButtonState = HIGH;

void setup() {
  // put your setup code here, to run once:

  pinMode(POTPIN, INPUT);
  pinMode(BTN, INPUT_PULLUP);
  pinMode(Led_Red, OUTPUT);
  pinMode(Led_Green, OUTPUT);
  pinMode(Led_Blue, OUTPUT);
  pinMode(Led_Yellow, OUTPUT);
  Display.clear();
  Serial.begin(9600);
}

void loop()
{

  int ButtonState = digitalRead(BTN);
  int valuePot = analogRead(POTPIN);
  float percentage = map(valuePot, 0 , 1023, 0, 100);
  //  Display.show(percentage2);

  if (ButtonState != lastButtonState )
  {
    if (ButtonState == LOW)
    {
      delay(10);
      if (state == 0) //state 0
      {
        //state 1
        Display.show("----");
        digitalWrite(Led_Red, HIGH);
        digitalWrite(Led_Green, HIGH);
        digitalWrite(Led_Blue, HIGH);
        digitalWrite(Led_Yellow, HIGH);
        state = 1;


      }
      else if (state == 1) //input first number
      {
        int value = analogRead(POTPIN);//read the potentiometer
        int number_1 = map(value, 0, 1023, -10, 10);//map the values
        Display.show(number_1);
        number_1 = number1;
        digitalWrite(Led_Red, LOW);
        digitalWrite(Led_Green, LOW);
        digitalWrite(Led_Blue, HIGH);
        digitalWrite(Led_Yellow, LOW);
        delay(100);
        state = 2;
      }
      else if (state == 2) //input operator
      {
        digitalWrite(Led_Red, LOW);
        digitalWrite(Led_Green, LOW);
        digitalWrite(Led_Blue, HIGH);
        digitalWrite(Led_Yellow, LOW);
        if (percentage < 25)
        {
          Display.show("a");

        }
        else if (percentage > 25 && percentage < 50)
        {
          Display.show("s");
        }
        else if (percentage > 50 && percentage < 75)
        {
          Display.show("t");
        }
        else if (percentage > 75 && percentage < 100)
        {
          Display.show("d");
        }
        state = 3;
      }
      else  if (state == 3) //input second number
      {
        int value = analogRead(POTPIN);//read the potentiometer
        int number_2 = map(value, 0, 1023, -10, 10);//map the values
        Display.show(number_2);
        number_2 = number2;
        delay(100);
        digitalWrite(Led_Red, LOW);
        digitalWrite(Led_Green, LOW);
        digitalWrite(Led_Blue, HIGH);
        digitalWrite(Led_Yellow, LOW);

        state = 4;

      }
      else if (state == 4)// state 4
      {
        if (percentage < 25)
        {
          result = number1 + number2;
          Display.show(result);
          digitalWrite(Led_Red, LOW);
          digitalWrite(Led_Green, HIGH);
          digitalWrite(Led_Blue, LOW);
          digitalWrite(Led_Yellow, LOW);


        }
        else if (percentage > 25 && percentage < 50)
        {
          result = number1 - number2;
          Display.show(result);
          digitalWrite(Led_Red, LOW);
          digitalWrite(Led_Green, HIGH);
          digitalWrite(Led_Blue, LOW);
          digitalWrite(Led_Yellow, LOW);

        }
        else if (percentage > 50 && percentage < 75)
        {
          result = number1 * number2;
          Display.show(result);
          digitalWrite(Led_Red, LOW);
          digitalWrite(Led_Green, HIGH);
          digitalWrite(Led_Blue, LOW);
          digitalWrite(Led_Yellow, LOW);

        }
        else if (percentage > 75 && percentage < 100)
        {
          if (number2 == 0) {
            digitalWrite(Led_Red, HIGH);
            digitalWrite(Led_Green, LOW);
            digitalWrite(Led_Blue, LOW);
            digitalWrite(Led_Yellow, LOW);
            Display.show("Err");
          }
          else {
            result = number1 / number2;
            Display.show(result);
            digitalWrite(Led_Red, LOW);
            digitalWrite(Led_Green, HIGH);
            digitalWrite(Led_Blue, LOW);
            digitalWrite(Led_Yellow, LOW);

          }

        }
        state = 5;
      }
      else if (state == 5)
      {
        Display.clear();
        digitalWrite(Led_Red, LOW);
        digitalWrite(Led_Green, LOW);
        digitalWrite(Led_Blue, LOW);
        digitalWrite(Led_Yellow, LOW);
        state = 0;
      }

    }
    delay(50);
    lastButtonState = ButtonState;
  }
}

More members will see your code if you post your code as described in the forum guidelines.

There was a similar thread (using a pot as input for calculator) recently. A search should find it.

yes, I looked over them but none seemed to have my proble. I think I'm almost done, but I have problems with refreshing the display and I can't select the values live

You have to be careful about leaving gaps. What happens if percentage is exactly 75 ?

 else if (percentage > 50 && percentage < 75 )
 . . . 
 else if (percentage > 75 && percentage < 100 )

Anyway, that is quite a tough assignment. If you haven’t already done so, you have to produce a diagram showing all the states, and the transitions between the states for at least the main part of collecting all the inputs from the user of this calculator.
Something like this example, where the circles are states and the arrows represent transitions. Then it is also easier to help you. Clearly also , for such an assignment, it has to be mainly your own work with just limited assistance from others.

You’d then be able to show activities like, for example, initial display of all four leds for 2 seconds, which appears to be missing from your solution.