Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Arduino Error 'keypadEvent' was not declared in this scope

Status
Not open for further replies.

dann11

Full Member level 3
Joined
Oct 22, 2015
Messages
166
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,725
Hi! I am trying to use the keypad.addEventListener() to call an event I did use it with keypadEvent,and then compiling my code gives me an Error 'keypadEvent' was not declared in this scope. Can you please help me where might have I gone wrong.
Code:
#include <Keypad.h>

//pin assignments
int latchPin = 15;                    
int clockPin = 14;                    
int dataPin = 16;                     
int keySwitch = 9;                   
int ledActual = 17;                   
int ledTarget = 18;                  
int ledTactTime = 19;                



//global variables
byte digitBuffer[6] = {0};

int digitScan = 1;

byte valueKeypad = 0;

byte num = 0;

//keypad
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'#','0','*','D'},
};
byte rowPins[ROWS] = {0 , 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 5, 6, 7};//nnect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );



//display 0,1,2,3,4,5,6,7,8,9:
byte datArray[10] = {B01111101, B00100001, B01010111, B01110011, B00101011, B01111010, B01111110, B01100001, B01111111, B01111011};

void setup()
{
 //Serial.begin(9600);

  //pin designation
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  pinMode(keySwitch, INPUT);
  pinMode(ledActual, OUTPUT);
  pinMode(ledTarget, OUTPUT);
  pinMode(ledTactTime, OUTPUT);



  //initialize display
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin,clockPin, MSBFIRST, datArray[0]);
  shiftOut(dataPin,clockPin, MSBFIRST, datArray[0]);
  shiftOut(dataPin,clockPin, MSBFIRST, datArray[0]);
  shiftOut(dataPin,clockPin, MSBFIRST, datArray[0]);
  digitalWrite(latchPin, HIGH); //pull the latchPin to save the data

  keypad.addEventListener(keypadEvent);
 }

void keypadEvent(KeypadEvent key)
{
  if (keySwitch == LOW)
  {
  switch(keypad.getState())
  {
    case PRESSED:
        switch(key);
          {
              case 'A':
                   digitalWrite(ledActual, HIGH);
                   //transmit data to the display - Actual Value
              break;

              case '3':
                   digitalWrite(ledTarget, HIGH);
                   //transmit data to the display - Target Value
              break;

              case '2':
                   digitalWrite(ledTactTime, HIGH);
                   //transmit data to the display - Time Value
              break;

              case '1';
                    digitalWrite(ledActual, LOW);
                    digitalWrite(ledTarget, LOW);
                    digitalWrite(ledTactTime, LOW);
                   //save the Actual Value to the Actual Variable
              break;
          }
    break;

    case RELEASED:
        switch(key);
          {
              case 'A':
                   digitalWrite(ledActual, HIGH);
                   keypadAction();
                   //transmit data to the display - Actual Value
              break;

              case '3':
                   digitalWrite(ledTarget, HIGH);
                   keypadAction();
                   //transmit data to the display - Target Value
              break;

              case '2':
                   digitalWrite(ledTactTime, HIGH);
                   keypadAction();
                   //transmit data to the display - Time Value
              break;

              case '1';
                   digitalWrite(ledActual, LOW);
                   digitalWrite(ledTarget, LOW);
                   digitalWrite(ledTactTime, LOW);
                   //save the Actual Value to the Actual Variable
              break;
          }
    break;

    case HOLD:
        switch(key);
          {
              case 'A':
                   digitalWrite(ledActual, HIGH);
                   //transmit data to the display - Actual Value
              break;

              case '3':
                   digitalWrite(ledTarget, HIGH);
                   //transmit data to the display - Target Value
              break;

              case '2':
                   digitalWrite(ledTactTime, HIGH);
                   
              break;

              case '1';
                   digitalWrite(ledActual, LOW);
                   digitalWrite(ledTarget, LOW);
                   digitalWrite(ledTactTime, LOW);
                   
              break;
          }
    break;
  }
  }
}
 

srizbf

Advanced Member level 5
Joined
Apr 3, 2010
Messages
1,992
Helped
417
Reputation
840
Reaction score
328
Trophy points
1,363
Activity points
10,985
add
void loop()
{
//the keypad case statements here...
}
to your code module.
 

srizbf

Advanced Member level 5
Joined
Apr 3, 2010
Messages
1,992
Helped
417
Reputation
840
Reaction score
328
Trophy points
1,363
Activity points
10,985
What happens when you write

keypad.addEventListener(keypadEvent);

as
within the void loop()
{
keypad.addEventListener(keypadEvent);
........
}

and remove it from setup()
 

    V

    Points: 2
    Helpful Answer Positive Rating

dann11

Full Member level 3
Joined
Oct 22, 2015
Messages
166
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,725
I have made it worked. It just re-arranged each function. Thanks for the help anyway.
 

