Hello, I got a task to build a calculator, which devides, subtracts, adds and times the numbers between -10 and 10 using a potentiometer to select them and a button to switch between 4 modes:
1 mode - pick a number using the potentiometer and store its value
2 mode - pick an operator( ‘a’ (add), ‘s’ (subtract), ‘t’ (times), ‘d’ (divide)) using the potentiometer and store it
3 mode - pick a second number and store it
4 mode - do the calculation and display the result
I believe that I am using an arduino uno and a rich shield with a 4-digit display, potentiometer, 2 buttons, 4 leds and some other stuff.
I have been working on it for 2 days and got hardstuck on the following:
So, our teacher told us to use sequence state pattern, but when I tried it , it wasn't very successful, so I wrote some code and managed to get mode 1 working but the problem is that in order to work the if-statement should be outside the button-if-statement, and when I try to go back to the button the state becomes 0 again and it sends me back to mode1 but without this state I can't even run mode1, so I just can't figure out how to switch between modes, I either skip mode1 and go to mode2 directly ot jsut stuck to mode1 only. I know that I should use this sequence state patter but can't figure out exactly how I should do it. This is my first post on the forum and also my first arduino task. I would be very thankful for any tipps that you, guys, can give me!!
Here is my code:
#include <DHT11.h>
#include <Display.h>
#include <TM1637Display.h>
const int number1;//imput
const int number2;//input
float result;//output
const int POTPIN = A0;//potentionmeter
const int LED_RED = 4;//red led
const int LED_GREEN = 5;//green led
const int LED_BLUE = 6;//blue led
const int LED_YELLOW = 7;//yellow led
const int BUTTON = 9;//left button
int lastButtonState = HIGH;
int state = -1;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(POTPIN, INPUT);
Display.clear();
Serial.begin(9600);
}
void loop() {
int buttonState = digitalRead(BUTTON);
if ( buttonState != lastButtonState)
{
if ( buttonState == LOW)//mode 1
{
Display.show("____");
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_YELLOW, HIGH);
delay(2000);
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_YELLOW, LOW);
delay(1000);
digitalWrite(LED_BLUE, HIGH);
state = 0;
if (state == 1)//mode 2
{
digitalWrite(LED_BLUE, HIGH);
state = 2;
}
}
lastButtonState = buttonState;
}
if (state == 0 )
{
int value = analogRead(POTPIN);//read the potentiometer
int number_1 = map(value, 0, 1023, -10, 10);//map the values
Display.show(number_1);
}
else if (buttonState == LOW) {//button pressed again
state = 1;
}
if (state == 2 )
{
int value = analogRead(POTPIN);
int percentage = map(value, 0, 1023, 0, 100);
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
{
Display.show("d");
}
}
}