I am working in a small project. Right now I can send information to my end device to turn on a lightbulb.
I am building a web site for turning the light of my house. I want as feedback to know when the lightbulb is turn on. (Sometimes I will turn on the light manually, and I will like to know that)
/*
*@joiedufranco
*23APR2012
*rxOnOff
*Recieves packet from remote Coordinator
*Turns LED On or Off
*/
int LED = 11; //Turn this LED on or off, depending on packet rx'd
int debugLED = 13; //Flash light to indicate rx
int packet=-1; //Packet will be two bytes in length
int analogValue;
int PIN=12;
int RELAY=10;
void setup() {
pinMode(LED, OUTPUT);
pinMode(PIN, INPUT);
pinMode(RELAY, INPUT);
pinMode(debugLED, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(Serial.available() >= 16){ //<--Changing the value from 21 to 16 fixed the problem!
if(Serial.read() == 0x7E){ //Look for starting byte
digitalWrite(debugLED, HIGH); //Flash Arduino LED (not the one on digital pin 11)
delay(1000); //to indicate rx'ing data
digitalWrite(debugLED, LOW);
for(int i = 0; i <15; i++){ //Discard unused data (see XBee API protocol for more info)
byte discard = Serial.read();
}
packet = Serial.read(); //This should be the first byte of "meat" data
}
}
if(digitalRead(PIN)==LOW){
if(packet == 0x01){ //If a '1' is pressed on keyboard side
if(digitalRead(PIN)==LOW){
digitalWrite(LED, HIGH); //turn on red LED
packet=-1;
}
}
}
if(digitalRead(PIN) == HIGH){
if(packet == 0x00){
if(digitalRead(PIN)==HIGH)
{
digitalWrite(LED, LOW);
packet=-1;
}
}
}
if(packet == 0x00){ //If a '1' is pressed on keyboard side
if(digitalRead(PIN)==LOW){
digitalWrite(LED, HIGH); //turn on red LED
packet=-1;
}
}
if(packet == 0x01){
if(digitalRead(PIN)==HIGH)
{
digitalWrite(LED, LOW);
packet=-1;
}
}
}