Hi, guys!
I made this quadratic equation solver which solves the equation based on three data, a, b and c.
Those are typed in with a 3 column, 4-row keypad, and the two solutions are written on a 16*2 LCD screen.
The code is here:
#include <Key.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
const int rs = 13, en = 12, d4 = 11, d5 = 10, d6 = 9, d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {6, 5, 4, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 1, 0}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int myarray[3]={0,0,0};
float myarray2[10]={0,0,0,0,0,0,0,0,0,0};
int num=0;
int xf=0;
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("a=");
lcd.setCursor(5,0);
lcd.print("b=");
lcd.setCursor(10,0);
lcd.print("c=");
}
void loop() {
lcd.setCursor(2,0);
myarray[1] = GetNumber();
lcd.print(myarray[1]);
myarray[1] = negativ(myarray[1],2,0);
lcd.setCursor(7,0);
myarray[2] = GetNumber();
lcd.print(myarray[2]);
myarray[2] = negativ(myarray[2],7,0);
lcd.setCursor(12,0);
myarray[3] = GetNumber();
lcd.print(myarray[3]);
myarray[3] = negativ(myarray[3],12,0);
LepTetes(myarray[1], myarray[2], myarray[3]);
vege();
}
int GetNumber()
{
num = 0;
xf = 0;
while (xf==0)
{
char key1 = kpd.getKey();
switch (key1)
{
case NO_KEY:
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
num = num * 10 + (key1 - '0');
break;
case '#':
xf=1;
return num;
break;
case '*':
num = 0;
break;
}
}
}
int LepTetes(int a, int b, int c)
{
xf = 0;
while (xf==0)
{
char key2 = kpd.getKey();
switch (key2)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '*':
break;
case '#':
myarray2[0]=b*-1;
myarray2[2]=sq(b);
myarray2[3]=4*a*c;
myarray2[1]=myarray2[2]-myarray2[3];
if(myarray2[1]<0)
{
lcd.setCursor(0,1);
lcd.print(" No solution");
xf=1;
break;
}
else
{
myarray2[4]=sqrt(myarray2[1]);
myarray2[5]=myarray2[0]+myarray2[4];
myarray2[7]=2*a;
myarray2[6]=myarray2[5]/myarray2[7];
myarray2[8]=myarray2[0]-myarray2[4];
myarray2[9]=myarray2[8]/myarray2[7];
lcd.setCursor(0,1);
lcd.print("x1=");
lcd.setCursor(3,1);
lcd.print(myarray2[6]);
lcd.setCursor(8,1 );
lcd.print("x2=");
lcd.setCursor(11,1);
lcd.print(myarray2[9]);
xf=1;
break;
}
break;
}
}
}
int negativ(int bejovo, int oszlop, int sor)
{
xf = 0;
while (xf==0)
{
char key3 = kpd.getKey();
switch (key3)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
xf=0;
break;
case'#':
xf=1;
return bejovo;
break;
case'*':
bejovo=-1*bejovo;
lcd.setCursor(oszlop,sor);
lcd.print(bejovo);
xf=1;
return bejovo;
break;
}
}
}
int vege()
{
xf=0;
while (xf==0)
{
char key6 = kpd.getKey();
switch (key6)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case'*':
break;
case'#':
xf=1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("a=");
lcd.setCursor(5,0);
lcd.print("b=");
lcd.setCursor(10,0);
lcd.print("c=");
break;
}
}
}
Do you got any idea how could be the code more simple?