Created
July 7, 2018 11:39
-
-
Save elgamine-dev/71a2e1f68f0110979d8fa374cb3b785a to your computer and use it in GitHub Desktop.
This file contains 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 ledPin = 13; | |
char incoming; | |
bool checked = true; | |
int id = 1; | |
bool ledState = false; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
delay(500); | |
pinMode(ledPin, OUTPUT); | |
state(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if (Serial.available() > 0) { | |
incoming = Serial.read(); | |
checked = false; | |
//Serial.println("incomgin data"); | |
//Serial.println(incoming); | |
} | |
check(); | |
} | |
void check() { | |
if (checked) { | |
return; | |
} | |
if (incoming == '0') { | |
off(); | |
state(); | |
} | |
if (incoming == '1') { | |
on(); | |
state(); | |
} | |
if (incoming == '2') { | |
toggle(); | |
state(); | |
} | |
if (incoming == '3') { | |
state(); | |
} | |
checked = true; | |
} | |
void on() { | |
digitalWrite(ledPin, HIGH); | |
ledState = true; | |
} | |
void off() { | |
digitalWrite(ledPin, LOW); | |
ledState = false; | |
} | |
void toggle() { | |
if (ledState == true) { | |
off(); | |
return; | |
} | |
on(); | |
} | |
void state() { | |
String msg = (String) id; | |
msg = "{\"id\":"+msg + ",\"state\":" + ledState + "}"; | |
Serial.println(msg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment