-
Notifications
You must be signed in to change notification settings - Fork 1
/
receiver.ino
103 lines (84 loc) · 2.03 KB
/
receiver.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
#include <TimerOne.h>
#include "Manchester.h"
#define SYMBOL_PERIOD 2000
#define SAMPLE_PERIOD 100
#define SAMPLE_PER_SYMBOL (SYMBOL_PERIOD / SAMPLE_PERIOD)
#define DOUBLE_SYMBOL_THRESHOLD 1.2
#define NUM_SYMBOLS 20
#define LEVEL_THRESHOLD 5
#define SYN 0x04
#define STX 0x02
#define ETX 0x03
#define SENSOR_PIN A0
#define LED_PIN 2
int state = 0, steadyCounter = 0;
int vertexValue = 0;
unsigned long symbols = 0;
int symbolCounter;
enum ParsingState {
Idle,
Started,
};
ParsingState ps = Idle;
void pushSymbol(int symbol) {
char ch;
symbols = (symbols << 1) | state;
symbolCounter++;
if (symbolCounter >= NUM_SYMBOLS) {
if (ps == Idle) {
if (checkWord(symbols) && decodeWord(symbols, &ch)) {
if (ch == SYN) {
digitalWrite(LED_PIN, HIGH);
symbolCounter = 0;
} else if (ch == STX) {
ps = Started;
symbolCounter = 0;
} else {
digitalWrite(LED_PIN, LOW);
}
} else {
digitalWrite(LED_PIN, LOW);
}
} else {
if (checkWord(symbols) && decodeWord(symbols, &ch)) {
if (ch == ETX) {
ps = Idle;
symbolCounter = 0;
Serial.println();
} else {
symbolCounter = 0;
Serial.print(ch);
}
} else {
ps = Idle;
Serial.println();
}
}
}
}
void timerInterrupt() {
int sensorValue = analogRead(SENSOR_PIN);
if (abs(sensorValue - vertexValue) >= LEVEL_THRESHOLD) {
state = sensorValue < vertexValue;
pushSymbol(state);
vertexValue = sensorValue;
steadyCounter = 0;
} else {
if ((!state && sensorValue > vertexValue) || (state && sensorValue < vertexValue)) {
vertexValue = sensorValue;
}
steadyCounter++;
if (steadyCounter >= SAMPLE_PER_SYMBOL * DOUBLE_SYMBOL_THRESHOLD) {
pushSymbol(state);
steadyCounter = 0;
}
}
}
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Timer1.initialize(SAMPLE_PERIOD);
Timer1.attachInterrupt(timerInterrupt);
}
void loop() {
}