Skip to content

Instantly share code, notes, and snippets.

@auycro
Last active October 5, 2015 14:16
Show Gist options
  • Save auycro/66ad03add77505ba442c to your computer and use it in GitHub Desktop.
Save auycro/66ad03add77505ba442c to your computer and use it in GitHub Desktop.
//Copyright (c) 2015 Gumpanat Keardkeawfa
//Licensed under the MIT license
//LED VAR
const int ledPin = 13;
//Read VAR
boolean nodeconnect = false;
boolean messageComplete = false;
String inputString = "";
void setup() {
// initialize serial:
Serial.begin(9600);
// initialize digital pin 13 as an output.
pinMode(ledPin, OUTPUT);
}
void loop() {
// Recieve data from Node and write it to a String
while (Serial.available() && messageComplete == false) {
char inChar = (char)Serial.read();
if(inChar == ';'){ //check end character
messageComplete = true;
}
else{
inputString += inChar;
}
}
// ProcessMessage
if(!Serial.available() && messageComplete == true)
{
messageComplete = false;
if (inputString == "hello") {
nodeconnect = true;
Serial.print("wait");
}else if(inputString == "close") {
nodeconnect = false;
Serial.print("finish");
delay(1000);
}else{
int blinkTimes = inputString.toInt();
blink(blinkTimes);
Serial.print("blink");
}
inputString = "";
}
//CheckNodeConnect()
if (!nodeconnect){
Serial.print("hello");
delay(1000);
}
}
//Blink(x) function
void blink(int x){
for (int i = 0; i < x; i++){
digitalWrite(ledPin,HIGH);
delay(200);
digitalWrite(ledPin,LOW);
delay(200);
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment