Hausautomatisierungs-Platine + Arduino-Software
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.

87 lines
1.7 KiB

int sTx = 4;
int sRx = 5;
long base = 100;
int id = 0;
int baud = 19200;
int enable = 2;
char c;
int counter = 0;
void setup(){
pinMode(A0,INPUT);
id = analogRead(A0);
pinMode(enable,OUTPUT);
digitalWrite(enable,LOW);
pinMode(sTx,OUTPUT);
digitalWrite(sTx,LOW);
pinMode(sRx,INPUT);
Serial.begin(115200);
Serial.print("Baud rate set to ");
Serial.println(baud);
Serial.print("Initialized Arduino #");
Serial.println(id);
}
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(){
counter++;
String s = "Hier ist ";
s.concat(id);
s+=": counter = ";
s.concat(counter);
while (!sendString(s)) delay(10*base);
delay(id);
}