Instantiate class in a library by method in arduino

1

I have this line of code, and in it I have 2 methods, where one sets the keypad and another one takes the key pressed.

void Maleta::setKeypad(int r1, int r2, int r3, int r4, int c1, int c2, int c3, int c4){
  const byte numRows= 4; // Numero de linhas
  const byte numCols= 4; // Numero de colunas

  char keymap[numRows][numCols]=
  {
   {'1','2','3','A'},
   {'4','5','6','B'},
   {'7','8','9','C'},
   {'*','0','#','D'},
  };

  byte rowPins[numRows] = {r1,r2,r3,r4}; // Pinos digitais onde as linhas estao conectadas
  byte colPins[numCols] = {c1,c2,c3,c4}; // Pinos digitais onde as colunas estao conectadas


  Keypad myKeypad = Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
}
char Maleta::getKeyPress(){
  char keypressed = myKeypad.getKey();
  return keypressed;
}

This is the error that returns:

  

C: \ Users \ Luca \ Documents \ Arduino \ libraries \ Suitcase \ Suitcase.cpp: In member   function 'char Case: getKeyPress ()':

     

C: \ Users \ Luca \ Documents \ Arduino \ libraries \ Suitcase \ Suitcase.cpp: 32: 21:   error: 'myKeypad' was not declared in this scope
  char keypressed = myKeypad.getKey ();

    
asked by anonymous 15.09.2015 / 19:29

2 answers

0

The problem is occurring because the myKeypad variable can not be accessed within getKeyPress since it is not visible in that scope.

Note that the variable is declared in another method, called setKeypad . The scopes for the getKeyPress and setKeypad methods are different. If myKeypad was a global variable, then it could be accessed because it would be visible in all scopes of the program. However, global variables, as a general rule, should be avoided.

    
13.10.2015 / 20:36
0
char Maleta::getKeyPress(){
  //Exemplo Maleta2 mykeypad; voce teria declarada o mykeypad.
  char keypressed = myKeypad.getKey();// assim você chama mykeypad depois o membro .getKey()
  return keypressed;
}
    
14.12.2016 / 18:19