You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
946 B
59 lines
946 B
int sTx = 2; |
|
int sRx = 3; |
|
|
|
long base = 100; |
|
long rst = base*10; // reset |
|
boolean bt = false; |
|
long dif = 0; |
|
long lt = 0; |
|
long now = 0; |
|
char rcv = 0; |
|
int idx = 0; |
|
String message; |
|
void setup(){ |
|
pinMode(sRx,INPUT); |
|
pinMode(sTx,OUTPUT); |
|
attachInterrupt(digitalPinToInterrupt(sRx),change,CHANGE); |
|
Serial.begin(115200); |
|
Serial.println("Receiver ready"); |
|
} |
|
|
|
void reset(long dif){ |
|
rcv = 0; |
|
idx = -1; |
|
message.reserve(32); |
|
} |
|
void change(){ |
|
bool bt = digitalRead(sRx); |
|
now = micros(); |
|
dif = now - lt; |
|
lt = now; |
|
if (dif > rst){ |
|
reset(dif); |
|
} else { |
|
handle(!bt,dif); |
|
} |
|
} |
|
|
|
void handle(boolean bt, long dif){ |
|
long count = dif/base; |
|
for (int i=0; i<count; i++) push(bt); |
|
} |
|
|
|
void push(boolean bt){ |
|
if (idx>=0) rcv |= bt<<idx; |
|
idx++; |
|
|
|
if (idx == 8){ |
|
if (rcv == 13) { |
|
Serial.println(message); |
|
message = ""; |
|
} else { |
|
message+=rcv; |
|
} |
|
idx =0; |
|
rcv =0; |
|
} |
|
} |
|
|
|
void loop(){}
|
|
|