-
Notifications
You must be signed in to change notification settings - Fork 5
/
RemoteCapture.ino
139 lines (111 loc) · 4.69 KB
/
RemoteCapture.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
******************************************************************************************************************************************************************
*
* A-OK protocol remote control capture
* Compatible with AC114-01B remote
*
* Code by Antti Kirjavainen (antti.kirjavainen [_at_] gmail.com)
*
* Use this code to capture the commands from your remotes. Outputs to
* serial (Tools -> Serial Monitor). What you need for mastering your
* motors are 65 bits commands.
*
*
* HOW TO USE
*
* Plug a 433.92MHz receiver to digital pin 2 and start pressing buttons
* from your original remotes (copy pasting them to A-OK.ino).
*
******************************************************************************************************************************************************************
*/
// Plug your 433.92MHz receiver to digital pin 2:
#define RECEIVE_PIN 2
// Enable debug mode if there's no serial output or if you're modifying this code for
// another protocol/device. However, note that serial output delays receiving, causing
// data bits capture to fail. So keep debug disabled unless absolutely required:
#define DEBUG false
#define ADDITIONAL false // Display some additional info after capture
#define COMMAND_LENGTH 65
String last_command = "";
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup()
{
pinMode(RECEIVE_PIN, INPUT);
Serial.begin(9600);
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
int i = 0;
unsigned long t = 0;
String command = "";
// *********************************************************************
// Wait for the AGC bit
// *********************************************************************
// NOTE: Some AC114-01B remotes precede this with a preamble
// (repeated 8 times):
// *********************************************************************
// HIGH of approx. 340 us
// LOW of approx. 520 us
// *********************************************************************
// But we'll ignore this preamble since not all remotes transmit it.
// All remotes start the command with an AGC of HIGH between 4800-5500 us
// *********************************************************************
while (t < 4800 || t > 5500) {
t = pulseIn(RECEIVE_PIN, HIGH, 1000000); // Waits for HIGH and times it
}
if (DEBUG) {
Serial.print("AGC: ");
Serial.println(t);
//return; // If modifying this code for another protocol, stop here
}
// *********************************************************************
// Command bits, locate them simply by HIGH waveform spikes:
// *********************************************************************
// 0 = 25-370 us
// 1 = 500-800 us
// *********************************************************************
while (i < COMMAND_LENGTH) {
t = pulseIn(RECEIVE_PIN, HIGH, 1000000); // Waits for HIGH and times it
if (DEBUG) {
Serial.print(t);
Serial.print(": ");
}
if (t > 25 && t < 370) { // Found 0
command += "0";
if (DEBUG) Serial.println("0");
} else if (t > 500 && t < 800) { // Found 1
command += "1";
if (DEBUG) Serial.println("1");
} else { // Unrecognized bit (could be the next command's starting AGC), finish
if (DEBUG) {
Serial.print("INVALID BIT");
Serial.println(t);
}
i = 0;
break;
}
i++;
}
// *********************************************************************
// Done! Display results:
// *********************************************************************
// Correct data bits length is 65 bits, dismiss bad captures:
if (command.length() != COMMAND_LENGTH) {
Serial.print("Bad capture, invalid command length ");
Serial.println(command.length());
if (ADDITIONAL) {
Serial.println("Invalid command: " + command);
}
} else {
if (command != last_command) { // Only print unique commands to serial for better readability
last_command = command;
Serial.print("Successful capture, command is: ");
Serial.println(command);
} else {
Serial.println("Command repeated");
}
}
}
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------