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