So I'm trying to interface with a piece of wireless equipment that accepts an input that appears as a binary number but is really just the status of 8 relays. For example, 00000001 would indicate that the first relay is on and the other 7 relays are off.
What I'm trying to do is read the status of digital pins 2 through 9, arrange them per that format and write them to my wifi module as one binary byte.
This has been my approach so far:
Read the digital pins through either digitalRead() or PINB/PIND
Convert to string
Modify the strings and combine them in the order I want
Convert back to binary
Send via Serial.write()
This is one of my recent versions that seemed to almost work...
String relay1 = String(digitalRead(2));
String relay2 = String(digitalRead(3));
String relay3 = String(digitalRead(4));
String relay4 = String(digitalRead(5));
String relay5 = String(digitalRead(6));
String relay6 = String(digitalRead(7));
String relay7 = String(digitalRead(8));
String relay8 = String(digitalRead(9));
String relaystatestring = String(relay1 + relay2 + relay3 + relay4 + relay5 + relay6 + relay7 + relay8);
relaystate = byte(relaystatestring.toInt());
Serial.write(relaystate);