diff --git a/Receiver/Receiver.ino b/Receiver/Receiver.ino index 3c75939..b5be42b 100644 --- a/Receiver/Receiver.ino +++ b/Receiver/Receiver.ino @@ -1,19 +1,59 @@ -#include - -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=0) rcv |= bt< - 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; } -void loop(){ - printSerial(); - delay(id); +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); - mySerial.print("Arduino #"); - mySerial.print(id); - mySerial.print(": Counter = "); - mySerial.println(counter); - mySerial.flush(); + 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); - printSerial(); + Serial.println(success ? "success" : "failed"); + return success; +} + +void loop(){ counter++; + String s = "Hier ist "; + s.concat(id); + s+=": counter = "; + s.concat(counter); + while (!sendString(s)) delay(10*base); + delay(id); }