Erste Verison mit Kollisions-Vermeidung.
Kollisions-Erkennung sollte auch funktionierten, aber das ist nicht ganz klar. Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,19 +1,59 @@
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
int baud = 19200;
|
||||
int sTx = 4;
|
||||
int sRx = 5;
|
||||
SoftwareSerial mySerial(sRx,sTx);
|
||||
int sTx = 2;
|
||||
int sRx = 3;
|
||||
|
||||
long base = 800;
|
||||
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);
|
||||
mySerial.begin(baud);
|
||||
Serial.println("Receiver ready");
|
||||
}
|
||||
|
||||
void loop(){
|
||||
if (mySerial.available() > 0){
|
||||
char c = mySerial.read();
|
||||
Serial.print(c);
|
||||
}
|
||||
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(){}
|
||||
|
||||
Reference in New Issue
Block a user