Created
June 19, 2015 07:27
-
-
Save rehrumesh/f1f8c7322a11e1b10355 to your computer and use it in GitHub Desktop.
Remote output XBee
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; | |
int buttonPin = 2; | |
boolean flag = LOW; | |
boolean buttonState; | |
boolean lastButtonState = LOW; | |
int lastDebounceTime = 0; | |
int debounceDelay = 50; | |
void setup() { | |
pinMode(ledPin, OUTPUT); | |
pinMode(buttonPin, INPUT); | |
digitalWrite(buttonPin, HIGH); | |
Serial.begin(9600); | |
} | |
void loop() { | |
boolean reading = digitalRead(buttonPin); | |
if(reading != lastButtonState){ | |
lastDebounceTime = millis(); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay){ | |
if(reading != buttonState){A | |
buttonState = reading; | |
if(buttonState == LOW){ | |
flag = !flag; | |
if(flag){ | |
setRemoteState(0x5); | |
}else{ | |
setRemoteState(0x4); | |
} | |
} | |
} | |
} | |
digitalWrite(ledPin, flag); | |
lastButtonState = reading; | |
// digitalWrite(ledPin, HIGH); | |
// setRemoteState(0x5); | |
// delay(2000); | |
// digitalWrite(ledPin, LOW); | |
// setRemoteState(0x4); | |
// delay(3000); | |
} | |
void setRemoteState(char value) { | |
Serial.write(0x7E); //start byte | |
Serial.write((byte)0x0); | |
Serial.write(0x10); | |
Serial.write(0x17); // remote AT command | |
Serial.write((byte)0x0); | |
Serial.write((byte)0x0); | |
Serial.write((byte)0x0); | |
Serial.write((byte)0x0); | |
Serial.write((byte)0x0); | |
Serial.write((byte)0x0); | |
Serial.write((byte)0x0); | |
Serial.write(0xFF); | |
Serial.write(0xFF); | |
Serial.write(0xFF); | |
Serial.write(0xFE); | |
Serial.write(0x02); | |
Serial.write('D'); // command 'D3' | |
Serial.write('3'); | |
Serial.write(value); // 4-OFF or 5-ON | |
long sum = 0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'D' + | |
'3' + value; | |
Serial.write(0xFF - (sum & 0xFF)); //checksu | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment