ich habe mal eine Verständnissfrage zu dem beigefügten Code. Es soll die ID der RFID Karte ausgegeben werden. Dies soll passieren, wenn eine Karte vor dem Reader ist. Wenn keine Karte vorhanden, soll "Warte auf Karte" ausgegeben werden. Es geht mir darum, das Ergebnis der bool cardPresent() sichtbar zu machen.
Aktuell wird nur die ID der Karte angezeigt. Wenn keine Karte vorhanden, passiert nichts.
Vielen Dank schon einmal im Voraus.
Christian
#include <SPI.h>
#include <Wire.h>
#include <PN532.h>
#include <PN532_I2C.h>
#define PN532_IRQ D5 // Definiere den IRQ Anschluss
#define PN532_RESET D6 // Definiere den Reset Anschluss
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
void setup()
{
Serial.begin(115200);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (10); // halt
}
// Got ok data, print it out!
Serial.print("Gefundener Chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.setPassiveActivationRetries(0xFF); // for example
nfc.SAMConfig();
Serial.println("Warte auf eine Karte ...");
}
String NfcCheck()
{
uint8_t 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)
{
String hex_wert = "";
for (uint8_t i=0; i < uidLength; i++)
{
hex_wert += (String)uid[i];
}
Serial.println(hex_wert);
return hex_wert;
}
}
bool cardPresent()
{
static bool wasSuccess = false;
uint8_t 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)
{
wasSuccess = true;
return true;
}
else if (wasSuccess)
{
wasSuccess = false;
return false;
}
}
void loop() {
if (cardPresent() == true)
{
String nfcID = NfcCheck();
delay (500);
}
else if (cardPresent()==false)
{
Serial.println("Warte auf Karte");
delay (100);
}
}
In cardPresent() musst du statt else if nur else schreiben oder noch ein else ergänzen. Weil wenn beide Bedingungen nicht wahr sind was gibt deine Funktion dann zurück?
Warnt dir der Compiler das gar nicht an?
danke für die Antwort. Ich habe das entsprechend geändert, das Ergebniss ist allerdings das Gleiche. Es wird nach wie vor nur die ID der Karte ausgegeben. Ich habe keine Info darüber, welches Ergbniss aus der cardPresent() kommt.
#include <SPI.h>
#include <Wire.h>
#include <PN532.h>
#include <PN532_I2C.h>
#define PN532_IRQ D5 // Definiere den IRQ Anschluss
#define PN532_RESET D6 // Definiere den Reset Anschluss
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
void setup()
{
Serial.begin(115200);
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (10); // halt
}
// Got ok data, print it out!
Serial.print("Gefundener Chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// configure board to read RFID tags
nfc.setPassiveActivationRetries(0xFF); // for example
nfc.SAMConfig();
Serial.println("Warte auf eine Karte ...");
}
String NfcCheck()
{
uint8_t 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)
{
String hex_wert = "";
for (uint8_t i=0; i < uidLength; i++)
{
hex_wert += (String)uid[i];
}
//Serial.println(hex_wert);
return hex_wert;
}
}
bool cardPresent()
{
static bool wasSuccess = false;
uint8_t 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)
{
wasSuccess = true;
return true;
}
else
{
wasSuccess = false;
return false;
}
}
void loop() {
if (cardPresent() == true)
{
String nfcID = NfcCheck();
Serial.println(nfcID);
delay (500);
}
else if (cardPresent()==false)
{
Serial.println("Warte auf Karte");
delay (100);
}
else
{
Serial.println("Geht");
delay(500);
}
}
Ich steh leider voll auf dem Schlauch. Wenn ich im Loop Serial.println(uid[0]); eingebe, kommt diese Fehlermeldung
:\Users\bollw\AppData\Local\Temp\.arduinoIDE-unsaved202396-11204-zcrr5n.eohq\sketch_oct6a\sketch_oct6a.ino: In function 'void loop()':
C:\Users\bollw\AppData\Local\Temp\.arduinoIDE-unsaved202396-11204-zcrr5n.eohq\sketch_oct6a\sketch_oct6a.ino:63:18: error: 'uid' was not declared in this scope
Serial.println(uid[0]);
^
exit status 1
Compilation error: 'uid' was not declared in this scope
Bitte um Nachsicht, falls ich mich einfach nur dämlich anstelle.
Hättest Du Deinen Sketch mit STRG-T vorher richtig gemacht, wäre das sofort sichtbar gewesen
if (cardPresent() == true)
{
String nfcID = NfcCheck();
delay (500);
}
else if (cardPresent() == false)
{
Serial.println("Warte auf Karte");
delay (100);
}
Das wird nie und nimmer was.
Hier: if (cardPresent() == true)
liest Du das erste Mal die Karte aus.
Wenn die Bedingung nicht erfüllt ist, dann versuchst Du es zum zweiten Mal. else if (cardPresent() == false)
Du weisst nicht, ob sich dazwischendurch etwas geändert hat.
Wenn ja, bist Du zweimal mit einer nicht erfüllten Bedingung dabei.
Einen ähnlichen Bock schiesst Du hier:
bool cardPresent()
{
static bool wasSuccess = false;
uint8_t 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)
{
wasSuccess = true;
return true;
}
else if (wasSuccess)
{
wasSuccess = false;
return false;
}
}
Das müsste Dir der Compiler schon vorwerfen und Dich darauf hinweisen.
Wenn keine der Bedingungen erfüllt ist, bekommst Du vom Compiler gesagt, das es am returnwert fehlt...