
delay(400);
}
}
The
get_relays_status
function is of particular interest. Getting the status of the relays works like an I C buffer.
Depending on the number of bytes requested, you will get the status of that many relays. For example, if you
request the status of relay one and four bytes, then you’ll get the status of relay one plus the other three. If you
request relay one and two bytes, then you’ll get the status of relay one and two. If you request the status of relay
three and three bytes, then you’ll get the status of relay three, relay four, and relay one.
void get_relays_status(){
int i = 1;
Wire.beginTransmission(RELAY_ADDR);
// Change the Wire.write statment below to the desired relay you want to check on.
Wire.write(RELAY_ONE_STATUS);
Wire.endTransmission();
// We want the states of all the relays so we're requesting four bytes, i.e.
// four relays. If you request 2 bytes, it will give you the state of two relays!
Wire.requestFrom(RELAY_ADDR, 4);
// Print it out, 0 == OFF and 15 == ON.
while(Wire.available()){
Serial.print("Relay ");
Serial.print(i);
Serial.print(": ");
Serial.println(Wire.read());
i++;
}
}
Now let's upload some code via the Arudino IDE. Before uploading, be sure to remove power to the load when
uploading to safely handle the relay. Then connect the Arduino to your computer to upload. Select the board (in
this case the
Arduino/Genuino Uno
) and COM port that your Arduino has enumerated to. Click the upload button.
When the code has finished uploading, place the Arduino and relay on a non-conductive surface to test.
Remember to not touch the relay’s contacts when the system is powered.
Let There Be Light!
After we load up the code, and press the button we should see the relay one LED light up.
2