#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define butManip1_up 7
#define butManip1_down 2
#define butManip2_up 10
#define butManip2_down 5
#define butManip3_right 6
#define butManip3_left 14

#define butCatch_left 25
#define butCatch_right 26

#define clickLeft_Done 0
#define clickLeft_Raw 1

#define clickRight_Done 27
#define clickRight_Raw 28

#define rightDone_up 24
#define rightDone_down 29
#define rightMedium_up 30
#define rightMedium_down 31
#define rightRaw_up 32
#define rightRaw_down 33

#define leftDone_up 22
#define leftDone_down 21
#define leftMedium_up 20
#define leftMedium_down 19
#define leftRaw_up 18
#define leftRaw_down 17

#define butMotor_left 15
#define butMotor_right 16

RF24 radio(9, 8); // CE, CSN

const byte address[6] = "00001"; //установление адрсса
int16_t text1[] = {0, 1, 2, 3, 4, 5 , 6, 7, 8, 9, 10, 11 , 12, 13, 14}; //1.2.3.4
//uint16_t text2[] = {16, 17, 18, 19, 20, 21, 22, 23, 24, 25};
int16_t text[] = {512, 512, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0}; 
int16_t last_text[] = {512, 512, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0};

int butManip = 0;
int Click = 0;
int rightMotor = 0;
int leftMotor = 0;
int butCatch = 0;

void setup() {
  radio.begin();  // Инициализация радиомодуля NRF24L01
  radio.setChannel(5); // Обмен данными будет вестись на пятом канале (2,405 ГГц)
  radio.setDataRate (RF24_1MBPS); // Скорость обмена данными 1 Мбит/сек
  radio.setPALevel(RF24_PA_HIGH); // Выбираем высокую мощность передатчика (-6dBm)
  radio.openWritingPipe (address); // Открываем трубу с уникальным ID

//  Serial.begin(9600);

  //pinMode(butMotor_left, INPUT_PULLUP);
  //pinMode(butMotor_right, INPUT_PULLUP);
  pinMode(butManip1_up, INPUT_PULLUP);
  pinMode(butManip1_down, INPUT_PULLUP);
  pinMode(butManip2_up, INPUT_PULLUP);
  pinMode(butManip2_down, INPUT_PULLUP);
  pinMode(butManip3_right, INPUT_PULLUP);
  pinMode(butManip3_left, INPUT_PULLUP);
  pinMode(butCatch_left, INPUT_PULLUP);
  pinMode(butCatch_right, INPUT_PULLUP);
  pinMode(clickLeft_Done, INPUT_PULLUP);
  pinMode(clickLeft_Raw, INPUT_PULLUP);
  pinMode(clickRight_Done, INPUT_PULLUP);
  pinMode(clickRight_Raw, INPUT_PULLUP);
  pinMode(rightDone_up, INPUT_PULLUP);
  pinMode(rightDone_down, INPUT_PULLUP);
  pinMode(rightMedium_up, INPUT_PULLUP);
  pinMode(rightMedium_down, INPUT_PULLUP);
  pinMode(rightRaw_up, INPUT_PULLUP);
  pinMode(rightRaw_down, INPUT_PULLUP);
  pinMode(leftDone_up, INPUT_PULLUP);
  pinMode(leftDone_down, INPUT_PULLUP);
  pinMode(leftMedium_up, INPUT_PULLUP);
  pinMode(leftMedium_down, INPUT_PULLUP);
  pinMode(leftRaw_up, INPUT_PULLUP);
  pinMode(leftRaw_down, INPUT_PULLUP);

}

void loop() {

while (last_text[0] == text[0] && last_text[1] == text[1] && last_text[2] == text[2] && last_text[3] == text[3] && last_text[4] == text[4] && last_text[5] == text[5] && last_text[6] == text[6]
        && last_text[7] == text[7] && last_text[8] == text[8] && last_text[9] == text[9] && last_text[10] == text[10] && last_text[11] == text[11] && last_text[12] == text[12] && last_text[13] == text[13]){
  if (analogRead(butMotor_left) - text[0] > 10 || analogRead(butMotor_left) - text[0] < -10) text[0] = analogRead(butMotor_left);
  if (analogRead(butMotor_right) - text[1] > 10 || analogRead(butMotor_right) - text[1] < -10) text[1] = analogRead(butMotor_right);

  text[2] = -abs(digitalRead(butManip1_down) - 1) + abs(digitalRead(butManip1_up) - 1);
  text[3] = -abs(digitalRead(butManip2_up) - 1) + abs(digitalRead(butManip2_down) - 1);
  text[4] = -abs(digitalRead(butManip3_right) - 1) + abs(digitalRead(butManip3_left) - 1);

  text[5] = -abs(digitalRead(clickLeft_Done) - 1) + abs(digitalRead(clickLeft_Raw) - 1);
  text[6] = -abs(digitalRead(clickRight_Raw) - 1) + abs(digitalRead(clickRight_Done) - 1);

  text[7] = -abs(digitalRead(rightDone_down) - 1) + abs(digitalRead(rightDone_up) - 1);
  text[8] = -abs(digitalRead(rightMedium_down) - 1) + abs(digitalRead(rightMedium_up) - 1);
  text[9] = -abs(digitalRead(rightRaw_down) - 1) + abs(digitalRead(rightRaw_up) - 1);

  text[10] = -abs(digitalRead(leftDone_up) - 1) + abs(digitalRead(leftDone_down) - 1);
  text[11] = -abs(digitalRead(leftMedium_up) - 1) + abs(digitalRead(leftMedium_down) - 1);
  text[12] = -abs(digitalRead(leftRaw_up) - 1) + abs(digitalRead(leftRaw_down) - 1);

  text[13] = -abs(digitalRead(butCatch_right) - 1) + abs(digitalRead(butCatch_left) - 1);

//  text1[0] = analogRead(butMotor_left);
//  text1[1] = analogRead(butMotor_right);
//  text1[2]=digitalRead(butManip1_up);
//  text1[3]=digitalRead(butManip1_down);
//  text1[4]=digitalRead(butManip2_up);
//  text1[5]=digitalRead(butManip2_down);
//  text1[6]=digitalRead(butManip3_right);
//  text1[7]=digitalRead(butManip3_left);
//  text1[8]=digitalRead(butCatch_left);
//  text1[9]=digitalRead(butCatch_right);
//  text1[10]=digitalRead(clickLeft_Done);
//  text1[11]=digitalRead(clickLeft_Raw);
//  text1[12]=digitalRead(clickRight_Done);
//  text1[13]=digitalRead(clickRight_Raw);
//  text1[14]=digitalRead(rightDone_up);
//  text1[15]=digitalRead(rightDone_down);
//  text1[16]=digitalRead(rightMedium_up);
//  text1[17]=digitalRead(rightMedium_down);
//  text1[18]=digitalRead(rightRaw_up);
//  text1[19]=digitalRead(rightRaw_down);
//  text1[20]=digitalRead(leftDone_up);
//  text1[21]=digitalRead(leftDone_down);
//  text1[22]=digitalRead(leftMedium_up);
//  text1[23]=digitalRead(leftMedium_down);
//  text1[24]=digitalRead(leftRaw_up);
//  text1[25]=digitalRead(leftRaw_down);
//
// Serial.print(text[0]);
//  Serial.print("\t");
//  Serial.print(text[1]);
//  Serial.print("\t");
//  Serial.print(text[2]);
//
//  Serial.print(text[3]);
//
//  Serial.print(text[4]);
//
//  Serial.print(text[5]);
//
//  Serial.print(text[6]);
//
//  Serial.print(text[7]);
//
//  Serial.print(text[8]);
// 
//
//  Serial.print(text[9]);
//
//
//  Serial.print(text[10]);
//
//
//  Serial.print(text[11]);
//
//
//  Serial.print(text[12]);
//
//
//  Serial.println(text[13]);
  delay(10);

//  Serial.print(text1[0]);
//  Serial.print("\t");
//  Serial.print(text1[1]);
//  Serial.print("\t");
//  Serial.print(text1[2]);
//  Serial.print(text1[3]);
//  Serial.print(text1[4]);
//  Serial.print(text1[5]);
//  Serial.print(text1[6]);
//  Serial.print(text1[7]);
//  Serial.print(text1[8]);
//  Serial.print(text1[9]);
//  Serial.print(text1[10]);
//  Serial.print(text1[11]);
//  Serial.print(text1[12]);
//  Serial.print(text1[13]);
//  Serial.print(text1[14]);
//  Serial.print(text1[15]);
//  Serial.print(text1[16]);
//  Serial.print(text1[17]);
//  Serial.print(text1[18]);
//  Serial.print(text1[19]);
//  Serial.print(text1[20]);
//  Serial.print(text1[21]);
//  Serial.print(text1[22]);
//  Serial.print(text1[23]);
//  Serial.print(text1[24]);
//  Serial.println(text1[25]);
}
//Serial.println("YES");
//delay(500);
 
  last_text[0] = text[0];
  last_text[1] = text[1];
  last_text[2] = text[2];
  last_text[3] = text[3];
  last_text[4] = text[4];
  last_text[5] = text[5];
  last_text[6] = text[6];
  last_text[7] = text[7];
  last_text[8] = text[8];
  last_text[9] = text[9];
  last_text[10] = text[10];
  last_text[11] = text[11];
  last_text[12] = text[12];
  last_text[13] = text[13];
  radio.write(text, sizeof(text));  //пердача текста, 9
  
}
