-1

I changed the code of nfc 532 but I wanted to make an alteration and I do not know how. Want to give a time out? That is, when I did not put the mobile phone at the foot of the chip during the wait this one leaves the code. That is, I am always waiting I put the mobile phone on the chip to send information, but it may happen that I do not put it, and as it is I always wait. Wanted for a timeout type 10 seconds as a stopwatch

#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"

PN532_SPI pn532spi(SPI, 10);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];
const int buttonPin = 6;
int buttonState = 0;

void setup()
{
    Serial.begin(115200);
    pinMode(7, OUTPUT); 

}
int var=0;  
boolean Acender=0;
void loop()
{
    buttonState = digitalRead(buttonPin);


  if (buttonState == 1) {

    digitalWrite(7,HIGH); //Desliga rele 2

    Serial.println("Send a message to Android");
    NdefMessage message = NdefMessage();
    message.addTextRecord("Alertas acidente: DADOS");
    int messageSize = message.getEncodedSize();
    if (messageSize > sizeof(ndefBuf)) {
        Serial.println("ndefBuf is too small");
        while (1) {
        }

    }

    message.encode(ndefBuf);
    if (0 >= nfc.write(ndefBuf, messageSize)) {

        Serial.println("Failed");

    } else {
        Serial.println("Success");
    }

//     Serial.println("Failed");
  }

  else{
    digitalWrite(7,LOW); //Desliga rele 2
  }
  }

1 Answers1

1

I think this solves your problem, but because your code is quite messy I'm not going to try to put my code into yours, you'll have to do that yourself.

You can use millis() function to calculate time differences:

long last = 0;

void loop() {

    ...

    //run this when a message was received
    last = millis();
    digitalWrite(7, HIGH);

    ...

    //run this every once in a while, eg. every loop()
    long curr = millis();
    if (curr - last >= 1000*10) {
        digitalWrite(7, LOW);
    }
}

The code keeps track of the last time at which a message was received, and if it was longer than 10 seconds ago it disables the led. You need to reset the timer and re-enable the led when you receive a message.

Todd Sewell
  • 1,444
  • 12
  • 26