  * EINES DELIKTES ODER ANDERS IM ZUSAMMENHANG MIT DER SOFTWARE ODER SONSTIGER VERWENDUNG DER SOFTWARE ENTSTANDEN.
  * 
  */
-#include <RCSwitch.h>
-RCSwitch mySwitch = RCSwitch();
-int RO = 1; // LOW = Versorge Relais (VCC) mit Spannung HIGH = schalte Spannung ab (PNP Transistor)
-int S1 = 4; // Relais 1
-int S2 = 3; // Relais 2
+#include <RemoteReceiver.h>
+char version = "1.2";
+int RO = PB1; // LOW = Versorge Relais (VCC) mit Spannung HIGH = schalte Spannung ab (PNP Transistor)
+int S1 = PB4; // Relais 1
+int S2 = PB3; // Relais 2
+/*
+int RO = 8;
+int S1 = 9;
+int S2 = 10;
+*/
 // da bei ATtiny85 bereits alle Pins gebraucht werden, muessen wir uns mit einem Boolean Feld zufrieden geben.
 // die Boolean Felder sind fuer uns sozusagen der Ersatz von 2 zusaetzlichen Input Pins
-boolean S1_State = false;
-boolean S2_State = false;
-boolean RO_State = false;
+boolean S1State=false;
+boolean S2State=false;
+boolean ROState=false;
 char mode[4]; // ein Reservierter Speicher fuer Modus ON / OFF
 unsigned long relaistime = 60000L; // Schalte die Relais Versorgungsspannung nach 1min. ab.
 // Wie lange das Relais den Status ON hat bis es wieder abgeschalten wird. Somit kann es
 // einen bestimmten Weg zuruecklegen.
 // unser Lastenaufzug schaft 6m pro Minute somit habe ich es auf 1m selbstfahren beschraenkt
-unsigned long worktime = 5000;
+unsigned long worktime = 5000L;
+
+unsigned long task2 = 0;
 
 void setup() {
   pinMode(RO, OUTPUT);


  // schalte das Relais nach eingestellter Zeit aus
  if ((unsigned long)(currmillis - task2) >= relaistime) {
    if (RO_State == true) {
      SWRelais("OFF");
    }
  }

  digitalWrite(S1, HIGH);
  digitalWrite(S2, HIGH);
  digitalWrite(RO, HIGH); // Pin auf HIGH Setzen
  
  RemoteReceiver::init(0, 0, 3, checkCode, 0); //RemoteReceiver::init(type, interrupt-pin, min-Repeats, callback-function, PulseWidth);

  //only for tests
  //Serial.begin(19200);
  //Serial.println("Start programm");
}

void SWRelais(char mode){
  /*
   * Powerswitch for relais modul
   */
  if (mode == "ON") {
    if (ROState == false) {
      // Schalte Versorgungspsannung am Relais EIN
      digitalWrite(RO, LOW);
      ROState=true;
    } else if (ROState == true) {
      // do nothing
    }
  } else {
    // Sicherheitshalber bei Aufruf das Relais ausschalten
    digitalWrite(RO, HIGH);
    ROState=false;
  }
  //Serial.println(ROState);
}

void SecRelais1(char mode){
  /*
   * Relais 1 with state check
   */
  if (mode == "ON") {
    if (S1State == false && S2State == false) {
      S1State=true;
      digitalWrite(S1, LOW);
    } else if (S1State == false && S2State == false) {
      digitalWrite(S2, HIGH);
      S2State=false;
      delay(80);
      S1State=true;
      digitalWrite(S1, LOW);
    } else if (S1State == true && S2State == false) {
      // do nothing
    }
  } else {
    digitalWrite(S1, HIGH);
    S1State=false;
  }
  //Serial.println(S1State);
}

void SecRelais2(char mode){
  /*
   * Relais 2 with state check
   */
  if (mode == "ON") {
    if (S2State == false && S1State == false) {
      S2State=true;
      digitalWrite(S2, LOW);
    } else if (S2State == false && S1State == true) {
      digitalWrite(S1, HIGH);
      S1State=false;
      delay(80);
      S2State=true;
      digitalWrite(S2, LOW);
    } else if (S2State == true && S1State == false) {
      // do nothing
    }
  } else {
    digitalWrite(S2, HIGH);
    S2State=false;
  }
  //Serial.println(S2State);
}

//Callback function is called only when a valid code is received.
char checkCode(unsigned long receivedCode, unsigned int period){
  //Note: interrupts are disabled. You can re-enable them if needed.

  //print received code
  //Serial.print("Code = ");
  //Serial.println(receivedCode);

  if (receivedCode == 531272){
    task2 = millis();
    //Serial.println("Switch Relais 1 ON");
    SWRelais("ON");
    SecRelais1("ON");
  
  } else if (receivedCode == 531380){
    //Serial.println("Switch Relais 2 ON");
    task2 = millis();
    SWRelais("ON");
    SecRelais2("ON");
  
  } else if ( receivedCode == 531276 || receivedCode == 531436 || receivedCode == 531384){
    //Serial.println("Switch Relais 1 and 2 OFF");
    SecRelais2("OFF");
    SecRelais1("OFF");
  }
  
}

void loop() {
  unsigned long currmillis = millis();
  
  if ((unsigned long)(currmillis - task2) >= worktime) {
    if ( S1State == true || S2State == true ) {
      //Serial.println("Switch Relais after worktime OFF");
      SecRelais1("OFF");
      SecRelais2("OFF");
    }
