forked from lyriarte/dummycomm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummycomm.ino
191 lines (172 loc) · 3.37 KB
/
dummycomm.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*** 4-ticks transmission frames
* carrier : 1000
* zero: 1100
* one: 1110
*/
#include <Servo.h>
#define BTX 13
#define BRX 8
#define SRV 7
#define CARRIER 2
#define NOFRAME 3
#define NOFRAME_TRESHOLD 0xFFFF
#define ERROR -1
int sleepms;
byte iobyte;
byte bytebits[8];
Servo servo;
int stepperms = 5;
byte steps8[] = {
HIGH, LOW, LOW, LOW,
HIGH, HIGH, LOW, LOW,
LOW, HIGH, LOW, LOW,
LOW, HIGH, HIGH, LOW,
LOW, LOW, HIGH, LOW,
LOW, LOW, HIGH, HIGH,
LOW, LOW, LOW, HIGH,
HIGH, LOW, LOW, HIGH,
};
void step8(byte pin1, byte pin2, byte pin3, byte pin4) {
byte i=0;
while (i<32) {
digitalWrite(pin1, steps8[i++]);
digitalWrite(pin2, steps8[i++]);
digitalWrite(pin3, steps8[i++]);
digitalWrite(pin4, steps8[i++]);
delay(stepperms);
}
}
void stepClkw(byte pin1, byte pin2, byte pin3, byte pin4) {
step8(pin4, pin3, pin2, pin1);
}
void stepCclw(byte pin1, byte pin2, byte pin3, byte pin4) {
step8(pin1, pin2, pin3, pin4);
}
void setup() {
pinMode(BTX, OUTPUT);
pinMode(BRX, INPUT);
servo.attach(SRV);
Serial.begin(9600);
sleepms = 5;
iobyte = 128;
}
byte bytebitsGet() {
byte value=0;
byte power=1;
byte i=0;
while (i<8) {
if (bytebits[i++])
value = value + power;
power = power * 2;
}
return value;
}
void bytebitsSet(byte value) {
byte i=0;
while (i<8) {
if (value & 1)
bytebits[i++] = 1;
else
bytebits[i++] = 0;
value = value >> 1;
}
}
byte readFrame(unsigned int cyclecount0, unsigned int cyclecount1)
{
if (cyclecount0 < cyclecount1) {
if (cyclecount1 < 2 * cyclecount0)
return 0; // c0 < c1 < 2.c0 : 1100
return 1; // 2.c0 < c1 : 1110
} else {
if (cyclecount0 < 2 * cyclecount1)
return 0; // c1 < c0 < 2.c1 : 1100
return CARRIER; // 2.c1 < c0 : 1000
}
return ERROR; // never reach
}
byte getFrame(int pin)
{
unsigned int cyclecount1=0;
unsigned int cyclecount0=0;
while (digitalRead(pin) == HIGH) {
cyclecount1 = cyclecount1 + 1;
}
while (digitalRead(pin) == LOW) {
cyclecount0 = cyclecount0 + 1;
if (cyclecount0 == NOFRAME_TRESHOLD)
return NOFRAME;
}
return readFrame(cyclecount0, cyclecount1);
}
void sendBytebits()
{
byte i=8;
while (i) {
if (bytebits[--i])
send1(BTX, sleepms);
else
send0(BTX, sleepms);
}
}
void sendCarrier(int pin, int sleepms)
{
digitalWrite(pin, HIGH);
delay(sleepms);
digitalWrite(pin, LOW);
delay(3 * sleepms);
}
void send0(int pin, int sleepms)
{
digitalWrite(pin, HIGH);
delay(2 * sleepms);
digitalWrite(pin, LOW);
delay(2 * sleepms);
}
void send1(int pin, int sleepms)
{
digitalWrite(pin, HIGH);
delay(3 * sleepms);
digitalWrite(pin, LOW);
delay(sleepms);
}
void sendByte(byte value) {
byte i;
digitalWrite(BTX, LOW);
for(i=0; i<16; i++) {
sendCarrier(BTX, sleepms);
}
bytebitsSet(value);
sendBytebits();
sendCarrier(BTX, sleepms);
sendCarrier(BTX, sleepms);
}
byte getByte() {
byte frame,i;
i=8;
while (i) {
while((frame = getFrame(BRX)) >= CARRIER) {
if (frame == NOFRAME) {
if (Serial.available() > 0) {
frame = Serial.parseInt();
return frame;
}
}
i=8;
}
bytebits[--i] = frame;
}
return bytebitsGet();
}
void loop() {
unsigned int angle;
sendByte(iobyte);
Serial.print("byte sent: ");
Serial.println(iobyte);
iobyte=getByte();
Serial.print("byte read: ");
Serial.println(iobyte);
angle=((unsigned int)iobyte)*180/255;
Serial.print("servo angle: ");
Serial.println(angle);
servo.write(angle);
}