Skip to content

Instantly share code, notes, and snippets.

@smetronic
Created November 15, 2019 18:50
Show Gist options
  • Save smetronic/1533388033b6cf4975a39ab565712b5b to your computer and use it in GitHub Desktop.
Save smetronic/1533388033b6cf4975a39ab565712b5b to your computer and use it in GitHub Desktop.
Code to toggle relay over UART communication.
int d1 = 2;
int d2 = 3;
byte data[3] = {};
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
//{0x00,0x01,0x01, 0x0A}
Serial.readBytesUntil(0x0A, data, 4);
//Switch 1
if (data[0] == 0x00) {
if (data[1] == 0x01) {
if (data[2] == 0x01) {
pinMode(d1, OUTPUT);
}
if (data[2] == 0x00) {
pinMode(d1, INPUT);
}
}
}
//Switch 2
if (data[0] == 0x00) {
if (data[1] == 0x02) {
if (data[2] == 0x01) {
pinMode(d2, OUTPUT);
}
if (data[2] == 0x00) {
pinMode(d2, INPUT);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment