Hilfe zu Funktionen und deren Verwendung im Loop

Hallo zusammen.

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? :thinking:

Hallo,

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);
      }
}

geht aus so:

bool cardPresent() {  
  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)
  return = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);  
}

lass dir mal mit Serial ausgeben was in uint8_t uid steht

Dahinter noch ein

while (!cardPresent()) do delay(100); //wait for card present


Das führt bei mir zu dieser Fehlermeldung.

Die man sich auch als Text fürs Forum kopieren lassen kann.

Was mache ich verkehrt?

exit status 1

Compilation error: expected 'while' before '}' token

#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 ...");
  while (!cardPresent()) do delay(100); //wait for card present
}
 
  

  

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);
      }
}
  
  

dann entferne das =

Sorry.

exit status 1

Compilation error: expected primary-expression before '=' token

meintest Du so?

Den wichtigsten Teil hast Du vergessen: alles was davor steht!

C:\Users\bollw\OneDrive\Dokumente\Arduino\PN532 Grundkonfiguration_einfach\PN532 Grundkonfiguration_einfach.ino: In function 'void setup()':
C:\Users\bollw\OneDrive\Dokumente\Arduino\PN532 Grundkonfiguration_einfach\PN532 Grundkonfiguration_einfach.ino:29:1: error: expected 'while' before '}' token
 }
 ^
C:\Users\bollw\OneDrive\Dokumente\Arduino\PN532 Grundkonfiguration_einfach\PN532 Grundkonfiguration_einfach.ino:29:1: error: expected '(' before '}' token
C:\Users\bollw\OneDrive\Dokumente\Arduino\PN532 Grundkonfiguration_einfach\PN532 Grundkonfiguration_einfach.ino:29:1: error: expected primary-expression before '}' token
C:\Users\bollw\OneDrive\Dokumente\Arduino\PN532 Grundkonfiguration_einfach\PN532 Grundkonfiguration_einfach.ino:29:1: error: expected ')' before '}' token
C:\Users\bollw\OneDrive\Dokumente\Arduino\PN532 Grundkonfiguration_einfach\PN532 Grundkonfiguration_einfach.ino:29:1: error: expected ';' before '}' token

exit status 1

Compilation error: expected 'while' before '}' token

Schaue dir das mal an

Benutze mal Strg-T in der IDE

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. :woozy_face:

Jetzt wäre noch interessant, was in dieser Zeile 29 und knapp davor steht.

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...

Nach dann.

Verschiebe
uint8_t uid[] ...
aus cardPresent vor setup().

Screenshots sind so schlecht zu kopieren, bitte alles Wesentliche als < CODE > oder sonstigen Text zeigen.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.