Hi, I’m trying to control an SG90 servo motor using a PN532 NFC module.
The servo should rotate 180° when the NFC module detects an NFC card. After 4 seconds, if the NFC module detects another card, it should revert the servo motor to its original position. Now, once the NFC module detects a card, it does move the servo but the servo gets stuck and won’t move.
Am I doing something wrong?
Thanks in advance
Here’s my code
#include <Servo.h>
#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532.h>
PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c);
Servo myservo; // create servo object to control a servo
int pos=1; // variable to store the servo position
void setup() {
nfc.begin();
nfc.SAMConfig(); // configure board to read RFID tags
}
void loop() {
boolean success;
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0};
uint8_t uidLength;
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
if ((success) and (pos==1)){
myservo.attach(3);
myservo.write(180);
pos=2;
delay(4000);
}
else if ((success) and (pos==2)) {
myservo.attach(3);
myservo.write(0);
pos=1;
delay(4000);
}
}
P.S. I’m new to programming, so please excuse the crappy code.