Hi all
I'have:
Arduino UNO
4 Channel 5V Relay Module wired with 2 Linear Actuator (to retract and extend)
Rfid rc522 module
The normal work would be:
When the rfid tag is present, dhe linear actuators extends
When arduino receives a serialport command (for example "close"), linear actuators retract.
That works while the 2 motors are unplugged (i see the relays that work).
The problem is:
As soon as i plug the linear actuators, it works for a couple of times: retract on serial command, extend on rfid tag presence.
This works for 2-3 times and than the RFID module don'work any more.
If I, on every serialport command call mfrc522.PCD_Init(), all works fine, the problem disapears.
any idea? Can it be a hw o sw problem?
Thank You very much
Here is the code:
include <SPI.h>
#include <MFRC522.h>
#define RST_PIN 9
#define SS_PIN 10
#define LED 8
const byte MY_ID = 3;
MFRC522 mfrc522(SS_PIN, RST_PIN);
const int relay1 = 4; //Arduino pin that triggers relay #1
const int relay2 = 5; //Arduino pin that triggers relay #2
const int relay3 = 6; //Arduino pin that triggers relay #3
const int relay4 = 7; //Arduino pin that triggers relay #4
const String DELIMITER = ":AAAAA:";
String message4server = "";
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
SPI.begin(); // SPI bus
Serial.begin(9600);
mfrc522.PCD_Init(); // MFRC522
pinMode(LED, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(LED, LOW);
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
}
void loop() {
message4server = "";
if(getID(true)){
}
else{
if(Serial.available()){
char inChar = (char)Serial.read();
if(inChar == '1'){
extendActuator();
delay(3000);
mfrc522.PCD_Init();
}
}
}
}
void extendActuator() {
digitalWrite(relay1, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay2, LOW);
digitalWrite(relay4, LOW);
}
void retractActuator() {
digitalWrite(relay1, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay4, HIGH);
}
boolean getID(boolean stopIt){ //Read new tag if available
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return false;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return false;
}
blinkLed(3, 100);
String tagID = "";
for ( uint8_t i = 0; i < 4; i++) { // The MIFARE PICCs that we use have 4 byte UID
tagID.concat(String(mfrc522.uid.uidByte*, HEX)); // Adds the 4 bytes in a single String variable*
- }*
- tagID.toUpperCase();*
- if(stopIt){*
- mfrc522.PICC_HaltA(); // Stop reading*
- //mfrc522.PCD_StopCrypto1();*
- }*
- retractActuator();*
- return true;*
}
void blinkLed(int volte, int intervallo){ - for(int i=0; i<volte;i++){*
- digitalWrite(LED, HIGH);*
- delay(intervallo);*
- digitalWrite(LED, LOW);*
- delay(intervallo); *
- }*
}