Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid nonClick event #37

Open
thalesmaoa opened this issue Jun 21, 2021 · 0 comments
Open

Avoid nonClick event #37

thalesmaoa opened this issue Jun 21, 2021 · 0 comments

Comments

@thalesmaoa
Copy link

Hello, I've build an application using keypad and mount it inside a box. From time to time I receive a beep indicating a fail click in the matrix pad. In other words, there are some interference that I can't understand why. I've tried increasing debounce time. It get better, but still a problem from time to time. Can anyone point a way to improve my code?

It always detect "1" as button pressed.

#include <Arduino.h>
#include <Keypad.h>
#include <UIPEthernet.h>

#define CLIENT_ID "Pad-Senha"
#define RESET_DELAY   5000
#define TEMPO_PREALARME 2000

void buttonPress();
void resetBeep();
void passOk();
void sendMessage(char[]);
void checkMessage();

const uint password_1 = 123; // change your password here
const uint password_2 = 456;  // change your password here
const uint password_3 = 789;   // change your password here
const uint password_4 = 012;   // change your password here
const uint password_5 = 345;   // change your password here
String input_password;

long previousMillis;
long tempoprealarme;

byte start_timer = 0;

byte alarme = 0;
byte prealarme = 0;
byte alarme_sirene = 1;

const int BUZZER_PIN = 9;
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte pin_rows[ROW_NUM] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {6, 7, 8}; //connect to the column pinouts of the keypad
byte i;

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );

EthernetUDP udp;

void setup(){
  pinMode(BUZZER_PIN,OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
  input_password.reserve(32);

  Serial.begin(9600);

  uint8_t mac[6] = {0x00,0x02,0x02,0x03,0x04,0x05};

  Ethernet.begin(mac,IPAddress(192,168,200,177));

  int success = udp.begin(5555);

  Serial.print(F("initialize: "));
  Serial.println(success ? "success" : "failed");
  Serial.println(UIPEthernet.localIP());
  
  sendMessage("Sistema ligado");
  sendMessage("Aguardando envio de mensagem");
  sendMessage(UIPEthernet.localIP());

  keypad.setDebounceTime(270); 
}

void loop() {
  checkMessage();
  char key = keypad.getKey();

  if(start_timer){
    if(millis() - previousMillis > RESET_DELAY){
      resetBeep();
      input_password = "";
      start_timer = 0;
    }
  }
  
  if (key){
    Serial.println(key);
    start_timer = 1;
    previousMillis = millis();
    if(key == '*') {
      resetBeep();
      input_password = "";
      start_timer = 0; // reset the input password
    }else if(key == '#') {
      uint input_pass = input_password.toInt();
      switch(input_pass){
        case password_1:
          Serial.println("Pass 1");
          sendMessage("secret");
          passOk();
          break;
        case password_2:
          Serial.println("Pass 2");
          sendMessage("secret");
          passOk();
          break;
        case password_3:
          Serial.println("Pass 3");
          sendMessage("secret");
          passOk();
          break;
        case password_4:
          Serial.println("Pass 4");
          sendMessage("secret");
          passOk();
          break;
        case password_5:
          Serial.println("Pass 5");
          sendMessage("secret");
          passOk();
          break;
        default:
          Serial.println("Senha incorreta");
          resetBeep();
      }
      input_password = "";
      start_timer = 0;
    }else {
      input_password += key; // append new character to input password string
      buttonPress();
    }
  }
}

void sendMessage(char msg[]){
  int success;
    do{
      success = udp.beginPacket(IPAddress(192,168,200,254),5000);
    }while (!success);

    success = udp.print(msg);
    success = udp.endPacket();
    udp.stop();
    Serial.println(udp.begin(5000) ? "success" : "failed");
}

void checkMessage(){
  //check for new udp-packet:
  int size = udp.parsePacket();
  if (size > 0) {
    do{
      char* msg = (char*)malloc(size+1);
      int len = udp.read(msg,size+1);
      msg[len]=0;
      Serial.println(msg);
      
      if (strcmp(msg, "alarme") == 0){
        alarme = 1;
      }else if (strcmp(msg, "prealarme") == 0){
        prealarme = 1;
      }else if(strcmp(msg, "desabilitaalarme") == 0){
        alarme = 0;
        prealarme = 0;
      }
      free(msg);
    }while ((size = udp.available())>0);
    //finish reading this packet:
    udp.flush();
    //Serial.println(F("'"));
  }
}

void passOk(){
  int NOTE_SUSTAIN = 100;
  for(uint8_t nLoop = 0;nLoop < 2;nLoop ++){
    tone(BUZZER_PIN,NOTE_A5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_B5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_C5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_B5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_C5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_D5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_C5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_D5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_E5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_D5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_E5);
    delay(NOTE_SUSTAIN);
    tone(BUZZER_PIN,NOTE_E5);
    delay(NOTE_SUSTAIN);
  }
  noTone(BUZZER_PIN);
}

void resetBeep(){
  tone(BUZZER_PIN, 500, 200);
  delay(200);
  tone(BUZZER_PIN, 1200, 200);
  delay(200);
  tone(BUZZER_PIN, 300, 200);
  delay(200);
  tone(BUZZER_PIN, 1000, 200);
  delay(200);
  tone(BUZZER_PIN, 400, 200);
  delay(200);
  tone(BUZZER_PIN, 1100, 200);
  delay(200);
}

void buttonPress(){
  tone(BUZZER_PIN,NOTE_B5,200);
  delay(100);
  noTone(BUZZER_PIN);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant