I have 3 function reading RFID card, sending data to Pc and servo controlling. It runs well when runs dependently but when I put them together, the reading card function sometimes doesn't work, the sending data totally doesn't work and servo controlling still runs well. Here is my code:
#include <SPI.h> // SPI Library
#include <MFRC522.h> // Library for RC522 module
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Ethernet.h>
LiquidCrystal_I2C lcd_green(0x27, 16, 2);
LiquidCrystal_I2C lcd_blue(0x3F, 16, 2);
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169,254,240,182);
byte server[] ={169,254,240,181};
#define RST_PIN_1 5
#define RST_PIN_2 6 // Configurable, see typical pin layout above
#define SS_1_PIN 38 // SDA Pin for Reader 0
#define SS_2_PIN 39 // SDA Pin for Reader 1
#define NR_OF_READERS 2 // Total number of Readers
byte ssPins[] = {SS_1_PIN, SS_2_PIN};// Storing the SDA pins of both reader so that we can call them 1 by 1
MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
Servo myServo_1;
Servo myServo_2;
int sensorPin_1 = 7;
int val_1 = 0;
int sensorPin_2 = 8;
int val_2 = 0;
int button_1 = 9;
int button_2 = 10;
EthernetClient client;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
Ethernet.begin(mac, ip);
Serial.print("Connect to IP:");
Serial.println(Ethernet.localIP());
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) // Loop to initialize all RFID readers 1 by 1
{
mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN_1); // Init each MFRC522 card
Serial.print(F("Reader"));
Serial.print(reader); //Print Reader number
Serial.print(F(": "));
mfrc522[reader].PCD_DumpVersionToSerial();//Printing Firmware version of RFID reader
}
delay(2000);
if(client.connect(server,65432)){
Serial.println("Connected");
}
else{
Serial.println("fail");
}
lcd_green.init();
lcd_green.backlight();
lcd_green.setCursor(0, 0);
lcd_blue.init();
lcd_blue.backlight();
lcd_blue.setCursor(0, 0);
SPI.begin(); // Init SPI bus
myServo_1.attach(3);
myServo_1.write(0);
myServo_2.attach(4);
myServo_2.write(0);
pinMode(sensorPin_1, INPUT);
pinMode(sensorPin_2, INPUT);
pinMode(button_1, INPUT_PULLUP);
pinMode(button_2, INPUT_PULLUP);
}
String content="";
String valid ="";
void loop() {
//code scan card, return valid = UID + reader
for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) //Loop to check readers 1 by 1
{
// Look for new cards
if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) //If card is Present and Data is coming
{
Serial.print(F("Reader "));
Serial.print(reader); //Print Reader number
// Show some details of the PICC (that is: the tag/card)
String UID = dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size); //Print Unique ID code of Card
if(reader == 0){
valid = UID + "?In";
}
if(reader == 1){
valid = UID + "?Out";
}
Serial.print(valid);
// Halt PICC
mfrc522[reader].PICC_HaltA(); //Pause the reader
// Stop encryption on PCD
mfrc522[reader].PCD_StopCrypto1();
}
}
//code communicate with app
delay(2000);
content = "";
if(client.connected()){
Serial.print("helo");
client.print("ok");
delay(2000);
}
while(client.available()){
char thisChar = client.read();
content += thisChar;
}
if(content != ""){
Serial.println(content);
if(content=="10"){
lcd_blue.clear();
lcd_blue.print("Welcome");
}
if(content=="00"){
lcd_blue.clear();
lcd_blue.print("Fail, try again");
}
if(content=="11"){
lcd_green.clear();
lcd_green.print("Good Bye");
}
if(content=="01"){
lcd_green.clear();
lcd_green.print("Fail, try again");
}
delay(10);
String check = content.substring(0, 1);
String way = content.substring(1);
Serial.println(check);
Serial.println(way);
if(check == "1" && way == "0"){
openServo_1();
closeServo_1();
}
else if(check == "1" && way == "1"){
openServo_2();
closeServo_2();
}
}
int clicked_1 = digitalRead(button_1);
int clicked_2 = digitalRead(button_2);
if(clicked_1 == 0){
myServo_1.write(90);
}
if(clicked_2 == 0){
myServo_2.write(90);
}
}
String dump_byte_array(byte *buffer, byte bufferSize) //Function to print the incoming bytes
{
String UID = "";
for (int i = 0; i < bufferSize; i++)
{
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
UID += String(buffer[i]);
}
return UID;
}
void openServo_1(){
myServo_1.write(90);
}
void openServo_2(){
myServo_2.write(90);
}
void closeServo_1(){
if(val_1 == 1){
myServo_1.write(0);
}
}
void closeServo_2(){
if(val_2 == 1){
myServo_2.write(0);
}
}
`