dann11

Full Member level 3
Joined
Oct 22, 2015
Messages
166
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Activity points
2,725
Arduino "no_Key" can it be used in multiple function

Hi! I am trying to used the "no_key" of arduino keypad with multiple function, as the first function is called, it works perfectly, but when the next function(also uses the "no_key") is called keypad is no longer responding. Can the "no_key" be used in multiple function?

first function:
Code:
void keypadPress()
{
  
  char key = keypad.getKey();

  if(keypadActionFlag == true)
   {
      if(key != NO_KEY)
         {

            if (key == 'A')
              {
                  
                  digitalWrite(ledActual, HIGH);
                  digitalWrite(ledTarget, LOW);
                  digitalWrite(ledTactTime, LOW);
        
                  keypadActualFlag = true;
        
                  //transmit data to the display - Actual Value
              }

           else if (key == '3')
              {
                
                digitalWrite(ledTarget, HIGH);
                digitalWrite(ledActual, LOW);
                digitalWrite(ledTactTime, LOW);
        
                keypadTargetFlag = true;
       
                //transmit data to the display - Target Value
              }

          else if (key == '2')
              {
 
                digitalWrite(ledTactTime, HIGH);
                digitalWrite(ledActual, LOW);
                digitalWrite(ledTarget, LOW);
                
                keypadTactTimeFlag = true;
        
                //transmit data to the display - Time Value
              }
   
         else if (key == '1')
              {
                digitalWrite(ledActual, LOW);
                digitalWrite(ledTarget, LOW);
                digitalWrite(ledTactTime, LOW);
                
                keypadActualFlag = false;
                keypadTargetFlag = false;
                keypadTactTimeFlag = false;
                
                //save the Actual Value to the Actual Variable    
              }

          else
              {
                  //do nothing
              }
  
    }
   }

   else
   {
    //do nothing
   }
}

2nd function:
Code:
void keypadAction()
{
if ((keypadActualFlag == true) || (keypadTargetFlag == true) || (keypadTactTimeFlag == true))
{
  
  char keyPad = keypad.getKey();
  
  //keypad printing of value
  if (keyPad != NO_KEY)
  {
   
       if (keyPad == 'D')
          {
              //delay(300);
              num = datArray[1];
              // Serial.println(num);
              digitalWrite(latchPin, LOW); 
              Serial.println(num);
              Serial.println("Keypress");
          }
        
        
        else if (keyPad == 'C')
          {
              // delay(300);
              num = datArray[2];
              digitalWrite(latchPin, LOW);
              Serial.println(num);
          }
        
    
        else if (keyPad == 'B')
          {
              //  delay(300);
              num = datArray[3];
              digitalWrite(latchPin, LOW);
              Serial.println(num);
          }
        
    
        else if (keyPad == '*')
          {
              num = datArray[4];
              Serial.println(num);
              digitalWrite(latchPin, LOW);
              // delay(300);
          }
        
    
        else if (keyPad == '9')
          {
               //delay(300);
               num = datArray[5];
               Serial.println(num);
               digitalWrite(latchPin, LOW);
          }
         
     
         else if (keyPad == '6')
          {
              // delay(300);
              //  delay(200);
              num = datArray[6];
              Serial.println(num);
              digitalWrite(latchPin, LOW);
          }
         

         else if (keyPad == '0')
          {
              num = datArray[7];
              Serial.println(num);
              digitalWrite(latchPin, LOW);
          }
         
    
         else if (keyPad == '8')
          {
              num = datArray[8];
              Serial.println(num); 
              digitalWrite(latchPin, LOW); 
          }
         
    
         else if (keyPad == '5')
          {
              num = datArray[9];
              digitalWrite(latchPin, LOW);
              Serial.println(num);
          }
         
         
        else if (keyPad == '7')
          {
              // delay(300);
              num = datArray[0];
              digitalWrite(latchPin, LOW);
              Serial.println(num);
          }
         

         else if (keyPad == '4')
          {
              digitScan = 0;
    
              digitalWrite(latchPin, LOW);
              shiftOut(dataPin,clockPin, MSBFIRST, datArray[0]);
              digitalWrite(latchPin, HIGH); //pull the latchPin to save the data
              //delay(250); //wait for a second
         }
        
  
        else if (keyPad == '#')
          {
              digitScan = 0;
    
              digitalWrite(latchPin, LOW);
              shiftOut(dataPin,clockPin, MSBFIRST, datArray[0]);
              digitalWrite(latchPin, HIGH); //pull the latchPin to save the data
              //delay(250); //wait for a second
          }

         else
         {
          //do nothing
         }

       
  

    digitBuffer = num;
  
    displayValue();

  
  }

 /* else
  {
    Serial.println("keypadPress else");
  }*/
}

else
{
  // do nothing
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top