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