Controlling 3 servos with 3 different buttons, if authorized

Hey guys, I’ve googled as far as I can get but need some pointers as to where this is going wrong.

Essentially, I have an RFID card that on success, grants access to control 3 servos with 3 distinct buttons. I thought I had this working yesterday, albeit with a bit of slop, but today there’s no real control. On successful authentication, each button does something different, every time, sometimes sending multiple servos on wild rides and I can’t quite pick up a pattern. I’ve got plenty of power going to the servos.

I don’t think this should have anything to do with it, but two of my servos are digital, one is an analog, all are connected to digital pins.

I’m not a programmer, but a pretty good googler. Hopefully this is understandable.

/**************************************************************************/
/*! 
    @file     readMifare.pde
    @author   Adafruit Industries
	@license  BSD (see license.txt)

    This example will wait for any ISO14443A card or tag, and
    depending on the size of the UID will attempt to read from it.
   
    If the card has a 4-byte UID it is probably a Mifare
    Classic card, and the following steps are taken:
   
    - Authenticate block 4 (the first block of Sector 1) using
      the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
    - If authentication succeeds, we can then read any of the
      4 blocks in that sector (though only block 4 is read here)
	 
    If the card has a 7-byte UID it is probably a Mifare
    Ultralight card, and the 4 byte pages can be read directly.
    Page 4 is read by default since this is the first 'general-
    purpose' page on the tags.


This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
This library works with the Adafruit NFC breakout 
  ----> https://www.adafruit.com/products/364
 
Check out the links above for our tutorials and wiring diagrams 
These chips use SPI or I2C to communicate.

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

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

#include <Servo.h> 
 
const int servoPin3 = 3;
const int servoPin5 = 5;
const int servoPin7 = 7;
const int buttonPin4 = 4;
const int buttonPin6 = 6;  // Pushbutton pin
const int buttonPin8 = 8;
Servo myservo3; 
Servo myservo5;
Servo myservo7;
int buttonState = 0; 
int directionState = 0;
int pos = 0;    // variable to store the servo position 

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (13)
#define PN532_MOSI (11)
#define PN532_SS   (10)
#define PN532_MISO (12)

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

// 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 software SPI connection (recommended):
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);

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
// also change #define in Adafruit_PN532.cpp library file
   #define Serial SerialUSB
#endif

void setup(void) {
    // initialize digital pin 13 as an output.
   pinMode(13, OUTPUT);
   pinMode(2, OUTPUT);      //MICHAEL CODE - goes to pn532
   digitalWrite(2, HIGH);  //MICHAEL CODE - power the pn532
   
   myservo3.attach(servoPin3);
   pinMode(buttonPin4, INPUT_PULLUP);      
   myservo5.attach(servoPin5);
   pinMode(buttonPin6, INPUT_PULLUP);
   myservo7.attach(servoPin7);
   pinMode(buttonPin8, INPUT_PULLUP);
  
  #ifndef ESP8266
    while (!Serial); // for Leonardo/Micro/Zero
  #endif
  Serial.begin(115200);
  Serial.println("Hello!");

  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();
  
  Serial.println("Waiting for an ISO14443A Card ...");

}


void loop(void) {

  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)
    
  // Wait for an ISO14443A type cards (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) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");
    
    
    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
	  
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
            buttonState = digitalRead(buttonPin4);
           if (directionState == 0){
           if (buttonState == LOW) {
            directionState = 1;
           for(pos = 0; pos < 180; pos += 1)
             {
               myservo3.write(pos);
               delay(15);  // waits 15ms to reach the position 
             }
             }
   
             } 
             
               buttonState = digitalRead(buttonPin6);
           if (directionState == 0){
           if (buttonState == LOW) {
            directionState = 1;
           for(pos = 0; pos < 180; pos += 1)
             {
               myservo5.write(pos);
               delay(15);  // waits 15ms to reach the position 
             }
             }
   
             } 

       
             
   else if (directionState == 1) 
             {
             if (buttonState == HIGH) {
             directionState = 0;   
       
            for (pos = 180; pos>=1; pos -=1)
             {
               myservo3.write(pos);
               myservo5.write(pos);
               myservo7.write(pos);
               delay(15);
             }
             }
             }
             
             
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        Serial.println("");
		
        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }

}
else if (directionState == 1) 
             {
             if (buttonState == HIGH) {
             directionState = 0;

Is this supposed to run only when button is NOT pressed?

Open your code in the IDE and press control-T. That will autoformat your code. Then repost it. With the code formatted and the blocks lined up it will be a lot easier to follow the flow of the logic and find the bug.

Yes, that’s to reset the servo position back to a neutral position.

Code formatted below, thanks for that tip by the way.

/**************************************************************************/
/*!
    @file     readMifare.pde
    @author   Adafruit Industries
	@license  BSD (see license.txt)

    This example will wait for any ISO14443A card or tag, and
    depending on the size of the UID will attempt to read from it.

    If the card has a 4-byte UID it is probably a Mifare
    Classic card, and the following steps are taken:

    - Authenticate block 4 (the first block of Sector 1) using
      the default KEYA of 0XFF 0XFF 0XFF 0XFF 0XFF 0XFF
    - If authentication succeeds, we can then read any of the
      4 blocks in that sector (though only block 4 is read here)

    If the card has a 7-byte UID it is probably a Mifare
    Ultralight card, and the 4 byte pages can be read directly.
    Page 4 is read by default since this is the first 'general-
    purpose' page on the tags.


This is an example sketch for the Adafruit PN532 NFC/RFID breakout boards
This library works with the Adafruit NFC breakout
  ----> https://www.adafruit.com/products/364

Check out the links above for our tutorials and wiring diagrams
These chips use SPI or I2C to communicate.

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

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

#include <Servo.h>

const int servoPin3 = 3;
const int servoPin5 = 5;
const int servoPin7 = 7;
const int buttonPin4 = 4;
const int buttonPin6 = 6;  // Pushbutton pin
const int buttonPin8 = 8;
Servo myservo3;
Servo myservo5;
Servo myservo7;
int buttonState = 0;
int directionState = 0;
int pos = 0;    // variable to store the servo position

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (13)
#define PN532_MOSI (11)
#define PN532_SS   (10)
#define PN532_MISO (12)

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

// 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 software SPI connection (recommended):
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);

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
// also change #define in Adafruit_PN532.cpp library file
#define Serial SerialUSB
#endif

void setup(void) {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);      //MICHAEL CODE - goes to pn532
  digitalWrite(2, HIGH);  //MICHAEL CODE - power the pn532

  myservo3.attach(servoPin3);
  pinMode(buttonPin4, INPUT_PULLUP);
  myservo5.attach(servoPin5);
  pinMode(buttonPin6, INPUT_PULLUP);
  myservo7.attach(servoPin7);
  pinMode(buttonPin8, INPUT_PULLUP);

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif
  Serial.begin(115200);
  Serial.println("Hello!");

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

  Serial.println("Waiting for an ISO14443A Card ...");

}


void loop(void) {

  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)

  // Wait for an ISO14443A type cards (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) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");


    if (uidLength == 7)
    {
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");

      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
      {
        buttonState = digitalRead(buttonPin4);
        if (directionState == 0) {
          if (buttonState == LOW) {
            directionState = 1;
            for (pos = 0; pos < 180; pos += 1)
            {
              myservo3.write(pos);
              delay(15);  // waits 15ms to reach the position
            }
          }

        }

        buttonState = digitalRead(buttonPin6);
        if (directionState == 0) {
          if (buttonState == LOW) {
            directionState = 1;
            for (pos = 0; pos < 180; pos += 1)
            {
              myservo5.write(pos);
              delay(15);  // waits 15ms to reach the position
            }
          }

        }


        buttonState = digitalRead(buttonPin6);
        if (directionState == 0) {
          if (buttonState == LOW) {
            directionState = 1;
            for (pos = 0; pos < 180; pos += 1)
            {
              myservo7.write(pos);
              delay(15);  // waits 15ms to reach the position
            }
          }

        }


        else if (directionState == 1)
        {
          if (buttonState == HIGH) {
            directionState = 0;

            for (pos = 180; pos >= 1; pos -= 1) 
            {
              myservo3.write(pos);
              myservo5.write(pos);
              myservo7.write(pos);
              delay(15);
            }
          }
        }


        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        Serial.println("");

        // Wait a bit before reading the card again
        delay(1000);
      }
      else
      {
        Serial.println("Ooops ... unable to read the requested page!?");
      }
    }
  }

}

I've fixed this! Thanks for the help.