1

I've been trying, for over a week now, to create a program that will store my keypad presses into variable, and with a press of an enter, it will switch to the next variable to store the presses. The program is made to load addresses (mainly IP addresses) to different devices over serial connections. Because there are different types of addresses, each time the user press enter in the keypad (made the # button to a '\r' char) the program needs to switch to the next type of address and get the key presses from the keypad and store them until the next '\r' press. My problem is that I don't know how to store those addresses in the Arduino, and switch to a different "char" (I used char because I didn't knew how to convert them to store key presses into string) every '\r' press. I've included the handler for this part of my code. This is my first post here so if anything is missing let me know. thanks ahead.

bool handlerManualEntry( void )
{
    static uint8_t stateManualEntry = statesFunc::ST_INIT;
       
    char keyval = ReadKey();
    switch( stateManualEntry )
    {
        case    statesFunc::ST_INIT:
            //do whatever inits you need here (screen, variables etc)
                      
            stateManualEntry = statesFunc::ST_RUN;
           
        break;

        case    statesFunc::ST_RUN:
            //is there a keypress?
            if( keyval != NO_KEY )
            {
                switch( keyval )
                {
                  
                    case    '\r':
                        //when we get a return, send it to the serial port                   
                        Serial.print( '\n' );
                        u8g2log.print('\f');
                        for(int i=0; i<6; i++)
                        {
                          if(i == 0)
                          {
                          Serial.print(ethernetIP);
                          }
                        else if(i == 1)
                          {
                          Serial.print(ethernetSubnet);
                          }
                        else if(i == 2)
                          {
                          Serial.print(ethernetGetway);              
                          }
                        else if(i == 3)
                          {
                          Serial.print(vmeIP);
                          }
                        else if(i == 4)
                          {
                          Serial.print(vmeSubnet);
                          }
                        else if(i == 5)
                          {
                          Serial.print(eventPort);
                          }
                        else if(i == 6)
                          {
                          Serial.print(timeOut);
                          }
                        }
                        //and finish out by returning the state to INIT and returning true
                        //stateManualEntry = statesFunc::ST_INIT;
                        //return true;
                       
                    break;
                    case 'D':                    
                          u8g2log.print('N');
                          Serial.print('N');                                                   
                    break;

                         
                    case 'C':                  
                          u8g2log.print('Y');
                          Serial.print('Y');                         
                    break;
       
                    default:
                        //for any other key, print to the serial port and LCD
                        u8g2log.print(keyval);
                        
                        enterAddresses();   
                    break;
                   
                }
               
            }
           
        break;

    }

    return false;

}

void enterAddresses()
{
  int x;
  if(keyval != '\r')
  {
    if(pointer == 1)
    {

      keyval = keypad.getKey();
      u8g2log.print(keyval);
      ethernetIP += keyval;
    }
    else if(pointer == 2)
    {
      keyval = keypad.getKey();
      u8g2log.print(keyval);
      ethernetSubnet += keyval;
      }
    }
    else if(pointer == 3)
    {

      keyval = keypad.getKey();
      u8g2log.print(keyval);
      ethernetGetway += keyval;
                
    }
    else if(pointer == 4)
    {
      keyval = keypad.getKey();
      u8g2log.print(keyval);
      vmeIP += keyval;
    }
    else if(pointer == 5)
    {
        keyval = keypad.getKey();
      u8g2log.print(keyval);
      vmeSubnet += keyval;
    }
    else if(pointer == 6)
    {
      keyval = keypad.getKey();
      u8g2log.print(keyval);
      eventPort += keyval;
    }
    else if(pointer == 7)
    {

      keyval = keypad.getKey();
      u8g2log.print(keyval);
      timeOut += keyval;

    }
    pointer++;
    u8g2log.print('\f');
  
}

char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'.','0','\r','D'}
};

byte rowPins[ROWS]={39,41,43,45};
byte colPins[COLS]={31,33,35,37};
    
//Create an object of keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char keyval;
2
  • please format all of your code, not just parts of it ... it is unreadable the way you presented it
    – jsotola
    Nov 21 '20 at 18:37
  • what is the reason for using for(int i=0; i<6; i++) to run seven Serial.print() commands in sequence?
    – jsotola
    Nov 21 '20 at 18:42
0
  • Consider constraining the user and simplifying your code.

Simplify your code by putting rules on how the IP address is entered. When entering the IPv4 address in dot-decimal notation require that all 3 digits are always used for each of the four quad-dotted notation values. For example, the IPv4 address 192.168.10.20 would be entered as "192168010020".

  • Eliminate unnecessary keypad buttons.

The only keys required for the above scheme are 0, 1, 2, ..., 9. No other keys need to be detected or supported by additional software.

  • Start simple, get that working then add complexity.

Essentially, a state machine is most often used as the underlying coding pattern when interfacing with a user. Keeping track of what the user has done, the current state the user is in and where the user may go is the job of the state machine. To implement the above, the state machine will need to be complex enough to track the 1s, 10s & 100s position of each quad-dotted notation values as well as which quad-dotted notation value is currently being entered. This is complex enough for a first pass. Later, in subsequent passes, consider adding an "abort" key for when the user feels a new attempt is in order. Still later, consider adding a "back space" key. Finally, consider adding an "enter" key and dripping the 3 digit per quad-dotted notation value requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.