//CODE SOURCE - PROJET DOMOTIQUE - SIMON GARNIER pour DFS Q2 2019
//Partie capteur alarme à utiliser sur Arduino UNO
//_______________________________________________________________

#include <VirtualWire.h>

int ledvert = 13;
int ledrouge = 12;
int Pinemetteur = 11;
int PinPIR = 2;
int valeur = 0; 
int EtatPIR = LOW;

//SETUP________________________________________________________________________
void setup()
{
   Serial.begin(9600);
   vw_set_tx_pin(Pinemetteur);
   vw_setup(4000); // Transmission rate
   pinMode(ledvert, OUTPUT);
   pinMode(ledrouge, OUTPUT);
   pinMode(PinPIR,INPUT);
   digitalWrite(ledvert, HIGH);
}

//LOOP_________________________________________________________________________
void loop()
{
  char msg[1] = {'0'};
  valeur = digitalRead(PinPIR);
  if (valeur == 1)
  {
      msg[0] = '1';
      digitalWrite(ledrouge, HIGH);
      vw_send((uint8_t *)msg, 1);
      vw_wait_tx();
      if (EtatPIR == LOW) 
      {
      Serial.println("** ATTENTION ! MOUVEMENT ! **");
      EtatPIR = HIGH;
      }
   }
 else
 {
   msg[0] = '0';
   digitalWrite(ledrouge, LOW);
   vw_send((uint8_t *)msg, 1);
   vw_wait_tx();
   if (EtatPIR == HIGH)
   {
      Serial.println("Pas de détection");
      EtatPIR = LOW;
   }
  }
}