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(){}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
int sTx = 4;
|
||||
int sRx = 5;
|
||||
SoftwareSerial mySerial(sRx,sTx);
|
||||
|
||||
long base = 800;
|
||||
|
||||
int id = 0;
|
||||
int baud = 19200;
|
||||
int enable = 2;
|
||||
char c;
|
||||
int counter = 0;
|
||||
|
||||
void setup(){
|
||||
pinMode(A0,INPUT);
|
||||
id = analogRead(A0)*10;
|
||||
|
||||
pinMode(enable,OUTPUT);
|
||||
mySerial.begin(baud);
|
||||
digitalWrite(enable,LOW);
|
||||
|
||||
pinMode(sTx,OUTPUT);
|
||||
digitalWrite(sTx,LOW);
|
||||
|
||||
pinMode(sRx,INPUT);
|
||||
Serial.begin(115200);
|
||||
Serial.print("Baud rate set to ");
|
||||
Serial.println(baud);
|
||||
@@ -21,23 +27,61 @@ void setup(){
|
||||
Serial.println(id);
|
||||
}
|
||||
|
||||
void printSerial(){
|
||||
while (mySerial.available()){
|
||||
c = mySerial.read();
|
||||
Serial.print(c);
|
||||
bool sendBit(bool bt){
|
||||
digitalWrite(sTx,bt);
|
||||
delayMicroseconds(80);
|
||||
if (digitalRead(sTx) != bt) return false;
|
||||
delayMicroseconds(base-80);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sendChar(char c){
|
||||
return sendBit(c & 0x01)
|
||||
&& sendBit(c & 0x02)
|
||||
&& sendBit(c & 0x04)
|
||||
&& sendBit(c & 0x08)
|
||||
&& sendBit(c & 0x10)
|
||||
&& sendBit(c & 0x20)
|
||||
&& sendBit(c & 0x40)
|
||||
&& sendBit(c & 0x80);
|
||||
}
|
||||
|
||||
bool sendString(String s){
|
||||
Serial.print("Sending '"+s+"'…");
|
||||
boolean busy = false;
|
||||
for (int i = 0; i<11; i++){
|
||||
if (!digitalRead(sRx)) {
|
||||
if (!busy){
|
||||
Serial.print("busy…");
|
||||
busy = true;
|
||||
}
|
||||
i = 0; // wait for free line
|
||||
}
|
||||
delayMicroseconds(base);
|
||||
}
|
||||
digitalWrite(enable,HIGH);
|
||||
bool success = sendBit(0); // erste Flanke erzeugen
|
||||
if (success) {
|
||||
for (char c : s){
|
||||
if (!sendChar(c)){
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
success = success && sendChar(13);
|
||||
success = success && sendBit(1); // letzte Flanke erzeugen
|
||||
digitalWrite(enable,LOW);
|
||||
Serial.println(success ? "success" : "failed");
|
||||
return success;
|
||||
}
|
||||
|
||||
void loop(){
|
||||
printSerial();
|
||||
delay(id);
|
||||
digitalWrite(enable,HIGH);
|
||||
mySerial.print("Arduino #");
|
||||
mySerial.print(id);
|
||||
mySerial.print(": Counter = ");
|
||||
mySerial.println(counter);
|
||||
mySerial.flush();
|
||||
digitalWrite(enable,LOW);
|
||||
printSerial();
|
||||
counter++;
|
||||
String s = "Hier ist ";
|
||||
s.concat(id);
|
||||
s+=": counter = ";
|
||||
s.concat(counter);
|
||||
while (!sendString(s)) delay(10*base);
|
||||
delay(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user