I've been working on coding a calculator, but i ran into a problem and can't figure out what i'm doing wrong.
The problem is, when dividing by 0 it should display 'Err' and the red LED should go on, sadly this is not the case. Could anyone help me?
This is my code
#include "Display.h"
int LEDRED = 4;
int LEDGREEN = 5;
int LEDBLUE = 6;
int LEDYELLOW = 7;
const int BUTTON = 9;
const int POTPIN = A0;
int value1, value2;
int Potval;
int mode = 0;
int ButtonState = 0;
int LastButtonState = HIGH;
float result;
float percent;
void setup() {
Display.show("----");
pinMode(LEDRED, OUTPUT);
pinMode(LEDGREEN, OUTPUT);
pinMode(LEDBLUE, OUTPUT);
pinMode(LEDYELLOW, OUTPUT);
pinMode(POTPIN, INPUT);
pinMode(BUTTON, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
int buttonState;
ButtonState = digitalRead(BUTTON); // read state of the button
if (ButtonState != LastButtonState) {
if (ButtonState == LOW) {
mode++;
if (mode > 5) {
mode = 2;
}
}
LastButtonState = ButtonState;
}
int PotVal;
PotVal = analogRead(POTPIN);
Serial.println(PotVal);
if (mode == 0) {
// do nothing
} else if (mode == 1) {
Display.show("");
digitalWrite(LEDBLUE, HIGH);
digitalWrite(LEDGREEN, HIGH);
digitalWrite(LEDYELLOW, HIGH);
digitalWrite(LEDRED, HIGH);
delay(2000);
mode = 2;
// press button, all leds turn on for 2 and after goes to input value 1
} else if (mode == 2) {
// input value 1
digitalWrite(LEDBLUE, HIGH);
digitalWrite (LEDRED, LOW);
digitalWrite (LEDGREEN, LOW);
digitalWrite(LEDYELLOW, LOW);
value1 = map(PotVal, 0, 1023, -10, 10);
Display.show(value1);
} else if (mode == 3) {
// input operator
digitalWrite(LEDBLUE, HIGH);
int PotVal = analogRead(POTPIN);
float percent = map(PotVal, 0, 1023, 0, 100);
if ((percent >= 0) && (percent <= 25)) {
Display.show ("a");
} else if ((percent >= 25) && (percent <= 50)) {
Display.show ("s");
} else if ((percent >= 50) && (percent <= 75)) {
Display.show ("t");
} else if ((percent >= 75) && (percent <= 100)) {
Display.show ("d");
}
} else if (mode == 4) {
// input value 2
digitalWrite(LEDBLUE, HIGH);
int PotVal = analogRead(POTPIN);
value2 = map(PotVal, 0, 1023, -10, 10); // value2 should be mapped into the number -10 and 10
Display.show(value2);
} else if (mode == 5) {
// calculate and display result
digitalWrite(LEDBLUE, LOW);
float result;
if ((percent >= 0) && (percent <= 25)) { // When potpin value is in between 0 and 25, the operator is +
result = value1 + value2;
digitalWrite(LEDGREEN, HIGH);
Display.show(result);
} else if ((percent >= 25) && (percent <= 50)) { // When potpin value is in between 25 and 50, the operator is -
result = value1 - value2;
digitalWrite(LEDGREEN, HIGH);
Display.show(result);
} else if ((percent >= 50) && (percent <= 75)) { // When potpin value is in between 50 and 75, the operator is *
result = value1 * value2;
digitalWrite(LEDGREEN, HIGH);
Display.show(result);
} else if ((percent >= 75) && (percent <= 100)) { // When potpin value is in between 75 and 100,the operator is /
float valueone = value1;
result = valueone / value2;
if (value2 != 0) {
Display.show(result);
digitalWrite(LEDGREEN, HIGH); // When answer correct, Green Led turns on and shows result
} else {
Display.show("Err");
digitalWrite(LEDRED, HIGH); // When trying to devide by 0, show error and Red Led turns on
}
}
}
}
}