3d48c9e49b8e12364b7b5e3d8f8b489165a641a9
[Arduino] / light_sockets / light_sockets.ino
1 #define TX_PIN 7
2 #define LED_PIN 13
3
4 /*
5 codes for my light sockets:
6
7 1000100110110000000000010
8 1011001001011111000000010
9
10 sniffed with rtl-sdr
11 */
12
13 void setup() {
14    pinMode(LED_PIN, OUTPUT);
15    pinMode(TX_PIN, OUTPUT);
16    
17    Serial.begin(9600);
18    Serial.println("1 or 2 to turn light sockets");
19 }
20
21
22 void send(char *code) {
23   Serial.print("send ");
24   Serial.println(code);
25   
26   // we have to send same signal at least two times
27   for(int repeat = 0; repeat < 5; repeat++ ) {
28     
29     digitalWrite(LED_PIN, HIGH);
30
31     for(int i = 0; i < strlen(code); i++) {
32       int i1 = 300;
33       int i2 = 900;
34       if (code[i] == '1' ) {
35         i1 = 900;
36         i2 = 300;
37       }
38       digitalWrite(TX_PIN, HIGH);
39       delayMicroseconds(i1);
40       digitalWrite(TX_PIN, LOW);
41       delayMicroseconds(i2);
42     }
43
44     delayMicroseconds(2000); // guess
45   }
46
47   digitalWrite(LED_PIN, LOW);
48 }
49
50 void loop() {
51   if(Serial.available()) {
52     int in = Serial.read();
53     if (in == '1') {
54       send("1000100110110000000000010");
55     } else if (in == '2') {
56       send("1011001001011111000000010");
57     } else {
58       Serial.print("ignored ");
59       Serial.println(in);
60     }
61   }
62 }