#include <VirtualWire.h>

// LED's
int ledPin = 3;

// Sensors 
int Sensor1Data;
const int relayPin = 2;

// RF Transmission container
char Sensor1CharMsg[2]; 

void setup()
{
  Serial.begin(9600);	// Debugging only
  Serial.println("setup");
  // sets the digital pin as output
  pinMode(ledPin, OUTPUT); 
  pinMode(relayPin, OUTPUT);
  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);	 // Bits per sec
  vw_rx_start();       // Start the receiver PLL running
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) // Non-blocking
  {

    int i;
    //delay(100);
    // Message with a good checksum received, dump it. 

    for (i = 0; i < buflen; i++)
    {
      // Fill Sensor1CharMsg Char array with corresponding 
      // chars from buffer.   
      Sensor1CharMsg[i] = char(buf[i]);
    }
    // Null terminate the char array
    // This needs to be done otherwise problems will occur
    // when the incoming messages has less digits than the
    // one before. 
    Sensor1CharMsg[buflen] = '\0';
    // Convert Sensor1CharMsg Char array to integer
    Sensor1Data = atoi(Sensor1CharMsg);

    // DEBUG 
    Serial.print("Sensor 1 INT: ");
    Serial.println(Sensor1Data);
    // END DEBUG

    if (Sensor1Data == 1)
    {
      digitalWrite(relayPin, HIGH);
      digitalWrite(ledPin, LOW);
    }
    else{
      if (Sensor1Data == 2||3)
        digitalWrite(relayPin, LOW);
      digitalWrite(ledPin, HIGH);
    }
  }
}