See Robin's Serial Input Basics - updated to get some ideas.
Note: Your code in post #19 will not work reliably; serial is slow so the processor might see 3 bytes followed by 5 bytes followed by 4 bytes.
See Robin's Serial Input Basics - updated to get some ideas.
Note: Your code in post #19 will not work reliably; serial is slow so the processor might see 3 bytes followed by 5 bytes followed by 4 bytes.
I want to receive string of 12 characters
I want to read 4 serial port independently and turn on/off relay pin after each reading and comparing string
What is the exact Data Format that you expect to receive ?
What kind of Data ?
How many characters ?
What terminator is used ?
How much ?
Ah, I see that you can look in the future. Replying before @LarryD posted his question.
![]()
![]()
if(Serial1.available())
You could use:
if(Serial1.available() > 11)
From your post #1
Serial.println(Switch_State1);
Serial1.println(Switch_State2);
Serial2.println(Switch_State3);
Serial3.println(Switch_State4);
What does the achieve ?
From your post #12.
What is connected to Serial1 port ?
How is the addition of the 4 Serial ports going to affect your current sketch ?
Do receive messages have a terminator character ?
What message turns on the relay ?
For how long ?
What message turns off the relay ?
For how long ?
Can any serial port operate any relay ?
I am posting complete sketch which is working fine but there is only one problem. Controller get busy to turn on, off relay so that other serial port can not work
How to remove blocking delay for every relay's
#define ResetTimer millis()
#define ENABLED true
#define DISABLED false
#define RelayOFF HIGH
#define RelayON LOW
#define PUSHED LOW
#define RELEASED HIGH
int Switch_State1, Switch_State2, Switch_State3, Switch_State4 = 0;
int count1, count2, count3, count4 = 0; // count
char input1[12], input2[12], input3[12], input4[12] ; // character array to store 12 bytes
const int Switch_Pin1 = 4;
const int Switch_Pin2 = 5;
const int Switch_Pin3 = 6;
const int Switch_Pin4 = 7;
const int Relay_Pin1 = 8;
const int Relay_Pin2 = 9;
const int Relay_Pin3 = 10;
const int Relay_Pin4 = 11;
boolean FLAG1 = DISABLED;
boolean FLAG2 = DISABLED;
boolean FLAG3 = DISABLED;
boolean FLAG4 = DISABLED;
byte lastSwitch_Pin1 = RELEASED;
byte lastSwitch_Pin2 = RELEASED;
byte lastSwitch_Pin3 = RELEASED;
byte lastSwitch_Pin4 = RELEASED;
//timing stuff
unsigned long checkSwitchesTime;
unsigned long currentTime;
unsigned long relayTime1;
unsigned long relayTime2;
unsigned long relayTime3;
unsigned long relayTime4;
const long interval = 4000ul;
// Store valid tags for each door access
char * Valid_Tags_Door1[] = {"0B000238D9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
char * Valid_Tags_Door2[] = {"0B000238D9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
char * Valid_Tags_Door3[] = {"0B000238D9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
char * Valid_Tags_Door4[] = {"0B000238D9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
void setup() {
// begin serial port with baud rate 9600bps
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Serial3.begin(9600);
// configure inputs pins for switche's
pinMode(Switch_Pin1, INPUT);
pinMode(Switch_Pin2, INPUT);
pinMode(Switch_Pin3, INPUT);
pinMode(Switch_Pin4, INPUT);
// configure output pins for Door's
pinMode(Relay_Pin1, OUTPUT);
pinMode(Relay_Pin2, OUTPUT);
pinMode(Relay_Pin3, OUTPUT);
pinMode(Relay_Pin4, OUTPUT);
// initially all door's are closed
digitalWrite(Relay_Pin1, RelayON );
digitalWrite(Relay_Pin2, RelayON );
digitalWrite(Relay_Pin3, RelayON );
digitalWrite(Relay_Pin4, RelayON );
}
void loop() {
currentTime = millis();
//time to check our switches ?
if (currentTime - checkSwitchesTime >= 50)
{
//restart this TIMER
checkSwitchesTime = ResetTimer;
checkSwitches();
}
//if enabled, is this TIMER expired ?
if (FLAG1 == ENABLED && currentTime - relayTime1 >= interval)
{
//we are now finished with this TIMER
FLAG1 = DISABLED;
digitalWrite(Relay_Pin1, RelayON);
}
//if enabled, is this TIMER expired ?
if (FLAG2 == ENABLED && currentTime - relayTime2 >= interval)
{
//we are now finished with this TIMER
FLAG2 = DISABLED;
digitalWrite(Relay_Pin2, RelayON);
}
if (FLAG3 == ENABLED && currentTime - relayTime3 >= interval)
{
//we are now finished with this TIMER
FLAG3 = DISABLED;
digitalWrite(Relay_Pin3, RelayON);
}
if (FLAG4 == ENABLED && currentTime - relayTime4 >= interval)
{
FLAG4 = DISABLED;
digitalWrite(Relay_Pin4, RelayON);
}
if(Serial.available())
{
while(Serial.available() && count1 < 12) // Read 12 characters and store them in input array
{
input1[count1] = Serial.read();
count1++;
delay(4);
}
if (count1 == 12){
count1 = 0;
String RFID1_tag_number = input1;
}
for (int i = 0; i < 5; i++)
{
if (strcmp(Valid_Tags_Door1[i] , input1) == 0)
{
digitalWrite(Relay_Pin1, RelayOFF );
delay(1500);
digitalWrite(Relay_Pin1, RelayON );
}
}
}*/
if(Serial1.available()){
while(Serial1.available() && count2 < 12) // Read 12 characters and store them in input array
{
input2[count2] = Serial1.read();
count2++;
delay(2);
}
if (count2 == 12){
count2 = 0;
String RFID2_tag_number = input2;
}
for (int i = 0; i < 5; i++)
{
if (strcmp(Valid_Tags_Door2[i] , input2) == 0){
digitalWrite(Relay_Pin2, RelayOFF );
delay(1000);
digitalWrite(Relay_Pin2, RelayON );
}
}
}
if(Serial2.available())
{
while(Serial2.available() && count3 < 12) // Read 12 characters and store them in input array
{
input3[count3] = Serial2.read();
count3++;
delay(4);
}
if (count3 == 12){
count3 = 0;
String RFID3_tag_number = input3;
}
for (int i = 0; i < 5; i++)
{
if (strcmp(Valid_Tags_Door3[i] , input3) == 0)
{
digitalWrite(Relay_Pin3, RelayOFF );
delay(1000);
digitalWrite(Relay_Pin3, RelayON );
}
}
}
if(Serial3.available())
{
while(Serial3.available() && count4 < 12) // Read 12 characters and store them in input array
{
input4[count4] = Serial3.read();
count4++;
delay(4);
}
if (count4 == 12){
count4 = 0;
String RFID4_tag_number = input4;
}
for (int i = 0; i < 5; i++)
{
if (strcmp(Valid_Tags_Door4[i] , input4) == 0)
{
digitalWrite(Relay_Pin4, RelayOFF);
delay(1000);
digitalWrite(Relay_Pin4, RelayON );
}
}
}
}
void checkSwitches()
{
byte currentState = digitalRead(Switch_Pin1);
if (lastSwitch_Pin1 != currentState)
{
lastSwitch_Pin1 = currentState;
if (FLAG1 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
FLAG1 = ENABLED;
digitalWrite(Relay_Pin1, RelayOFF);
//reset this TIMER
relayTime1 = ResetTimer;
}
} //END of this switch
//********************************* Switch_Pin2
currentState = digitalRead(Switch_Pin2);
//did this switch change state ?
if (lastSwitch_Pin2 != currentState)
{
//update to the new state
lastSwitch_Pin2 = currentState;
//if we are not timing, is the switch now pushed ?
if (FLAG2 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
FLAG2 = ENABLED;
digitalWrite(Relay_Pin2, RelayOFF);
//reset this TIMER
relayTime2 = ResetTimer;
}
}
currentState = digitalRead(Switch_Pin3);
//did this switch change state ?
if (lastSwitch_Pin3 != currentState)
{
//update to the new state
lastSwitch_Pin3 = currentState;
//if we are not timing, is the switch now pushed ?
if (FLAG3 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
FLAG3 = ENABLED;
digitalWrite(Relay_Pin3, RelayOFF);
//reset this TIMER
relayTime3 = ResetTimer;
}
}
currentState = digitalRead(Switch_Pin4);
//did this switch change state ?
if (lastSwitch_Pin4 != currentState)
{
//update to the new state
lastSwitch_Pin4 = currentState;
//if we are not timing, is the switch now pushed ?
if (FLAG4 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
FLAG4 = ENABLED;
digitalWrite(Relay_Pin4, RelayOFF);
//reset this TIMER
relayTime4 = ResetTimer;
}
}
}
You must give use complete code.
As you see below, one group has 11 characters and the others have 12
0B000238D9B This has 11 characters <-----<<<<<
060031F4894C This has 12 characters
16002GH7BF62 This has 12 characters
190077FC1D24 This has 12 characters
160064F9C45A This has 12 characters
sorry it was copy paste error. All are 12 characters
What should this be 0B000238D9B ?
12 characters
0B000238CD9B
060031F4894C
16002GH7BF62
190077FC1D24
160064F9C45A
This is close.
You can do Serial1-2-3
//********************************************^************************************************
//
// https://forum.arduino.cc/t/how-to-get-rid-of-this-blocking-delay/1025651
//
//********************************************^************************************************
// 0_Arduino_Skeleton_Sketch.ino
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================
// 1.01 22/08/25 Running code
// 1.02 22/08/25 Added code to handle relays 4 and 5, move from pin12 to pin 11
// 1.03 22/08/26 Added RFID code
//
//
//********************************************^************************************************
#define RESETtime millis()
#define ENABLED true
#define DISABLED false
#define RelayOFF HIGH
#define RelayON LOW
#define PUSHED LOW //+5V---[Pullup]---[Input Pin]---[Switch]---GND
#define RELEASED HIGH
//********************************************^************************************************
const byte Switch1 = 4;
const byte Switch2 = 5;
const byte Switch3 = 6;
const byte Switch4 = 7;
const byte Relay1 = 8;
const byte Relay2 = 9;
const byte Relay3 = 10;
const byte Relay4 = 11;
const byte heartbeatLED = 13;
boolean TimerFlag1 = DISABLED;
boolean TimerFlag2 = DISABLED;
boolean TimerFlag3 = DISABLED;
boolean TimerFlag4 = DISABLED;
byte lastSwitch1 = RELEASED;
byte lastSwitch2 = RELEASED;
byte lastSwitch3 = RELEASED;
byte lastSwitch4 = RELEASED;
byte count1;
byte count2;
byte count3;
byte count4;
char input1[12];
char input2[12];
char input3[12];
char input4[12];
//valid tags for each door access:
char * Valid_Tags_Door1[] = {"0B000238CD9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
char * Valid_Tags_Door2[] = {"0B000238CD9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
char * Valid_Tags_Door3[] = {"0B000238CD9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
char * Valid_Tags_Door4[] = {"0B000238CD9B", "060031F4894C", "16002GH7BF62", "190077FC1D24", "160064F9C45A"};
//timing stuff
const unsigned long switchInterval = 4000ul;
const unsigned long rfidInterval = 1500ul;
unsigned long interval;
unsigned long heartbeatTime;
unsigned long checkSwitchesTime;
unsigned long checkRfidTime;
unsigned long relay1Time;
unsigned long relay2Time;
unsigned long relay3Time;
unsigned long relay4Time;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
Serial.begin(9600);
// Serial1.begin(9600);
// Serial2.begin(9600);
// Serial3.begin(9600);
pinMode(Switch1, INPUT_PULLUP);
pinMode(Switch2, INPUT_PULLUP);
pinMode(Switch3, INPUT_PULLUP);
pinMode(Switch4, INPUT_PULLUP);
pinMode(heartbeatLED, OUTPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
digitalWrite(Relay1, RelayON);
digitalWrite(Relay2, RelayON);
digitalWrite(Relay3, RelayON);
digitalWrite(Relay4, RelayON);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//********************************* heartbeat TIMER
//is it time to toggle the heartbeatLED (every 500ms)?
if (millis() - heartbeatTime >= 500ul)
{
//restart this TIMER
heartbeatTime = RESETtime;
//toggle the heartbeatLED
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
//********************************* checkSwitches TIMER
//is time to check our switches ?
if (millis() - checkSwitchesTime >= 50ul)
{
//restart this TIMER
checkSwitchesTime = RESETtime;
checkSwitches();
}
//********************************* checkRfidTime TIMER
//is time to check for RFID receive characters ?
if (millis() - checkRfidTime >= 20ul)
{
//restart this TIMER
checkRfidTime = RESETtime;
checkrfidTags();
}
//********************************* Relay1 TIMER
//if enabled, is this TIMER expired ?
if (TimerFlag1 == ENABLED && millis() - relay1Time >= interval)
{
//we are now finished with this TIMER
TimerFlag1 = DISABLED;
digitalWrite(Relay1, RelayON);
}
//********************************* Relay2 TIMER
//if enabled, is this TIMER expired ?
if (TimerFlag2 == ENABLED && millis() - relay2Time >= interval)
{
//we are now finished with this TIMER
TimerFlag2 = DISABLED;
digitalWrite(Relay2, RelayON);
}
//********************************* Relay3 TIMER
//if enabled, is this TIMER expired ?
if (TimerFlag3 == ENABLED && millis() - relay3Time >= interval)
{
//we are now finished with this TIMER
TimerFlag3 = DISABLED;
digitalWrite(Relay3, RelayON);
}
//********************************* Relay4 TIMER
//if enabled, is this TIMER expired ?
if (TimerFlag4 == ENABLED && millis() - relay4Time >= interval)
{
//we are now finished with this TIMER
TimerFlag4 = DISABLED;
digitalWrite(Relay4, RelayON);
}
//*********************************
//other non blocking code goes here
//*********************************
} //END of loop()
// c h e c k S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches()
{
byte currentState;
//********************************* Switch1
currentState = digitalRead(Switch1);
//did this switch change state ?
if (lastSwitch1 != currentState)
{
//update to the new state
lastSwitch1 = currentState;
//if we are not timing, is the switch now pushed ?
if (TimerFlag1 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
TimerFlag1 = ENABLED;
//set the interval for the relay TIMER
interval = switchInterval;
//reset this TIMER
relay1Time = RESETtime;
digitalWrite(Relay1, RelayOFF);
}
} //END of this switch
//********************************* Switch2
currentState = digitalRead(Switch2);
//did this switch change state ?
if (lastSwitch2 != currentState)
{
//update to the new state
lastSwitch2 = currentState;
//if we are not timing, is the switch now pushed ?
if (TimerFlag2 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
TimerFlag2 = ENABLED;
//set the interval for the relay TIMER
interval = switchInterval;
//reset this TIMER
relay2Time = RESETtime;
digitalWrite(Relay2, RelayOFF);
}
} //END of this switch
//********************************* Switch3
currentState = digitalRead(Switch3);
//did this switch change state ?
if (lastSwitch3 != currentState)
{
//update to the new state
lastSwitch3 = currentState;
//if we are not timing, is the switch now pushed ?
if (TimerFlag3 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
TimerFlag3 = ENABLED;
//set the interval for the relay TIMER
interval = switchInterval;
//reset this TIMER
relay3Time = RESETtime;
digitalWrite(Relay3, RelayOFF);
}
} //END of this switch
//********************************* Switch4
currentState = digitalRead(Switch4);
//did this switch change state ?
if (lastSwitch4 != currentState)
{
//update to the new state
lastSwitch4 = currentState;
//if we are not timing, is the switch now pushed ?
if (TimerFlag4 == DISABLED && currentState == PUSHED)
{
//enable this TIMER
TimerFlag4 = ENABLED;
//set the interval for the relay TIMER
interval = switchInterval;
//reset this TIMER
relay4Time = RESETtime;
digitalWrite(Relay4, RelayOFF);
}
} //END of this switch
//*********************************
} //END of checkSwitches()
// r f i d T a g s ( )
//********************************************^************************************************
void checkrfidTags()
{
//********************************* port Serial
//are any characters in the buffer on this port ?
while (Serial.available() > 0)
{
//Don't allow more than 12 characters, i.e. 0 thru 11 only
if (count1 < 12)
{
input1[count1] = Serial.read();
count1++;
}
else
{
//clear out extra characters if any
while (Serial.available() > 0)
{
count1 = 0;
//throw away this character
Serial.read();
}
//clear out the array
for (byte x = 0; x < 12; x++)
{
input1[x] = 0;
}
}
}
//have we received all 12 characters 0 thru 11 ?
if (count1 == 12)
{
//get ready for next string of received characters
count1 = 0;
//go thru all the Tags on file to see if we find a match
for (int i = 0; i < 5; i++)
{
//do we have a compare with the received Tag to the Tags on file ?
if (TimerFlag1 == DISABLED && strcmp(Valid_Tags_Door1[i] , input1) == 0)
{
//enable this TIMER
TimerFlag1 = ENABLED;
//set the Timer interval
interval = rfidInterval;
//reset this TIMER
relay1Time = RESETtime;
digitalWrite(Relay1, RelayOFF);
}
}
}
//*********************************
} //END rfidTags()s
//********************************************^************************************************
My Thoughts on how to accomplish this:
const int Switch_Pin1 = 4;
const int Switch_Pin2 = 5;
const int Switch_Pin3 = 6;
const int Switch_Pin4 = 7;
const int Relay_Pin1 = 8;
const int Relay_Pin2 = 9;
const int Relay_Pin3 = 10;
const int Relay_Pin4 = 12;
void setup()
{
// begin serial port with baud rate 9600bps
Serial.begin(9600);
pinMode(Switch_Pin1, INPUT);
pinMode(Switch_Pin2, INPUT);
pinMode(Switch_Pin3, INPUT);
pinMode(Switch_Pin4, INPUT);
pinMode(Relay_Pin1, OUTPUT);
pinMode(Relay_Pin2, OUTPUT);
pinMode(Relay_Pin3, INPUT);
pinMode(Relay_Pin4, INPUT);
digitalWrite(Relay_Pin1, LOW);
digitalWrite(Relay_Pin2, LOW);
digitalWrite(Relay_Pin3, LOW);
digitalWrite(Relay_Pin4, LOW);
}
void loop()
{
int Switch_State1 = digitalRead(Switch_Pin1);
int Switch_State2 = digitalRead(Switch_Pin2);
int Switch_State3 = digitalRead(Switch_Pin3);
int Switch_State4 = digitalRead(Switch_Pin4);
Serial.println(Switch_State1);
Serial1.println(Switch_State2);
Serial2.println(Switch_State3);
Serial3.println(Switch_State4);
static unsigned int StartTime1;
static unsigned int StartTime2;
static unsigned int StartTime3;
static unsigned int StartTime4;
if (Switch_State1 == LOW && (millis() - StartTime1) >= (4000) ) { // Restricts the output from restarting like delay() would
StartTime1 = millis();
}
if (Switch_State2 == LOW && (millis() - StartTime2) >= (4000) ) { // Restricts the output from restarting like delay() would
StartTime2 = millis();
}
if (Switch_State3 == LOW && (millis() - StartTime3) >= (4000) ) { // Restricts the output from restarting like delay() would
StartTime3 = millis();
}
if (Switch_State4 == LOW && (millis() - StartTime4) >= (4000) ) { // Restricts the output from restarting like delay() would
StartTime3 = millis();
}
digitalWrite(Relay_Pin4, (millis() - StartTime1) < (4000));
digitalWrite(Relay_Pin4, (millis() - StartTime2) < (4000));
digitalWrite(Relay_Pin4, (millis() - StartTime3) < (4000));
digitalWrite(Relay_Pin4, (millis() - StartTime4) < (4000));
}
This felt a lot easier than other examples I've seen
Z
How can copy/paste cause errors like that? This is the second time you mentioned a copy paste error in this thread. Are you re-typing the code in to this forum?