Hi there,
My goal:
Create a query from several SoftwareSerial instances, which checks 2 bytes in each case to be able to react accordingly in the program afterwards - for the sake of simplicity only a serial output is implemented in the attached code.
My problem:
I can't get all instances to run via the for loop. I only get the last instance (=Channel 4) to run. If I deactivate the for loop and define a channel, then they all work - but only separately.
My question:
Just like almost everyone else in the forum: What could be the problem? Or how could a solution look like?
Additional info:
- Arduino Nano is used.
- At SoftwareSerial the data will arrive every 50 ms from the slaves.
I hope I have formulated this clearly and have not forgotten any important data - otherwise please feel free to ask.
Thank you very much in advance for your thought and feedback.
#include <SoftwareSerial.h>
SoftwareSerial softSerial[5] = { SoftwareSerial (7, 11),
SoftwareSerial (8, 19),
SoftwareSerial (9, 20),
SoftwareSerial (10, 21),
SoftwareSerial (12, 22)
};
#define numsoftSerial 5 // number of Softwarechannels
int inByte = 0; // variable used for saving received bytes on SoftwareSerial port
byte ReadDataState = 0; // used for states of state machine 0-waiting for first byte
void setup() {
Serial.begin(115200);
for (byte i = 0; i < numsoftSerial; i++) { //begin all SoftwareSerial shannels
softSerial[i].begin(115200);
}
Serial.println("Setup ready, starting ...");
}
void loop() {
for (byte i = 0; i < numsoftSerial; i++) { //loop through all SoftwareSerial channels
if (softSerial[i].available()>0) {
inByte = softSerial[i].read();
// Serial.println(inByte, HEX);
switch (ReadDataState) {
case 0: // first byte received
if (0x94 == inByte || (0xFD == inByte)) //if first byte is correct move to next state
{
ReadDataState = 1;
}
break;
case 1: // second byte received
if ((0x51 == inByte) || (0x52 == inByte) || (0x43 == inByte)) {
ReadDataState = 2; //if second byte was correct go to "button pressed state", if not go to first state and wait for next packet
}
else {
ReadDataState = 0; //second byte has worng value, wait for next packet (serial event)
}
break;
case 2:
Serial.println("Match @ Channel: " + String(i, DEC));
ReadDataState = 0;
break;
}
}
}
}