recognise only long keypress

Hello, I’m using the following code with a lcd, keypad and stepper.
I enter an amount of inches, and the stepper moves accordingly.

What i can’t get to work is this; I’m trying to recognize only long keypresses (any key)
and theres a beep on key released if keys werent pressed long enough

on compiling, it says: ‘keypadEvent’ was not declared in this scope
it is highlighted in the code, as well as the switch part that is iffy

#include <AccelStepper.h> // AccelStepper Library
#include <Keypad.h>  // Keypad Library
#include "U8glib.h"  // U8glib for Nokia LCD
#include <EEPROM.h> // internal memory

// Global variables
byte index = 0;
char numbers[20]; // Plenty to store a representation of a float


String set = "";  // Used for display on Nokia LCD

 #define motor A7 // Pin A7 connected to enbl- of driver
 #define Buzzer 46 // Pin 46 to + pin of buzzer
 
// Keypad Setup
const byte ROWS = 5; // Five Rows
const byte COLS = 4; // Four Columns
char keys[ROWS][COLS] = {
  {'F','G','#','*'},
  {'1','2','3','U'},
  {'4','5','6','D'},
  {'7','8','9','E'},
  {'L','0','R','N'}
};
byte rowPins[ROWS] = {22, 24, 26, 28, 30}; // Arduino pins connected to the row pins of the keypad
byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
Keypad mykeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );  // Keypad Library definition
 
 
// U8glib Setup for Nokia LCD
#define backlight_pin 11
U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7);  // Arduino pins connected to Nokia pins:
                                    // CLK=3, DIN=4, CE=6, DC=5, RST=7
                                    
                                    
// AccelStepper Setup
AccelStepper stepper(1, A2, A3);  // 1 = Easy Driver interface
                                  // Arduino A2 connected to STEP pin of Driver
                                  // Arduino A3 connected to DIR pin of Driver
                                  
 static char outstr[20];
 
 
void setup(void) {
  

  float len=000.000;
   float left=000.000;
   int eeAddress = 0; //EEPROM address to start reading from
  pinMode(motor, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  digitalWrite(motor, LOW); // motor driver  Off 
  digitalWrite(Buzzer, LOW); // buzzer  Off 
  [color=red]mykeypad.addEventListener(keypadEvent); // Add an event listener for this keypad[/color]
  mykeypad.setHoldTime(1000); //time to recognise long keypress 
 
  //  Light up the LCD backlight LEDS
  analogWrite(backlight_pin, 0);  // Set the Backlight intensity (0=Bright, 255=Dim)
    
  //  AccelStepper speed and acceleration setup
  stepper.setMaxSpeed(2000);  // max 3900 or you will have missed steps
  stepper.setAcceleration(800);  //  Same here

 // Draw starting screen on Nokia LCD
 
  EEPROM.get( eeAddress, len );

         dtostrf(len,7, 3, outstr);
         
           u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 35);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "STROKE LIMIT");
      u8g.drawStr(62, 29, "set");
      u8g.drawStr(62, 46, "pos");
      u8g.setPrintPos(4,46); 
      u8g.print(outstr);  //  Display current position of stepper
    }
      while( u8g.nextPage() );

   stepper.setCurrentPosition(len*3200); //  3200 steps for 1 inch 
}
 

void loop()
{
   char key = mykeypad.getKey();
   if(key != NO_KEY)

  [color=red]  // Taking care of some special events.
void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
    [/color]
    
        case PRESSED:
        
      if(key == 'E')
      { 
         digitalWrite (Buzzer, HIGH) ;// Buzzer On
        delay (100) ;// Delay 100ms 
        digitalWrite (Buzzer, LOW) ;// Buzzer Off
        
      }
      else if(key == '*')
      {
         
        digitalWrite (Buzzer, HIGH) ;// Buzzer On
        delay (100) ;// Delay 100ms 
        digitalWrite (Buzzer, LOW) ;// Buzzer Off
         
      }
      else if(key >= '0' && key <= '9')
      {
        
        digitalWrite (Buzzer, HIGH) ;// Buzzer On
        delay (100) ;// Delay 100ms 
        digitalWrite (Buzzer, LOW) ;// Buzzer Off
         
      }
      else if(key == 'N')
      {
       
        digitalWrite (Buzzer, HIGH) ;// Buzzer On
        delay (100) ;// Delay 100ms 
        digitalWrite (Buzzer, LOW) ;// Buzzer Off
        
          }
       
      

      
       
      
       else if(key == 'F')
      { digitalWrite (Buzzer, HIGH) ;// Buzzer On
        delay (100) ;// Delay 100ms 
        digitalWrite (Buzzer, LOW) ;// Buzzer Off
        
      }
   
   break;

    case HOLD 
   
      if(key == 'E')
      {
        stuff happens
      }
      else if(key == '*')
      {
        stuff happens
         
      }
      else if(key >= '0' && key <= '9')
      {
         numbers[index++] = key;
         numbers[index] = '\0';
         set = String(numbers);

        stuff happens
         
      }
      else if(key == 'N')
      {
        stuff happens
      
       else if(key == 'F')
      {
        stuff happens
      }
      }
      
}
}           
 
 void drawnokiascreen(String set) {

    u8g.firstPage();
    do {
      u8g.drawHLine(0, 15, 84);
      u8g.drawVLine(50, 16, 35);
      u8g.drawHLine(0, 35, 84); 
      u8g.setFont(u8g_font_profont11);
      u8g.drawStr(0, 10, "STROKE LIMIT");
      u8g.setPrintPos(0,29);
      u8g.print(set);  // Put entered number on Nokia lcd    
      u8g.drawStr(62, 29, "set");
      u8g.drawStr(62, 46, "pos");
     u8g.setPrintPos(4,46); 
      u8g.print(outstr);  //  Display current position of stepper
    }
      while( u8g.nextPage() );






}

You can't define a function inside another function.

Your definition of keypadEvent is inside the loop function.

I've based the keypadevent on the arduino ide examples.

every key entry in code is with if/else, and i've duplicated it into categories released and hold.

would it be better if every key entry looked like this?

if(key == 'E')
      { if 
        case RELEASED
         digitalWrite (Buzzer, HIGH) ;// Buzzer On
        delay (100) ;// Delay 100ms 
        digitalWrite (Buzzer, LOW) ;// Buzzer Off

      else if
       case HOLD
        do something
        
      }

i still have trouble having the program to "recognise" wether it is released or hold

would it be better if every key entry looked like this?

No, that is not even acceptable as "pseudocode". You need to brush up on syntax of switch case and if statements.

If you want the program to be responsive, don't use delay().

To determine the length of time that a key was pressed, or to detect a long press, you need to start a millisecond timer when it is first pressed and read out that timer either at regular intervals while still pressed, or when the key is released.

Maybe a simpler way, is it possible to ask that a key is "pressed" only if it has been pressed for, lets say 100ms ?

adding >(100)

to:

char key = mykeypad.getKey();
   if(key != NO_KEY)
   {

so that it only recognises a keypress if it has been pressed for 100ms?

Where do you think the number that would be > 100 would come from?

Google "arduino detect long key press".

Copy pasting has also gained you on the keypadEvent line

GypsumFantastic:
You can't define a function inside another function.

Your definition of keypadEvent is inside the loop function.

Ah!

thank you, now it works!