/* | |
sample to try control led from open hab | |
*/ | |
// constants won't change. They're used here to | |
// set pin numbers: | |
const int switchPin = 2; // the number of the pushbutton pin | |
const int ledPin = 13; // the number of the LED pin | |
// variables will change: | |
int buttonState = 0; // variable for reading the pushbutton status | |
boolean ledOn = false; | |
boolean currentButton = false; | |
boolean lastButton = false; | |
boolean debounce(boolean last) | |
{ | |
boolean current = digitalRead(switchPin); | |
if (last != current) | |
{ | |
delay(5); | |
current = digitalRead(switchPin); | |
} | |
} | |
void setup() { | |
// initialize the LED pin as an output: | |
pinMode(ledPin, OUTPUT); | |
// initialize the pushbutton pin as an input: | |
pinMode(switchPin, INPUT); | |
Serial.begin(9600); | |
} | |
void printButtonState(bool state) { | |
Serial.print("item=Light_GF_Living_Ceiling_1"); | |
Serial.print(", "); | |
Serial.print("value="); | |
Serial.print(state?"ON":"OFF"); | |
Serial.println(";"); | |
} | |
void readsCommand(char *command) { | |
if (Serial.available() > 0) { | |
Serial.readBytes(command, 50); | |
} | |
} | |
void switchLedForCommand(char *command) { | |
if (!strstr(command, "led") > 0) | |
return; | |
if (strstr(command, "red") > 0) { | |
ledOn = strstr(command, "on") > 0; | |
digitalWrite(ledPin,ledOn); | |
} | |
} | |
void loop(){ | |
char command[50]; | |
// read the state of the pushbutton value: | |
currentButton = debounce(lastButton); | |
// check if the pushbutton is pressed. | |
// if it is, the buttonState is HIGH: | |
if (lastButton == LOW && currentButton == HIGH) { | |
ledOn = !ledOn; | |
digitalWrite(ledPin,ledOn); | |
printButtonState(ledOn); | |
} | |
readsCommand(command); | |
if (strstr(command, "led") > 0) { | |
switchLedForCommand(command); | |
} | |
lastButton = currentButton; | |
command[0] = '\0'; | |
delay (100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment