#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
#define modePin 3
#define answerPin 8
#define questionPin 9
int16_t a = 1;
int16_t b;
int16_t c;
float lineOfSym = 0.0;
float vertexY = 0.0;
int8_t x1;
int8_t x2;
bool refresh = 0;
bool triggerHistory = 0;
byte mode = 1;
byte powerOf2[] = {
B11100,
B00100,
B01000,
B10000,
B11100,
B00000,
B00000,
B00000
};
void setup() {
// put your setup code here, to run once:
lcd.init();
lcd.backlight();
lcd.createChar(0, powerOf2);
//Serial.begin(9600);
pinMode(modePin, INPUT); //Mode Button
attachInterrupt(digitalPinToInterrupt(modePin), modeChange, FALLING);
pinMode(answerPin, INPUT); //Answer Button
pinMode(questionPin, INPUT); //New Question Button
}
void loop() {
//Serial.println(mode);
//Randomize
if ((digitalRead(questionPin) == HIGH) || (refresh == 1)) {
lcd.clear();
triggerHistory = 1;
refresh = 0;
a = random(1, 9);
x1 = random(-9, 9);
if (x1 == 0) {
x1 = 1;
}
x2 = random(-9, 9);
if (x2 == 0) {
x2 = 1;
}
}
//Set the equation
b = a * (x1 + x2);
c = a * (x1 * x2);
lineOfSym = -b / (2.0 * a);
vertexY = (a * lineOfSym * lineOfSym) + (b * lineOfSym) + c;
switch (mode) {
case 1: //Find X
//Draw the equation
if (triggerHistory == 1) {
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Find X");
lcd.setCursor(0,1);
lcd.print(a);
lcd.print("x");
lcd.write(0);
if (b > 0) {
lcd.print("+");
}
if (b != 0) {
lcd.print(b);
lcd.print("x");
}
if (c > 0) {
lcd.print("+");
}
lcd.print(c);
lcd.print("=0");
}
//Reveal Answer
if ((digitalRead(answerPin) == HIGH) && (triggerHistory == 1)) {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("The Answer To:");
delay(2000); //The amount of time "The Answer To:" is displayed
lcd.setCursor(0,1);
lcd.print(a);
lcd.print("x");
lcd.write(0);
if (b > 0) {
lcd.print("+");
}
if (b != 0) {
lcd.print(b);
lcd.print("x");
}
if (c > 0) {
lcd.print("+");
}
lcd.print(c);
lcd.print("=0");
delay(1000); //The amount of time the equation is displayed
lcd.setCursor(7,2);
lcd.print("Is....");
delay(1000); //The amount of time "Is...." is displayed
lcd.setCursor(0, 3);
lcd.print("x = ");
lcd.print(x1 * -1);
if (x1 != x2) {
lcd.print(" or ");
lcd.print("x = ");
lcd.print(x2 * -1);
}
delay(6000); //The amount of time the answer is displayed
while (digitalRead(answerPin) == 1) {
delay(1000);
}
refresh = 1;
lcd.clear();
}
//Preview Screen
if (triggerHistory == 0) {
lcd.setCursor(1, 1);
lcd.print("Quadratic Equation");
lcd.setCursor(1, 2);
lcd.print("Factoring Practice");
}
break;
case 2: //Find the Line of Symmetry
//Draw the equation
if (triggerHistory == 1) {
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Find the Line of Sym");
lcd.setCursor(0,1);
lcd.print(a);
lcd.print("x");
lcd.write(0);
if (b > 0) {
lcd.print("+");
}
if (b != 0) {
lcd.print(b);
lcd.print("x");
}
if (c > 0) {
lcd.print("+");
}
lcd.print(c);
lcd.print("=0");
}
//Reveal Answer
if ((digitalRead(answerPin) == HIGH) && (triggerHistory == 1)) {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("The Answer To:");
delay(2000); //The amount of time "The Answer To:" is displayed
lcd.setCursor(0,1);
lcd.print(a);
lcd.print("x");
lcd.write(0);
if (b > 0) {
lcd.print("+");
}
if (b != 0) {
lcd.print(b);
lcd.print("x");
}
if (c > 0) {
lcd.print("+");
}
lcd.print(c);
lcd.print("=0");
delay(1000); //The amount of time the equation is displayed
lcd.setCursor(7,2);
lcd.print("Is....");
delay(1000); //The amount of time "Is...." is displayed
lcd.setCursor(0, 3);
lcd.print("Sym Line = ");
if (int(lineOfSym) != lineOfSym) {
lcd.print(lineOfSym);
} else {
lcd.print(int(lineOfSym));
}
delay(6000); //The amount of time the answer is displayed
while (digitalRead(answerPin) == 1) {
delay(1000);
}
refresh = 1;
lcd.clear();
}
break;
case 3: //Find Vertex
//Draw the equation
if (triggerHistory == 1) {
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Find the Vertex");
lcd.setCursor(0,1);
lcd.print(a);
lcd.print("x");
lcd.write(0);
if (b > 0) {
lcd.print("+");
}
if (b != 0) {
lcd.print(b);
lcd.print("x");
}
if (c > 0) {
lcd.print("+");
}
lcd.print(c);
lcd.print("=0");
}
//Reveal Answer
if ((digitalRead(answerPin) == HIGH) && (triggerHistory == 1)) {
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("The Answer To:");
delay(2000); //The amount of time "The Answer To:" is displayed
lcd.setCursor(0,1);
lcd.print(a);
lcd.print("x");
lcd.write(0);
if (b > 0) {
lcd.print("+");
}
if (b != 0) {
lcd.print(b);
lcd.print("x");
}
if (c > 0) {
lcd.print("+");
}
lcd.print(c);
lcd.print("=0");
delay(1000); //The amount of time the equation is displayed
lcd.setCursor(7,2);
lcd.print("Is....");
delay(1000); //The amount of time "Is...." is displayed
lcd.setCursor(0, 3);
lcd.print("V = ");
if (int(lineOfSym) != lineOfSym) {
lcd.print(lineOfSym);
} else {
lcd.print(int(lineOfSym));
}
lcd.print(", ");
if (int(vertexY) != vertexY) {
lcd.print(vertexY);
} else {
lcd.print(int(vertexY));
}
delay(6000); //The amount of time the answer is displayed
while (digitalRead(answerPin) == 1) {
delay(1000);
}
refresh = 1;
lcd.clear();
}
break;
}
}
void modeChange() {
if (triggerHistory == 1) {
if (digitalRead(modePin) == HIGH) {
mode = mode + 1;
delay(500);
//Serial.println("Help");
}
if (mode >= 4) {
mode = 1;
}
}
}