Pointers on How to Combine Knock Sketch with Password and Keypad Sketch

Hey,

First the compulsive disclaimer. I’m definitely a newbie.

I’ve been trying to combine the knock sketch in Arduino with the 4x3 keypad and Password sketches and libraries. I can get both sketches working independently, and as a test I wire up one piezo sensor to analogRead(A0) and I get a reading on the serial monitor converting it to char ‘4’. I knock it four times, getting the serial monitor to write “4444” and then I test the password(which is what it’s been set as). It still denies it.

I’m guessing writing to the monitor isn’t read by the password sketch, but I’'m really stumped on how to do it.

I’d be really appreciative if someone can send me in the right direction on what I should read up on, specifically, I know there’s big gaps in my knowledge, but right now I’m looking to understand why this isn’t working.

I’ve attached the current code below:

#include <Password.h> 
#include <Keypad.h> 

// these constants won't change:
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 50;  // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor piezo

Password password = Password( "4444" ); //27478622 = cristobal

const byte ROWS = 3; // 
const byte COLS = 2; // 
// 
char keys[ROWS][COLS] = {
{'1','2'},
{'3','4'},
{'*','#'},
};
//
byte rowPins[ROWS] = { 10, 9, 8 };//
byte colPins[COLS] = { 7, 6 };

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

void setup(){
  Serial.begin(9600);
  Serial.write(254);
  Serial.write(0x01);
  delay(200); 
  
  keypad.addEventListener(keypadEvent); //
  }

void loop(){
  sensorReading = analogRead(knockSensor);
  if (sensorReading >= threshold) {
    digitalWrite(12, HIGH);
    delay(1000);
    digitalWrite(12, LOW);
    digitalWrite(9, LOW);
    digitalWrite(6, HIGH);
    Serial.write('4');
    
}
 
  keypad.getKey();

}
  
  void keypadEvent(KeypadEvent eKey){
  
  switch (keypad.getState()){
  case PRESSED:
  digitalWrite(13, HIGH); // lights up a led to confirm a key press
  delay(500);             // holds if for half a second
  digitalWrite(13, LOW);  // turns if off again
  Serial.print("Enter:");
  Serial.println(eKey);
  delay(10);
  

  
  Serial.write(254);


  
  switch (eKey){
    case '*': checkPassword(); delay(1); break;
    
    case '#': password.reset(); delay(1); break;
    
     default: password.append(eKey); delay(1);  
}
}
}
void checkPassword(){
  
if (password.evaluate()){  //
    
    Serial.println("Accepted");
    Serial.write(254);delay(100);
    //
  
    
        digitalWrite(11, HIGH);// PUT WHATEVER YOU WANT TO TRIGGER ONCE PASSWORD IS CORRECT
       
    delay(10000); //
    digitalWrite(11, LOW);// TURN IT OFF AFTER A DELAY
   
    
    
}else{
    Serial.println("Denied"); //
    Serial.write(254);delay(10);
    //
   
    digitalWrite(12, HIGH); //FAILURE ACTION.
    delay(500); //
    digitalWrite(12, LOW);// END OF FAILURE ACTION.
    
}
}

Hope someone can help.

-Tom Erik

and I get a reading on the serial monitor converting it to char ‘4’.

Converting what to char ‘4’?

I’m guessing writing to the monitor isn’t read by the password sketch

Nothing you send out is read by anything that reads what comes in.

but I’'m really stumped on how to do it.

Forget that you can even use Serial.print(). The data that you print is what is important, NOT the fact that you print it.

  Serial.write(254);
  Serial.write(0x01);

What, EXACTLY, are you sending this to? Why?

  keypad.addEventListener(keypadEvent); //

Just how much value does THAT comment add?

  keypad.getKey();

Why? If you don’t give a rats ass what key is pressed, why are you asking what key is pressed? If you did care, you’d be saving the returned value.

Thanks for your response.

To address the things you pointed out, I've answered below:

I'm guessing writing to the monitor isn't read by the password sketch
Nothing you send out is read by anything that reads what comes in.

Got it! That answers one of many misperceptions I had.

but I''m really stumped on how to do it.
Forget that you can even use Serial.print(). The data that you print is what is important, NOT the fact that you print it.

Again. Got it. Thanks.

Serial.write(254);

Serial.write(0x01);



What, EXACTLY, are you sending this to? Why?

I was hoping to send information to what ever processes the inputs on the keyboard sketch.

In other words, when I press "a key" on a keypad it provides a signal between two digital pin slots on the Arduino, but I don't know what it further does. If the password or keypad sketch stores that information as an integer or character, and then stores that as a string or an array to futher more be assessed whether the password is correct. I was hoping to bypass the button pressing, and instead have an integer saved prompt what ever it is the sketch needs to register as the same action as a button press.

Code: [Select]
keypad.addEventListener(keypadEvent); //

Just how much value does THAT comment add?

Code: [Select]
keypad.getKey();

Why? If you don't give a rats ass what key is pressed, why are you asking what key is pressed? If you did care, you'd be saving the returned value.

I have no idea what the above codes do. They're there in the original keypad sketch. I didn't write those lines.