This repository has been archived on 2024-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
trev/arduino/src/sketch.ino

94 lines
1.9 KiB
Arduino
Raw Normal View History

2016-01-12 20:05:43 +01:00
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
2016-01-18 22:30:36 +01:00
uint8_t message[8];
int index = 0;
boolean com = false;
boolean on = false;
2016-01-11 21:09:53 +01:00
void setup()
{
2016-01-12 20:05:43 +01:00
Serial.begin(9600);
2016-01-16 22:43:32 +01:00
mySwitch.enableTransmit(11);
2016-01-11 21:09:53 +01:00
}
void loop()
{
2016-01-18 22:30:36 +01:00
while(Serial.available()) {
if(index > 0 || (Serial.peek() >= 80 && Serial.peek() <= 95)) {
message[index++] = Serial.read();
if(index > 7){
decodeMessage();
index = 0;
}
2016-01-12 20:05:43 +01:00
}
}
}
2016-01-18 22:30:36 +01:00
void decodeMessage() {
if(com) {
int cmd = message[0];
2016-01-18 22:35:01 +01:00
Serial.println(cmd);
2016-01-18 22:30:36 +01:00
int pin = message[1] - 97;
2016-01-18 22:35:01 +01:00
Serial.println(pin);
2016-01-18 22:30:36 +01:00
int val[6];
memcpy(val, message + 2, 6);
2016-01-18 22:35:01 +01:00
for(int i =0;i<6;i++) {Serial.println(val[i]);}
2016-01-18 22:30:36 +01:00
switch (cmd) {
//case 80: break;
//case 81: break;
//case 82: break;
//case 83: break;
//case 84: break;
//case 85: break;
//case 86: break;
case 87: switchLight(val); break;
//case 88: break;
//case 89: break;
default: break;
}
} else if(message[0] == 90) {
com = true;
Serial.println("Ready!");
} else {
Serial.println("Arduino not configured for communication!");
}
}
void switchLight(int val[]) {
String triStateCode = "";
for(int i = 0; i < 6; i++)
{
String triStatePart = String(val[i], HEX);
triStatePart = (triStatePart.length() < 2) ? String("0" + triStatePart) : triStatePart;
triStateCode.concat(triStatePart);
}
char triState[triStateCode.length()];
triStateCode.toCharArray(triState, triStateCode.length());
if(on) {
controlRCOutlets(triState);
Serial.println("Light is off.");
on = false;
} else {
controlRCOutlets(triState);
Serial.println("Light is on.");
on = true;
}
}
2016-01-12 20:05:43 +01:00
void controlRCOutlets(const char* sCodeWord) {
mySwitch.sendTriState(sCodeWord);
delay(1000);
2016-01-13 09:55:37 +01:00
}