-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser_Data_receive.ino
51 lines (44 loc) · 1.08 KB
/
laser_Data_receive.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
const int sensorPin = A0;
const int sensorThreshold = 500;
// Configuration
const int baudRate = 2400;
const int interval = 1000000 / baudRate;
const int startBitCount = 3;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
}
void loop() {
if (analogRead(sensorPin) > sensorThreshold) {
// Wait for start bits
bool startBitsDetected = true;
for (int i = 0; i < startBitCount; i++) {
delayMicroseconds(interval);
if (analogRead(sensorPin) <= sensorThreshold) {
startBitsDetected = false;
break;
}
delayMicroseconds(interval);
}
if (startBitsDetected) {
// Receive message
while (true) {
char receivedByte = receiveByte();
if (receivedByte == '\0') break;
Serial.print(receivedByte);
}
Serial.println();
}
}
}
char receiveByte() {
char receivedByte = 0;
for (int i = 0; i < 8; i++) {
delayMicroseconds(interval);
if (analogRead(sensorPin) > sensorThreshold) {
receivedByte |= (1 << i);
}
delayMicroseconds(interval);
}
return receivedByte;
}