Created
November 15, 2019 18:50
-
-
Save smetronic/1533388033b6cf4975a39ab565712b5b to your computer and use it in GitHub Desktop.
Code to toggle relay over UART communication.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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