/*
*Arduino Bomb wireless 2 way transmittion code
* Lucas Dahl
*/

// ARDUINO FOR BOMB ONE BY ROUND TABLE

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include "Arduino.h"
#include "Keypad.h"

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {26, 25, 24, 23}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {29, 28, 27}; //connect to the column pinouts of the keypad


Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define button 4
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};

//variables for buttons and their pin outputs
// the 4 small sqare buttons and their input states
int squareButton[] = {3,3,3,3,3};
int largeSquares[] = {44,43,42,41,40};

// the small square buttons and their pins
int squareBut[] = {3,3,3,3,3};
int smallSquares[] = {49,48,47,46,45};


int buttonState = 1;
int buttonStates = 1;
// the inputs for the 2 large buttons on both bombs 
int count[] = {0,0,0,0,0};
int holdTime[] = {0,0,0,0,0};
int holdState[] = {0,0};
int holdCheck[] ={0,0};
int numState[] = {0,0};
int numCheck[] = {0,0};
int finish[] = {0,0,0,0,0,0,0};
//the true or false state for the large and small buttons
int butState[] ={0,0,0,0,0,0,0,0,0,0,0};
int lastbutState[] ={0,0,0,0,0,0,0,0,0,0,0};

// pin values for the hold and number large buttons
int hold = 5;
int num = 6;

void setup() {
  pinMode(11, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(hold, INPUT);
  pinMode(num, INPUT);
  for(int j = 0;j<4;j++){
  pinMode(smallSquares[j], INPUT);
  }
  for(int j = 0;j<4;j++){
  pinMode(largeSquares[j], INPUT);
  }
  Serial.begin(9600);
  radio.begin();
  //opens up the channel to communicate between radios
  radio.openWritingPipe(addresses[0]); // 00002
  radio.openReadingPipe(1, addresses[1]); // 00001
  radio.setPALevel(RF24_PA_MIN);
  
}
void loop() {
//**********************************SENDING RADIO HERE*******************************************
   delay(5);
   radio.stopListening();
  
   char key = keypad.getKey();
   if(key)  // Check for a valid key.
   {
    switch (key)
    {
      case '*':
        digitalWrite(3, LOW);
        if (!(buttonState%2 == 0)){
        buttonState = buttonState * 2;
        }
        break;
      case '#':
        digitalWrite(3, HIGH);
        if ((buttonState%2 == 0)){
        buttonState = buttonState / 2;
        }
        break;
        
    }
  }
  
  numState[0] = digitalRead(num);
  holdState[0] = digitalRead(hold);
    if(holdState[0]==HIGH){
      if(!(buttonState%3 ==0)){
    buttonState = buttonState*3;
      }
  } else{
    if(buttonState%3 == 0){
      buttonState=buttonState/3;
    }
  }
 for (int j=0;j<4;j++){
      squareBut[j] = digitalRead(smallSquares[j]);
      squareButton[j] = digitalRead(largeSquares[j]);
 }
 if ( holdState[0]== HIGH && buttonStates%3==0){
  //digitalWrite(4,HIGH);
 }
 else{
  //digitalWrite(4,LOW);
 }

 radio.write(&buttonState, sizeof(buttonState));

//****************************************LISTENING HERE******************************************  
  delay(5);
  radio.startListening();
  if (radio.available()){ 
     radio.read(&buttonStates, sizeof(buttonStates));
  }
      if ((buttonStates)%4==0){
      //digitalWrite(4,HIGH);
     } else {
      //digitalWrite(4,LOW);
     }
  }
  
  



/* first prime numbers
 *  2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 
 *  71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 
 *  151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 
 *  233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 
 *  317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 
 *  419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 
 *  503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 
 *  607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 
 *  701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 
 *  811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 
 *  911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
 */
 
