I am currently trying to make a buzzer go off when it detects a certain NFC tag which works but i also want to control 2 servo motors remotely from a website that i made which they both work separately but the Server won't work (it won't check for commands send by the website) when NFC is working and i need them both working in the same section of code. So how can i get both the NFC and the server working at the same time.
Here is my current code:
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <Servo.h>
#include <SPI.h>
#include "PN532_SPI.h"
#include "PN532.h"
#include "NfcAdapter.h"
String const myUID1 = "04 A7 86 9A 79 40 80" ;
String const myUID2 = "04 8B 86 9A 79 40 80" ;
int Buzzer = 8;
PN532_SPI interface(SPI, 10); // create a SPI interface for the shield with the SPI CS terminal at digital pin 10
NfcAdapter nfc = NfcAdapter(interface); // create an NFC adapter object
YunServer server;
Servo PanServo;
Servo TiltServo;
void setup() {
PanServo.attach(5);
TiltServo.attach(6);
Bridge.begin();
server.listenOnLocalhost();
server.begin();
Console.begin();
Console.println("NDEF Reader");
nfc.begin();
pinMode(Buzzer, OUTPUT);
}
void loop() {
YunClient client = server.accept();
if (client) {
process(client);
client.stop();
}
delay(20);
Console.println("Scanning...");
if (nfc.tagPresent())
{
NfcTag tag = nfc.read();
String scannedUID = tag.getUidString();
if ( myUID1.compareTo(scannedUID) == 0)
{
Console.println("Correct Key");
digitalWrite(Buzzer, HIGH);
delay(50);
digitalWrite(Buzzer, LOW);
delay(50);
} else {
if (myUID2.compareTo(scannedUID) == 0)
{
Console.println("Correct Key");
digitalWrite(Buzzer, HIGH);
delay(50);
digitalWrite(Buzzer, LOW);
delay(50);
} else {
Console.println("Incorrect key");
digitalWrite(Buzzer, HIGH);
delay(250);
digitalWrite(Buzzer, LOW);
delay(250);
digitalWrite(Buzzer, HIGH);
delay(125);
digitalWrite(Buzzer, LOW);
delay(125);
}
}
delay(15);
}
}
//Below to commented works
void process(YunClient client) {
String command = client.readStringUntil('/');
if (command == "servo") {
servoCommand(client);
}
}
void servoCommand(YunClient client) {
int pin, value;
pin = client.parseInt();
if (client.read() == '/') {
value = client.parseInt();
if (pin == 5)
{
PanServo.write(value);
}
else if (pin == 6)
{
TiltServo.write(value);
}
else if (pin == 99)
{
PanServo.write(value);
TiltServo.write(value);
}
}
}