//Two Player
#include "Keyboard.h"
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 5);  // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001"; //Note the receiver and transmitter should have the same address.

struct Data_Package {
  int deviceno = 0;
  float ax = 0;
  float ay = 0;
  float az = 0;
  float asqrt = 0;  
};

Data_Package data,data1,data2;


unsigned long previousMillis = 0; 
const long interval = 800;

unsigned long prevkeypress1 = 0, prevkeypress2 = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  if (!radio.begin()) {
   Serial.println(F("radio hardware is not responding!!"));
   while (1) {} // hold in infinite loop
  }
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();  //Set module as receiver
  
}

void loop() {

  unsigned long currentMillis = millis();
  
  if (radio.available()) {
    radio.read(&data, sizeof(Data_Package));

    if (data.deviceno == 1){
      data1 = data;
    }
    else if (data.deviceno == 2){
      data2 = data;
    }

    if  (data1.az > 0.20){
           unsigned long keypress1 = millis();
           if (keypress1 - prevkeypress1 >=interval){
           Keyboard.press(' ');
           prevkeypress1 = keypress1;
           }                                                                                                                                                                                                                                                                                                                                        
       }
    if  (data2.az > 0.20){
           unsigned long keypress2 = millis();
           if (keypress2 - prevkeypress2 >=interval){
           Keyboard.press('l');
           prevkeypress2 = keypress2;
           }                                                                                                                                                                                                                                                                                                                                        
       }
      
    Serial.println("     ");
    Serial.println("1 Ax: " + String(data1.ax));
    Serial.println("1 Ay: " + String(data1.ay));
    Serial.println("1 Az: " + String(data1.az));
    Serial.println("1 Asqrt: " + String(data1.asqrt));
    
    
    Serial.println("     ");
    Serial.println("2 Ax: " + String(data2.ax));
    Serial.println("2 Ay: " + String(data2.ay));
    Serial.println("2 Az: " + String(data2.az));
    Serial.println("2 Asqrt: " + String(data2.asqrt));  
    
    delay(10);       
    }


  if (currentMillis - previousMillis >= interval/2 ) {                  
    Keyboard.release(' ');
    Keyboard.release('l');
    previousMillis = currentMillis;
  }
  
}
