/* TEST A wakes up only as much as seemingly required to catch a 
 *  transmssion and broadcast - but is this good? Should it wake up more often? 
 *  The shortened cycle is governed by an if function and the condition variable
 *  InconTrol. If removed I see a lot more "woke" statements.
*/
#include <RFM69.h>

#include <LowPower.h>

const int analogPin = A2; 
const int analogPin2 = A4; 

typedef struct {
  int sensorReading; 
  int sensorReadingL;      
} Node;
Node theData;

int sensorValue = 0; 
int sensorValueL = 0; 


int ledPin = 9;  // the Moteino LED


// Uncomment the appropriate frequency for your hardware
#define FREQUENCY     RF69_433MHZ
//#define FREQUENCY     RF69_868MHZ
//#define FREQUENCY     RF69_915MHZ
#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!

#define ENCRYPT_KEY "sampleEncryptKey"
#define ADDRESS 3 //CHANGE THIS FOR EVERY NODE
#define NETWORK_ID 99

RFM69 radio;
int InconTrol = 0;


void setup() {
  Serial.begin(115200);
  radio.initialize(FREQUENCY, ADDRESS, NETWORK_ID);
  radio.encrypt(ENCRYPT_KEY);
#ifdef IS_RFM69HW
  radio.setHighPower(); //uncomment only for RFM69HW!
#endif
radio.writeReg(0x03,0x68);      //RegBitrateMsb 1200 bitrate
 radio.writeReg(0x04,0x2B);      //RegBitrateLsb 1200 bitrate
 radio.writeReg(0x05,0x00);      //RegFdevMsb     2000 
 radio.writeReg(0x06,0x52);      //RegFdevLsb     2000
 radio.writeReg(0x19,0x40|0x10|0x05);      //RegRxBw  DccFreq:010, RxBw_Mant:24, RxBw_Exp:5 
 radio.writeReg(0x18,0x00|0x00|0x01);      //RegLna  LnaZin:50ohm, LowPower:Off, CurrentGain:MAX 

}

void loop() {

    Serial.flush();
InconTrol = 0;
    radio.listenModeStart();
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
    //Woke up, check for a message
    Serial.println("Woke up!");
    uint8_t from = 0;
    long burstRemaining = 0;
    if (radio.DATALEN > 0) {
      
      Serial.println("Received a message in listen mode");
      Serial.println((char*)radio.DATA);
      String newVal = ((char*)radio.DATA);
            delay (10);
            
            InconTrol = (newVal.toInt());
                      
            Serial.flush();
      
      from = radio.SENDERID;
      burstRemaining = radio.LISTEN_BURST_REMAINING_MS;
    
    }

    // Radio goes back to standby, ready for normal operations
    radio.listenModeEnd();
         LowPower.powerDown(SLEEP_250MS, ADC_OFF, BOD_OFF);
     // radio.send(from, "woke", 4);
          
    if(InconTrol == 1000){
      Serial.println("read sensors");
     //VVVVVVVVVVVVVVVVVVV READ THE SENSOR HERE VVVVVVVVVVVVVVVVVVV
      
                                 
                      int analog3 = analogRead(analogPin);
                      
                      int highADD = mapf(analog3,113, 1024, 0, 175); // note no calibrated map functions - will need to sort
                      sensorValue = highADD + (ADDRESS*1000);
                     
                     theData.sensorReading = sensorValue;
                      
                      
                 
   //  ^^^^^^^^^^^^^^^^^ READ THE SENSOR HERE ^^^^^^^^^^^^^^^^^        
        
                    
   
    sensorValueL = (ADDRESS*1000) + 570;// JUST A VALUE FOR PROCESSING AT THE MCP
    theData.sensorReadingL = sensorValueL;

 
       radio.sendWithRetry(from, (const void*)(&theData), sizeof(theData), 10);
       LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);
    }
  }
  double mapf(double x, double in_min, double in_max, double out_min, double out_max) {
   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

