Why is Arduino Skipping this line of code?

I am doing an engineering project and trying to code my Arduino. I’m using an NFC shield that attaches on top of the Arduino. My code works all the way through, except that it skips the digitalWrite lines.

 if (success)
  {
Serial.print("This Works");

digitalWrite(on, HIGH); // Unlock door!

delay(2000); // Hold door lock open for 2 seconds

digitalWrite(on, LOW); // Relock door

Serial.print("Yay!");
}

 
}

The digitalWrite lines are the most important in my code because I need them to turn on a motor. The basic premise of my project is that when an NFC tag is scanned, it will turn on a motor. Please help, as I can’t seem to figure out why those two lines specifically are being skipped (especially when the two Serial.print lines work fine). For reference, here is my entire code below:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (2)
#define PN532_MOSI (3)
#define PN532_SS   (4)
#define PN532_MISO (5)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield
#define PN532_ON (12)

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a SPI connection:
//Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);

/*
    We can encode many different kinds of pointers to the card,
    from a URL, to an Email address, to a phone number, and many more
    check the library header .h file to see the large # of supported
    prefixes!
*/

#define on (9)

void setup(void) {
  #ifndef ESP8266
    while (!Serial); // for Leonardo/Micro/Zero
  #endif
  Serial.begin(115200);
  Serial.println("Looking for PN532...");
  pinMode(on, OUTPUT);
  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }


  // Got ok data, print it out!
  Serial.print("Found 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.SAMConfig();
}



void loop(void) {
  uint8_t success;                          // Flag to check if there was an error with the PN532
  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)
  bool authenticated = false;               // Flag to indicate if the sector is authenticated

  // Use the default key
  uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

  // Wait for an ISO14443A type card (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success)
  {
Serial.print("This Works");

digitalWrite(on, HIGH); // Unlock door!

delay(2000); // Hold door lock open for 2 seconds

digitalWrite(on, LOW); // Relock door

Serial.print("Yay!");
}

 
}

Let's start with the elephant in the room, why is your code formation such a mess? Press Ctrl+T and see how that looks.

And what is the output on the serial monitor?

Next up:

  • on is the most terrible variable name of the day. Why isn't it called a most sensible DoorLockPin ?
  • And use const bytes/uint8_t (or other type you need) instead of #defines.

It does not skip that line; change on to use pin 13 (built in led); disconnect everything from pin 9 (just in case), do not connect it to pin 13.

Next test it; the led on pin 13 will work if success is not equal to 0.

I just tested pin 13 and it worked exactly according to the code. What is my next step?

How do you have your motor hooked up? You're not trying to drive it directly from the Arduino output, are you?

ianraich:
... What is my next step?

Make a clear drawing how everything is connected; scan or take a photo of the drawing and post here.

Attached is the picture of our circuit

  1. That’s NOT a drawing / circuit diagram which would be more useful.

  2. What do I see on top?

  3. Looks like you do try to connect the motor directly to the Arduino. Let’s hope you didn’t kill the Arduino (pin) and do a Google about how to drive a motor :wink: