/*
*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"

int buzzer = A10;
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
int timers = 0;

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

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

int timer = 0;
int newtimer = 0;
//variables for buttons and their pin outputs
// the 4 small sqare buttons and their input states
int squareButton[] = {0,0,0,0,0};
int largeSquares[] = {30,31,32,33,34};
int largecount = 0;

// the small square buttons and their pins
int squareBut[] = {0,0,0,0};
int test = 0;
int smallSquares[] = {35,36,37,38};

int lift = 0;

//---------------------------------motion stuff
int motion = 39;
int motionsensor = 0;
int pirstate = LOW;
int motiontimer = 0;

//button states are the signals from both bombs, if a button is pressed
// the button is multiplied by a prime number and the other bomb reads
// the signal
long buttonStates = 0;
long buttonState = 0;




// count is an array that will keep counts for when things are solved
int count[] = {0,0,0,0,0};



//the array to check when everything is satisfied
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};


//----------------------------------------------------hold stuff
// pin values for the hold and number large buttons
int hold = 41;
// hold state reads if button is held
int holdState[] = {0,0};


//-------------------------------mic stuff
int mic = A0;
int micsensor = 0;
int miccount[] = {0,0};

//--------------------------- random led stuff
int randone =0;
int randtwo = 0;
int randleds[] = {11, 12, 13, A1, A2};
int randint = 0;
int placeholder = 0;

//------------------------------number button stuff
int counterleds[] = {49,53,2,5,4,3,10,9,6};
int countertimer= 0;
int countercount =0;
int num = 40;
// reads if the number is read correctly.
int numstate[] = {0,0};
int numcheck[] = {LOW,HIGH,0};

//------------------------------password stuff
int k = 0;
int password[4];
int pass[4] = {49,57,53,56};

//------------------------------final stuff
int finalleds[] ={42,43,44,45,46,47,48};
int finalcount = 0;
int finalcheck = 0;
int timertwo = 0;
int timerthree =0;
int randtest=0;
int i = 0;

//--------------------------------------------------------------------------SETUP-----------------------------------------------------------------------
void setup() {

  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer,HIGH);
  for (int i=0; i<10; i++){
  pinMode(randleds[i], OUTPUT);
  }
  pinMode(12, 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<10;j++){
  pinMode(counterleds[j], OUTPUT);
  digitalWrite(counterleds[j],LOW);
  }
      for(int j = 0;j<7;j++){
  pinMode(finalleds[j], OUTPUT);
  digitalWrite(finalleds[j],LOW);
  }
  for(int j = 0;j<4;j++){
  pinMode(largeSquares[j], INPUT);
  }
  pinMode(motion, INPUT);
  pinMode(hold, INPUT);
  pinMode(num,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);

 
  
}

//============================================================================================LOOP=====================================================================================
void loop() {
//Serial.println(buttonStates);
//**********************************SENDING RADIO HERE*******************************************
   delay(5);
   radio.stopListening();
 //----------------------------------------------------------------Rand LEDS CODE----------------------------------------------------------------------------------
   numstate[0] = digitalRead(num);
   //Serial.println(numstate[0]);
   timertwo = millis();
   if ((timertwo - randtest) > 50){
    digitalWrite(randleds[placeholder],LOW);
    randtest = millis();
    randint = random(0,5);
    digitalWrite(randleds[randint],HIGH);
    placeholder = randint;
   }

   //-----------------------------------------------------------part 2 number button test-------------------------------------------------------------------------
   if (bitRead(buttonStates, 2) != 1){
      numstate[0] = digitalRead(num);
      if(countercount != 0 && countercount %2==0 && numstate[0] == 1 && numcheck[2]==0){
         numcheck[0]=HIGH;
         numcheck[1]=LOW;
         numcheck[2]=1;
         countercount = 1;
         for (int i=0; i<10; i++){
           digitalWrite(counterleds[i],LOW);
         }
         numstate[0]=0;
         //Serial.println("yup");
       }
       if(numcheck[2] == 1 && countercount > 7 ){
         numcheck[2] =2;
       }        
       if(numcheck[2]==2 && countercount == 7 && numstate[0]==1){
         //Serial.println("led 1 set");
         finalcount++;
         Serial.println("number");
         bitWrite(buttonStates, 2, 1);
         for (int i=0; i<10; i++){
            digitalWrite(counterleds[i],LOW);
         }
       }
       if(countercount%2!=0 && numstate[0]==1 && bitRead(buttonStates,2) != 1 && numcheck[2] !=3 && numcheck[2] !=1){
         numcheck[0]=LOW;
         numcheck[1]=HIGH;
         numcheck[2]=3;
         countercount = 0;
         for (int i=0; i<10; i++){
            digitalWrite(counterleds[i],HIGH);
         }
         numstate[0]=0;
         //Serial.println("nope");
       } 
       if(numcheck[2] == 3 && countercount > 7 ){
         numcheck[2] =0;
       } 
   }
   //----------------------------------------------------------------Part 2 Number button counter-------------------------------------------------------------------------------
   if ((timertwo - countertimer) > 1000 && bitRead(buttonStates,2) != 1){
    digitalWrite(counterleds[countercount],numcheck[1]);
    digitalWrite(counterleds[countercount - 1],numcheck[0]);
    countercount++;
    if(countercount == 1){
      digitalWrite(counterleds[8],numcheck[0]);
    }
   //Serial.println(countercount);
    
    if(countercount > 8){
      countercount =0;
    }
    countertimer = timertwo;
   }

   //--------------------------------------------------------------part 5 keypad-------------------------------------------------------------------------------------------------
   if(bitRead(buttonStates,5) != 1){
      char key = keypad.getKey();
      if(key)  // Check for a valid key.
      {
        password[k]= key;
        k++;
        smallbeep();
        //Serial.print(key);
      if(k>3)
      {
         for (int j=0;j<4;j++){
          Serial.println(password[j]);
          Serial.print(pass[j]);
          if (password[j] == pass[j]){
            count[2]++;
          }
          else{
            count[2]=0;
             k =0;
             smallbeep();
             smallbeep();
             smallbeep();
          }
         }
         }
       if(count[2] == 4){
          bitWrite(buttonStates, 5, 1);
          finalcount++;
          Serial.println("Keypad");  
          //Serial.println(count[2]);
          //Serial.println("keypad");      
       }
      }
   }
  
//----------------------------------------------------------------part 6 small buttons-------------------------------------------------------------------------------------------
/*if(bitRead(buttonStates,6)!=1){
  Serial.println(count[3]);
 for (int l=0;l<4;l++){
      squareBut[l] = digitalRead(smallSquares[l]);
             //Serial.println(squareBut[2]);
 }
      
if(squareBut[0] == 0 && squareBut[1] == 0 && squareBut[2] == 0 && squareBut[3] == 0){
    count[3] =0;
    bitWrite(buttonState, 9,0);
    bitWrite(buttonState, 10,0);
    bitWrite(buttonState, 11,0);
    bitWrite(buttonState, 12,0);
    Serial.println("loop");
}
if(squareBut[2] == 0){
    bitWrite(buttonState, 9,0);
    bitWrite(buttonState, 10,0);
    bitWrite(buttonState, 11,0);
    bitWrite(buttonState, 12,0);
    count[3] =0;
    Serial.println("bye1");
 }
  if(squareBut[2]==1 && count[3] ==0 && bitRead(buttonState,9)==1 && squareBut[0]==0 && bitRead(buttonState,10)!=1 && squareBut[3]==0 && bitRead(buttonState,11)!=1 && squareBut[1]==0 && bitRead(buttonState,12)!=1){
  count[3] ++;
  bitWrite(buttonStates, 9,1);
 }
 if(squareBut[2]==1 && count[3] ==1 && bitRead(buttonState,9)==1 && squareBut[0]==1 && bitRead(buttonState,10)!=1 && squareBut[3]==0 && bitRead(buttonState,11)!=1 && squareBut[1]==0 && bitRead(buttonState,12)!=1){
  count[3] ++;
  bitWrite(buttonStates, 10,1);
 }
   if(squareBut[2]==1 && count[3] ==2 && bitRead(buttonState,9)==1 && squareBut[0]==1 && bitRead(buttonState,10)==1 && squareBut[3]==1 && bitRead(buttonState,11)==1 && squareBut[1]==0 && bitRead(buttonState,12)!=1){
  count[3] ++;
  bitWrite(buttonStates, 11,1);
 }
  if(squareBut[2]==1 && count[3] ==3 && bitRead(buttonState,9)==1 && squareBut[0]==1 && bitRead(buttonState,10)==1 && squareBut[3]==1 && bitRead(buttonState,11)==1 && squareBut[1]==1 && bitRead(buttonState,12)==1){
  count[3] ++;
  bitWrite(buttonStates, 12,1);
  bitWrite(buttonStates, 6,1);
  finalcount++;
 }
  if (count[3] ==2 && squareBut[1] ==1) {
    bitWrite(buttonStates, 9,0);
    bitWrite(buttonStates, 10,0);
    bitWrite(buttonStates, 11,0);
    bitWrite(buttonStates, 12,0);
    Serial.println("bye2");
    count[3]=0;
  }
  if(count[3] == 1 && squareBut[1] == 1){
       bitWrite(buttonStates, 9,0);
    bitWrite(buttonStates, 10,0);
    bitWrite(buttonStates, 11,0);
    bitWrite(buttonStates, 12,0);
    Serial.println("bye3");
    count[3]=0;
  }
  

  //Serial.println(count[3]);
}*/


