Serial1 on the Arduino Leonardo is hardware serial, not software.
There are several ways to read in multiple bytes from Serial.
Instead of detecting Serial.available()
(i.e. non-zero), change it to Serial.available() >= 2
if (Serial.available() >= 2) {
for (int i=0; i<2; i++) {
buffer[i] = Serial.read();
}
}
Or you can leave it as it is, and read into the buffer one at a time.
static int bufferIndex;
if (Serial.available())
{
buffer[bufferIndex++] = Serial.read()
}
You'll need to detect the contents of the buffer in another if
statement, and also deal with the bounds of the index.
Honestly though, noise is your issue here - this is an example of an XY problem. There's nothing inherent in using a microcontroller with relays that should mean that serial won't work.
- 5.4k
- 7
- 34
- 51