Witam
Na początku chciałbym się przywitać, ponieważ to mój pierwszy post.
Pracuję nad projektem dostępu do pomieszczeń (chwilowo na diodach):
Arduino Yun połączone z Adafruit PN532 (kontroler NFC/RFID) i klawiaturą 4x4.
Udało się metodą kopiuj-wklej z drobnymi zmianami uzyskać działające świecenie diodą poprzez wpisanie poprawnego/niepoprawnego kodu z klawiatury a także tą samą funkcjonalność z wykorzystaniem czytnika rfid/nfc.
Problem pojawił się w momencie próby połączenia tych dwóch programów w jeden.
I widzę tu dwie możliwości działania programu:
1) Program cały czas odczytuje dane z czytnika rfid/nfc i jednocześnie z klawiatury
2) Program domyślnie odczytuje dane z czytnika rfid/nfc, a w momencie naciśnięcia przycisku (albo na klawiaturze, albo fizycznego na płytce stykowej "tact switch 2 pin") przełącza na klawiaturę(albo na jakiś czas np. 15 sekund, albo na stałe z możliwością powrotu tym samym przyciskiem do czytnika rfid/nfc)
Proszę o pomoc, bo nie bardzo wiem jak się do tego zabrać.
Kod czytnika rfid/nfc
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
int dioda_zielona = 12; //pin12 dioda zielona
int dioda_czerwona = 11; //pin11 dioda czerwona
#define PN532_IRQ (8) //Z arduino Yun: od strony PN532 podpinamy do 2 pinu, od strony Arduino podpinamy do portu 8
#define PN532_RESET (3) ///nie podpiete
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); ///sterowanie po I2C
void setup(void) {
pinMode(dioda_zielona,OUTPUT); // Definiujemy diodę_zieloną
pinMode(dioda_czerwona,OUTPUT); // Definiujemy diodę_czerwona
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion(); // sprawdzanie firmware czytnika rfid/nfc
if (! versiondata) { // jeżeli nie jest to PN532
Serial.print("Didn't find PN53x board"); // Wypisuje komunikat
while (1); // I przestaje dzialac
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); ///Jeżeli poprawny czytnik
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); //wyświetla wersje firmware czytnika (przed kropką)
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); // PO KROPCE
nfc.setPassiveActivationRetries(0xFF); // Set the max number of retry attempts to read from a card This prevents us from waiting forever for a card, which is the default behaviour of the PN532.
nfc.SAMConfig(); // configure board to read RFID tags
Serial.println("Waiting for an ISO14443A card"); ///Napis początkowy
}
uint8_t karta1[4] ={0x91,0xB1,0xF3,0xCF}; // definiujemy dane karty nr.1
void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
Serial.print(" 0x");Serial.print(uid[i], HEX);
if (uid[i] == karta1[i]) //jeżeli karta jest dobra
{
digitalWrite(dioda_zielona,HIGH);
delay(500);
Serial.print("Dziala");
digitalWrite(dioda_zielona,LOW);
}
if (uid[i] != karta1[i]) /////jeżeli karta jest zła
{
digitalWrite(dioda_czerwona,HIGH);
delay(500);
Serial.print("Nie dziala");
digitalWrite(dioda_czerwona,LOW);
}
}
Serial.println("");
// Wait 1 second before continuing
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
Serial.println("Timed out waiting for a card");
}
}
Kod klawiatury
#include <Password.h>
#include <Keypad.h>
int dioda_zielona = 12; //pin12 dioda zielona
int dioda_czerwona = 11; //pin11 dioda czerwona
Password password = Password( "1234" );
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = { 7,6,5,4 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = { A3,A2,A1,A0, };// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
pinMode(dioda_zielona,OUTPUT); // Definiujemy diodę_zieloną
pinMode(dioda_czerwona,OUTPUT); // Definiujemy diodę_czerwona
Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop(){
keypad.getKey();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
Serial.print("Pressed: ");
Serial.println(eKey);
switch (eKey){
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword(){
if (password.evaluate()){
Serial.println("Success");
digitalWrite(dioda_zielona,HIGH);
delay(500);
Serial.print("Dziala");
digitalWrite(dioda_zielona,LOW);
//Add code to run if it works
}else{
Serial.println("Wrong");
digitalWrite(dioda_czerwona,HIGH);
delay(500);
Serial.print("Nie dziala");
digitalWrite(dioda_czerwona,LOW);
//add code to run if it did not work
}
}