Hi Railroader,
I didn't realise the forum transcribed the code into formatted colour coded it (thats rather clever), I uploaded the code into the editor to make it easier to read. Here is the oriiginal code. Like wise, your thoughts are much appreciated.
Regards,
Josh
const int InputPins[6] = {14, 15, 16, 17, 18, 19};
const int OutputPins[6] = {2, 3, 4, 5, 6, 7};
const int MasterSelectPin = 10;
const int StatusLED = 13;
bool MasterSelect;
char ReceivedData[32];
void setup() {
Serial.begin(9600);
pinMode(MasterSelectPin, INPUT_PULLUP);
pinMode(StatusLED, OUTPUT);
for (int p = 0; p <= 5; p++) {
pinMode(InputPins[p], INPUT_PULLUP);
pinMode(OutputPins[p], OUTPUT);
}
if (digitalRead(MasterSelectPin) == LOW) { // Check the master select pin to see if this unit is the master or slave.
MasterSelect = HIGH;
digitalWrite(StatusLED, HIGH);
} else {
MasterSelect = LOW;
}
}
void loop() {
if (MasterSelect == HIGH) {
SendData();
ReceiveData();
} else {
ReceiveData();
SendData();
}
delay(500);
}
void SendData() {
Serial.print('S'); // Send start flag.
for (int i = 0; i <= 5; i++) { // Read digital input pins then send them one by one via serial. 1 = off, 2 = on.
if (digitalRead(InputPins[i]) == LOW) {
Serial.print('1');
} else {
Serial.print('2');
}
}
Serial.print('E'); // Send end flag.
}
void ReceiveData() {
if (Serial.available() >= 8 && Serial.read() == 'S') { // If there is 8 bytes of data in the receive buffer AND the buffer starts with 'S'
Serial.readBytesUntil('E', ReceivedData, 7); // read the received buffer into the ReceivedData array until 'E' is found.
for (int r = 0; r <= 5; r++) {
if (ReceivedData[r] == '2') {
digitalWrite(OutputPins[r], HIGH); // If a 2 is received, turn on the output.
} else {
digitalWrite(OutputPins[r], LOW); // If anything else is received, turn off.
}
}
}
}