/*
PROGRAM CORRESPONDING TO THE ISSUE OF THE "WIRELESS DARK MAGIC CANDLE"
authors: Aitana Ballerini Salvador, Manel Martinez Rubio
tutor: Jonathan Chacón Pérez
uses and specific terminology in English - third year, first trimester
ELISAVA - Barcelona school of engineering and design
*/


#include "nRF24L01.h" //incluimos la libreria del NRF24L01
#include "RF24.h"
#include "SPI.h"

int ledState;
int pinLed = A5, pinR = A1, pinG = A2, pinB = A3;

int ReceivedMessage[1] = {0001};
RF24 radio(9, 10);

const uint64_t canal = 0xE6E6E6E6E6E6; //el mismo canal para el emisor y receptor

void setup()
{
  radio.begin();

  radio.openReadingPipe(1, canal);

  radio.startListening();

  pinMode(pinLed, OUTPUT); //we set the red LED in a pin as an output.
  pinMode(pinR, OUTPUT); //we set the 'red' in the RGB LED in a pin as an output.
  pinMode(pinG, OUTPUT); //we set the 'green' in the RGB LED in a pin as an output.
  pinMode(pinB, OUTPUT); //we set the 'blue' in the RGB LED in a pin as an output.
  ledState = LOW; //in the first instance, the LED is off.
}

void loop() //lee la información que mande el emisor y ejecuta
{
  while (radio.available())
  {
    radio.read(ReceivedMessage, 1);

    if (ReceivedMessage[0] == 111 && ledState == LOW)
    {
      //if the sensor notices a minimal value, and the leds are off.
        analogWrite (pinLed, random(50, 255)); //make the red led turn on and change randomly its intensity.
     
        for (int i = 130; i < 255; i++) { //with this, we make the RGB LED go from orange to yellow.
            analogWrite (pinR, 255);
            analogWrite (pinG, i);
            analogWrite (pinB, 0);
          
      }
        for (int i = 255; i > 255; i--) { //in this case, it comes back from yellow to orange.
            analogWrite (pinR, 255);
            analogWrite (pinG, i);
            analogWrite (pinB, 0);
      }
      ledState = HIGH; //the state of the LED is updated.
    }
    else if (sensor >= valmin && ledState == HIGH) { //with this, we make the leds turn off again.
        analogWrite (pinLed, LOW);
        analogWrite (pinR, LOW);
        analogWrite (pinG, LOW);
        analogWrite (pinB, LOW);
    }
    ledState = LOW; //the state of the LED is updated.
  }




  
