Simplifying code

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?

For goodness sake write a function to read the keypad and return a number rather than repeating the same code over and over again.

You could remove all the unnecessary blank lines.

And use the AutoFormat tool to give you consistent indentation.

...R
Planning and Implementing a Program

UKHeliBob:
For goodness sake write a function to read the keypad and return a number rather than repeating the same code over and over again.

Do you mean uniting the GetNumber function with the function that makes the number negative?

Do you mean uniting the GetNumber function with the function that makes the number negative?

I had more in mind the repetitive use of kpd.getKey(); and switch/case

Also in your switch() block - explore the default: case.
That will eliminate all the faff about :

case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '*':

You have intricately and confusing mixed three separate activities together that should be
kept separate.

  1. Read the input from the keyboard.
  2. Calculating the quadratic solution
  3. Displaying results

Separate them from each other. Put all code into small well named functions, not in loop()

Test each individually, much easier to test individually, much easier to just look at the code
and see its correct too.

Give your intermediate results sensible names, not array indices. Alternatively use a separate
function for each intermediate result (again sensibly named).

What do you think about this?

#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 num = 0;
int xf = 0;
int xe = 0;
int xr = 0;
int xt = 0;

int a = 0;
int b = 0;
int c = 0;

float e = 0; //b*b-4ac
float j = 0;
float m = 0;

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

  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() {
  // put your main code here, to run repeatedly:

  a = GetNumber;

  lcd.setCursor(2, 0);
  lcd.print(a);

  b = GetNumber;

  lcd.setCursor(7, 0);
  lcd.print(b);

  c = GetNumber;

  lcd.setCursor(12, 0);
  lcd.print(c);

  calculate(a, b, c);
}


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;
        xe = 0;
        while ( xe == 0)
        {
          char key2 = kpd.getKey();
          switch (key2)
          {
            default:
              xe = 0;
              break;

            case '*':

              num = -num;
              xe = 1;
              return num;
              break;

            case '#':

              xe = 1;
              return num;
              break;
          }
        }
        break;
    }
  }
}

int calculate(int x, int y, int z)
{
  xr = 0;

  while (xr == 0);
  {
    char key3 = kpd.getKey();

    switch (key3)
    {
      default:

        xr = 0;
        break;

      case '#':

        e = sq(x) - (4 * y * z);

        if (e < 0)
        {
          lcd.setCursor(0, 1);

          lcd.print("  Nincs megoldas");

          xr = 1;

          break;
        }

        else
        {
          j = (-x) + sqrt(e);

          m = (-x) - sqrt(e);

          lcd.setCursor(0, 1);

          lcd.print("x1=");

          lcd.setCursor(3, 1);

          lcd.print(j);

          lcd.setCursor(8, 1 );

          lcd.print("x2=");

          lcd.setCursor(11, 1);

          lcd.print(m);

          xr = 1;

          xt = 0;

          while (xt == 0)
          {
            char key5 = kpd.getKey();

            switch (key5)
            {
              default:

                xt = 0;

                break;

              case '#':

                lcd.clear();

                lcd.setCursor(0, 0);

                lcd.print("a=");

                lcd.setCursor(5, 0);

                lcd.print("b=");

                lcd.setCursor(10, 0);

                lcd.print("c=");

                xt = 1;

                break;

            }

            break;
          }
          break;
        }
    }
  }

}