//-----------------------------------------------------------------------------------Large Button-------------------------------------------------------------------
 
if(bitRead(buttonState,7)!=1){
 for (int m=0;m<5;m++){
  squareButton[m] = digitalRead(largeSquares[m]);
 // Serial.print(m);
 // Serial.print(":");
// Serial.println(squareButton[m]);
      
 }
if (bitRead(buttonState,18)==0 && largecount != 0 && largecount != 1&& timertwo - timerthree > 7000){
  timerthree = timertwo;
       for (int m=13;m<18;m++){
 bitWrite(buttonStates,m,0);
      
 }
 if( bitRead(buttonStates,18)==1){
  timerthree = timertwo;
 }
 largecount =0;
}
if( largecount == 0 && bitRead(buttonState, 13 ) == 1){
  largecount ++;
  bitWrite(buttonStates,18,1);
}
if( largecount == 1 &&  squareButton[0] == 1){
  largecount ++;
  smallbeep();
  bitWrite(buttonStates,13,1);
}if(largecount == 1 && (squareButton[2] == 1 || squareButton[3] == 1 || squareButton[4] == 1)){
  largecount =0;
  bitWrite(buttonStates,18,0);
       for (int m=13;m<18;m++){
 bitWrite(buttonStates,m,0);
      
 }
}
if( largecount == 2 &&   bitRead(buttonState, 14 ) == 1){
  largecount ++;
  smallbeep();
}
if(largecount == 2 && squareButton[4] == 1 && timertwo - timerthree > 4000){
  largecount =0;
  bitWrite(buttonStates,18,0);
      for (int m=13;m<18;m++){
 bitWrite(buttonStates,m,0);
 timerthree = timertwo;
      
 }
}
if( largecount == 3 &&  squareButton[1] == 1){
  largecount ++;
  bitWrite(buttonStates,14,1);
  smallbeep();
  timerthree = timertwo;
}
if(largecount == 3 && (squareButton[3] == 1 || squareButton[4] == 1) && timertwo - timerthree > 4000){
  largecount =0;
  timerthree = timertwo;
  bitWrite(buttonStates,18,0);
      for (int m=13;m<18;m++){
 bitWrite(buttonStates,m,0);
      
 }
}
if( largecount == 4 &&  bitRead(buttonState, 15 ) == 1){
  largecount ++;
  timerthree = timertwo;
  smallbeep();
}
if( largecount == 5 &&  squareButton[2] == 1){
  largecount ++;
  smallbeep();
  timerthree = timertwo;
  bitWrite(buttonStates,15,1);
}
 if(largecount == 5 && ( squareButton[0] == 1 || squareButton[4] == 1) && timertwo - timerthree > 4000){
  largecount =0;
  timerthree = timertwo;
  bitWrite(buttonStates,18,0);
      for (int m=13;m<18;m++){
 bitWrite(buttonStates,m,0);
      
 }
}
if( largecount == 6 &&  bitRead(buttonState, 16 ) == 1){
  largecount ++;
  timerthree = timertwo;
  smallbeep();
}
if( largecount == 7 &&  squareButton[3] == 1){
  largecount ++;
  timerthree = timertwo;
  smallbeep();
  bitWrite(buttonStates,16,1);
}
if(largecount == 7  && (squareButton[1] == 1  || squareButton[0] == 1 ) && timertwo - timerthree > 4000){
  largecount =0;
  timerthree = timertwo;
   bitWrite(buttonStates,18,0);
       for (int m=13;m<18;m++){
 bitWrite(buttonStates,m,0);
      
 }
}
if( largecount == 8 &&  bitRead(buttonState, 17 ) == 1){
  largecount ++;
  timerthree = timertwo;
  smallbeep();
}
if( largecount == 9 &&  squareButton[4] == 1){
  largecount ++;
  bitWrite(buttonStates,17,1);
  smallbeep();
}
if(largecount == 9 &&  (squareButton[1] == 1 || squareButton[2] == 1 || squareButton[0] == 1 ) && timertwo - timerthree > 4000){
  largecount =0;
  timerthree = timertwo;
   bitWrite(buttonStates,18,0);
       for (int m=13;m<18;m++){
 bitWrite(buttonStates,m,0);
      
 }
}
if(largecount > 9){
  finalcount ++;
  timerthree = timertwo;
  bitWrite(buttonStates, 7,1);
}
Serial.println(largecount);
 }

 
