-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmegadecoder.ino
106 lines (89 loc) · 2.41 KB
/
megadecoder.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
const int rxPin = 8;
void setup() {
pinMode(rxPin, INPUT);
Serial.begin(115200);
Serial.println("Ready to receive...");
}
bool wait_off(unsigned long delay) {
unsigned long start = micros();
while(micros() - start < delay) {
if (digitalRead(rxPin)) {
return false;
}
}
return true;
}
void displayOutput(unsigned long data) {
unsigned int code = (data >> 3);
Serial.print(data, HEX);
Serial.print("\t(");
Serial.print(code, DEC);
Serial.println(")");
}
// Returns the time the bit was received.
// Or 0 for timeout or other framing issue.
// A zero start time of 0 means we are waiting for the first bit.
unsigned long time_next_bit(unsigned long start_time) {
if (start_time == 0) {
// Receiving first bit.
// Ensure radio silence between transmissions.
while (wait_off(8500)) {}; // 8.5uS
while (!digitalRead(rxPin)) {}; // Wait without Deadline
unsigned long now = micros();
delayMicroseconds(1200); // Wait for bit to pass.
if (digitalRead(rxPin)) {
Serial.println("first bit: overrun.");
return 0; // Still receiving a signal after 1.2ms.
}
return now;
}
long delay = start_time - micros();
if (delay > 0) {
delayMicroseconds(delay);
}
// Receiving a followup bit.
while(!digitalRead(rxPin)) {
delay = micros() - start_time;
if (delay > 4500) {
Serial.println("followup bit: timeout");
break; // Timeout.
}
}
unsigned long now = micros();
delayMicroseconds(1200); // Wait for bit to pass.
if (digitalRead(rxPin)) {
Serial.println("followup bit: overrun.");
return 0; // Still receiving a signal after 1.2ms.
}
return now;
}
void loop() {
unsigned long start_time = 0;
start_time = time_next_bit(0);
start_time += 2000;
unsigned long data = 1;
int bits = 1;
while (bits++ < 24) {
unsigned long next_start = time_next_bit(start_time);
long delay = next_start - start_time;
if (500 < delay && delay < 1500) {
start_time = next_start + 5000;
data<<=1; // Push bit value 0.
continue;
}
if (3500 < delay && delay < 4500) {
start_time = next_start + 2000;
data<<=1;
data|=1; // Push bit value 1.
continue;
}
// Else failure.
Serial.print("Bit ");
Serial.print(bits);
Serial.print(" received at ");
Serial.print(delay);
Serial.println(" microseconds.");
return;
}
displayOutput(data);
}