85818e059b07cc7582448f10073953e20ebb5f8c
[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 int int_0 = 300; // ms
14 int int_1 = 900; // ms
15 int wait  = 2000; // ms
16 int repeat = 5; // times
17
18 void setup() {
19    pinMode(LED_PIN, OUTPUT);
20    pinMode(TX_PIN, OUTPUT);
21    
22    Serial.begin(9600);
23    Serial.println("1 or 2 to turn light sockets");
24    Serial.println("q/a - 0 inteval +/- 100 ms");
25    Serial.println("w/s - 1 inteval +/- 100 ms");
26    Serial.println("e/d - wait      +/- 100 ms");
27    Serial.println("r/f - repeat    +/- 1");
28 }
29
30
31 void send(char *code) {
32   Serial.print("send ");
33   Serial.println(code);
34   
35   // we have to send same signal at least two times
36   for(int r = 0; r < repeat; r++ ) {
37     
38     digitalWrite(LED_PIN, HIGH);
39
40     for(int i = 0; i < strlen(code); i++) {
41       int i1 = int_0;
42       int i2 = int_1;
43       if (code[i] == '1' ) {
44         i1 = int_1;
45         i2 = int_0;
46       }
47       digitalWrite(TX_PIN, HIGH);
48       delayMicroseconds(i1);
49       digitalWrite(TX_PIN, LOW);
50       delayMicroseconds(i2);
51     }
52
53     delayMicroseconds(wait); // guess
54   }
55
56   digitalWrite(LED_PIN, LOW);
57 }
58
59 void loop() {
60   if(Serial.available()) {
61     int in = Serial.read();
62     if (in == '1') {
63       send("1000100110110000000000010");
64     } else if (in == '2') {
65       send("1011001001011111000000010");
66
67     } else if (in == 'q') {
68       int_0 += 100;
69       Serial.print("inteval 0 = ");
70       Serial.println(int_0);
71     } else if (in == 'a') {
72       int_0 -= 100;
73       Serial.print("inteval 0 = ");
74       Serial.println(int_0);
75
76     } else if (in == 'w') {
77       int_1 += 100;
78       Serial.print("inteval 1 = ");
79       Serial.println(int_1);
80     } else if (in == 's') {
81       int_1 -= 100;
82       Serial.print("inteval 1 = ");
83       Serial.println(int_1);
84
85     } else if (in == 'e') {
86       wait += 100;
87       Serial.print("wait = ");
88       Serial.println(wait);
89     } else if (in == 'd') {
90       wait -= 100;
91       Serial.print("wait = ");
92       Serial.println(wait);
93
94     } else if (in == 'r') {
95       repeat += 1;
96       Serial.print("repeat = ");
97       Serial.println(repeat);
98     } else if (in == 'f') {
99       repeat -= 1;
100       Serial.print("repeat = ");
101       Serial.println(repeat);
102
103     } else {
104       Serial.print("ignored ");
105       Serial.println(in);
106     }
107   }
108 }