//--------------------------------------------------------------------------------part 3 motion sensor-------------------------------------------------------------------------- 
  if( bitRead(buttonStates,3) != 1){
    //Serial.println(motiontimer);
     motionsensor = digitalRead(motion);  // read input value
     //Serial.println(motionsensor);
     if (motionsensor == HIGH) {            // check if the input is HIGH
       if (pirstate == LOW) {
         pirstate = HIGH;
         motiontimer = timertwo;
         count[1] =0;
       }
     } else {
       if (pirstate == HIGH){
         pirstate = LOW;
         count[1] = 0;
       }
       if (pirstate == LOW && timertwo - motiontimer > 3000){
        count[1]++;
        motiontimer = timertwo;
        //Serial.println(count[1]);
       }
       if (count[1] >90){
          bitWrite(buttonStates, 3, 1);
          finalcount++;
          Serial.println("motionsensor");
       }    
     }
  }
//----------------------------------------------------------------------------------part 4 nock knnock----------------------------------------------------------------------------------
if(timertwo > 5000){
  timers = 1;
}
 if(bitRead(buttonStates,4) != 1 && timers == 1){
     micsensor = analogRead(mic);
     //Serial.println(micsensor);
     //Serial.println(micsensor);
     if(micsensor < 10 && miccount[1] == 0){
        miccount[0]++;
        miccount[1] = 1;
        //Serial.println(miccount[0]);
         timer = millis();
     }
    if(micsensor < 10 && miccount[1] == 1){
        miccount[0]++;
        miccount[1] = 1;
        //Serial.println(miccount[0]);

     }

     if(miccount[1] == 1 && (timertwo - timer  < 30000) && miccount[0] > 25){
         bitWrite(buttonStates, 4, 1);
         finalcount++;
         Serial.println("Knock Knock");
     }
     if(miccount[1] ==1 && timertwo - timer > 30000){
      miccount[0] =0;
      miccount[1]= 0;
      timer = millis();
     }
 }
 //----------------------------------------------------------------------------------------part 1 hold--------------------------------------------------------------------------------------
 if(bitRead(buttonState,1)==1){
  bitWrite(buttonStates,1,1);
 }
 if(bitRead(buttonStates,1)!=1){
 holdState[0] = digitalRead(hold);
 if (holdState[0] == HIGH ){
  if(bitRead(buttonStates,8)!=1){
  bitWrite(buttonStates, 8, 1);
  }
 }
 else{
  holdState[1] = timertwo;
  if(bitRead(buttonStates,8)==1){
   bitWrite(buttonStates, 8, 0);
  }
 }
 if ( holdState[0]== HIGH && bitRead(buttonState,8)==1 && timertwo - holdState[1] > 30000){
    Serial.println("HOLD");
    bitWrite(buttonStates,1,1);
    finalcount++;
 }
}

 //-------------------------------------------------------------------------------------------final leds------------------------------------------------------------------------------------
if (finalcount >0){
  digitalWrite(finalleds[finalcheck],HIGH);
  finalcount = finalcount -1;
  beep();
  finalcheck++;
}

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

//****************************************LISTENING HERE******************************************  
  delay(5);
  radio.startListening();
  if (radio.available()){ 
     radio.read(&buttonState, sizeof(buttonState));

     radio.read(&lift, sizeof(lift));
  }
  if(lift == 1){
    digitalWrite(12,HIGH);
    clearprogram();
  }
  else{
    digitalWrite(12,LOW);
  }
  }
  
void beep(){
  digitalWrite(buzzer,LOW);
  delay(70);
  digitalWrite(buzzer,HIGH);
}
void smallbeep(){
    digitalWrite(buzzer,LOW);
  delay(50);
  digitalWrite(buzzer,HIGH);
}


void clearprogram(){
   for (int m=0;m<18;m++){
 bitWrite(buttonStates,m,0);
      
 }
}
 
