Hello everyone. I am doing a door control system. I have a problem with my keypad 4 * 4.
When I put the code to make it work in my global program it does not work. The goal is that the user types a code, the arduino verifies if the code exists in the MySQL database. this part of SQL verification has not been done yet. For the moment I tested to see if the keypad works in my general code but it does not work. But when I try the Keypad outside my general code, it works.
I put you the general code afterwards.
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <SPI.h>
#include <RFID.h>
#include <Keypad.h>
#include <Password.h>
int led = 13; // the pin that the LED is atteched to
int sensor = 7; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
bool detected = false;
int buzzerPin = 3;
//mot de passe
Password pass = Password( "1234" );
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 33,32,31,30 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { 37,36,35,34, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte mac_addr[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
#define SS_PIN 9
#define RST_PIN 8
IPAddress server_addr(192,168,1,8); // IP of the MySQL *server* here
char user[] = "hello"; // MySQL user login username
char password[] = "hello"; // MySQL user login password
RFID rfid(SS_PIN, RST_PIN);
String requete, carte;
char INSERT_SQL[] = "SELECT prenom FROM maison_intelligente.utilisateurs WHERE carte = '";
// Left-over from original code, variables needed to read the ID of the tag
// Setup variables:
int serNum0;
int serNum1;
int serNum2;
int serNum3;
int serNum4;
EthernetClient client;
MySQL_Connection conn((Client *)&client);
void setup()
{
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
pinMode(buzzerPin, OUTPUT);
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600);
SPI.begin();
rfid.init();
while (!Serial); // wait for serial port to connect
Serial.println("\n\nRequete SQL:");
Serial.println(requete);
Ethernet.begin(mac_addr);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
Serial.println("Sucess connexion to DataBase!");
delay(1000);
}
else
Serial.println("Connection failed.");
}
void loop()
{
val = digitalRead(sensor);
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(100); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(100); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
detected = true;
delay(1000);
Serial.println("Presenter Carte: ");
}
}
bool card_found = false;
row_values *row = NULL;
if (rfid.isCard() && detected == true)
{
Serial.print("Numero de Carte : ");
if (rfid.readCardSerial())
{
Serial.print(rfid.serNum[0]);
serNum0 = rfid.serNum[0];
Serial.print(rfid.serNum[1]);
serNum1 = rfid.serNum[1];
Serial.print(rfid.serNum[2]);
serNum2 = rfid.serNum[2];
Serial.print(rfid.serNum[3]);
serNum3 = rfid.serNum[3];
Serial.print(rfid.serNum[4]);
serNum4 = rfid.serNum[4];
carte=String(serNum0)+String(serNum1)+String(serNum2)+String(serNum3)+String(serNum4);
requete = INSERT_SQL+String(carte)+"'";
delay(10000); // wait 10 seconds (or whatever you want if you change it)
// start from the beginning again
delay(1000);
//Serial.println("Recording data");
requete.toCharArray(INSERT_SQL, requete.length()+2);
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(INSERT_SQL);
column_names *columns = cur_mem->get_columns();
// Read the row (we are only expecting the one)
do {
row = cur_mem->get_next_row();
if (row != NULL) {
Serial.println(" ");
Serial.print("Bonjour ");
Serial.println(row->values[0]);
beep(100,50);
card_found = true;
}
keypad.getKey();
}
while (row != NULL);
if(!card_found){
Serial.println(" ");
Serial.println("Code Incorrect");
}
delete cur_mem;
// while(true){
//}
}
}
rfid.halt(); // close the stream
//vraie = beep(100,50);
//faux = beep(100,2000);
}
void beep(unsigned char delayms, unsigned char pause ) { //creating function
analogWrite(buzzerPin, delayms); //Setting pin to high
delay(pause); //Delaying
analogWrite(buzzerPin ,0); //Setting pin to LOW
delay(pause); //Delaying
}
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
//Serial.print("Pressed: ");
Serial.print(eKey);
switch (eKey){
case '*': checkPassword(); break;
case '#': pass.reset(); break;
default: pass.append(eKey);
}
}
}
void checkPassword(){
if (pass.evaluate()){
Serial.println("Success");
//Add code to run if it works
}else{
Serial.println("Wrong");
//add code to run if it did not work
}
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.