I/O control over serial

It's not bad, you are on the right track. There are a few basic things to make it run smooth which you don't know yet.

Could you describe the protocol of the serial data ?
Should your sketch allow that there is a CarriageReturn or LineFeed at the end ? Are there always 6 pins ? It is allowed to continuously transmit data without any delay in between ? and so on.

There are a few basic rules to do such a project:

  • Always be ready to receive something. Always, all the time. Never delay.
  • If the same sketch is used on both Arduino boards, then there is no Master. They both send the inputs to the other one.
  • We don't use the 'Serial.readBytesUntil()' functions. It is better to make your own code to read the incoming data.
  • In my opinion, the 'S' and 'E' and the fixed length are good enough. You can add a checksum if you want. You can go over-the-top and confirm the data, to let the sender know that the right data is received. You could even encrypt the data, so that no one else can control the output pins. You can do anything that you can think of.

That means:

  • Because receiving data should be possible all the time, sending the data can be done in a millis-timer, or send something when a input changes.
  • In the loop(), read the available data, and check if a full command line has been received. Process the data when it is a full command. Do not wait to receive a full command.
  • You could add a error counter. If the received data is not okay, increase the error counter. Then you know if something went wrong. The goal is 100% reliability.

Give it a try and how us the new sketch here on this forum (put it between code tags, use the </> button).

1 Like