#include <Keyboard.h>

const int VERT = A0; // analog
const int HORIZ = A2; // analog
const int btn2 = 2; // sprint
const int btn3 = 3; // shoot
const int VERTblue = A4; // analog
const int HORTZblue = A5; //
const int btn4 = 4; // sprint
const int btn5 = 5; // shoot

char leftKey = KEY_LEFT_ARROW;
char rightKey = KEY_RIGHT_ARROW;
char upKey = KEY_UP_ARROW;
char downKey = KEY_DOWN_ARROW;
char sprintRed = 'M';
char shootRed = 'K';
char esquerdaKey = 'A';
char direitaKey = 'D';
char cimaKey = 'W';
char baixoKey = 'S';
char sprintBlue = 'Z';
char shootBlue = 32; // space

void setup(){
//Put your setup code here, to run once:
  pinMode(btn2, INPUT_PULLUP);
  pinMode(btn3, INPUT_PULLUP);
  pinMode(btn4, INPUT_PULLUP);
  pinMode(btn5, INPUT_PULLUP);
  
  Serial.begin(9600);
  Keyboard.begin();
}


void loop() {
  // put your main code here, to run repeatedly:

  int vertical, horizontal, verticalBlue, horizontalBlue;
  
  // read all values from the joystick
  
  vertical = analogRead(VERT); // will be 0-1023
  horizontal = analogRead(HORIZ); // will be 0-1023
  verticalBlue = analogRead(VERTblue); // will be 0-1023
  horizontalBlue = analogRead(HORTZblue); // will be 0-1023

  
  Serial.print("X-axis: " + (String)horizontal);
  Serial.flush();
  delay(10);
  Serial.println("Y-axis: " + (String)vertical);
  Serial.flush();
  delay(10);

  if (digitalRead(btn2) == LOW) {
    Keyboard.press(sprintRed);
    Keyboard.release(sprintRed);
   Serial.print("Botão2 pressionado");
  Serial.flush();     
  }

 if (digitalRead(btn4) == LOW) {
    Keyboard.press(sprintBlue);
    Keyboard.release(sprintBlue);
   Serial.print("Botão4 pressionado");
  Serial.flush();  
  }

  if (digitalRead(btn3) == LOW) {
    Keyboard.press(shootRed);
    Keyboard.release(shootRed);
   Serial.print("Normal pressionado");
  Serial.flush(); 
  delay(10); 
  }

 if (digitalRead(btn5) == LOW) {
    Keyboard.press(shootBlue);
    Keyboard.release(shootBlue);
   Serial.print("Normal pressionado");
  Serial.flush();  
  delay(10);
  }

  if(horizontal > 505) {
  Keyboard.release(downKey);
  delay(10);
  Keyboard.press(upKey);
  delay(10);
  }
  
   if(horizontalBlue > 401) {
  Keyboard.release(baixoKey);
  delay(10);
  Keyboard.press(cimaKey);
  delay(10);
  }

    
  if(horizontal < 490) {
  Keyboard.release(upKey);
  delay(10);
  Keyboard.press(downKey);
  //delay(10);
  }

  if(horizontalBlue < 299) {
  Keyboard.release(cimaKey);
  delay(10);
  Keyboard.press(baixoKey);
  delay(10);
  }
  
  
  if(vertical > 505) {
  Keyboard.release(leftKey);
  delay(10);
  Keyboard.press(rightKey);
  //delay(10);
  }

  if(verticalBlue > 401) {
  Keyboard.release(esquerdaKey);
  delay(10);
  Keyboard.press(direitaKey);
  delay(10);
  }

  if(vertical < 490) {
  Keyboard.release(rightKey);
  delay(10);
  Keyboard.press(leftKey);
  //delay(10);
  }
  
  if(verticalBlue < 299) {
  Keyboard.release(direitaKey);
  delay(10);
  Keyboard.press(esquerdaKey);
  delay(10);
  }

  if(vertical > 490 && vertical < 505 && horizontal > 490 && horizontal < 505) {
  Keyboard.release(upKey);
  Keyboard.release(downKey);
  Keyboard.release(rightKey);
  Keyboard.release(leftKey);
  delay(10);
  }

  if(verticalBlue > 300 && verticalBlue < 400 && horizontalBlue > 300 && horizontalBlue < 400) {
  Keyboard.release(cimaKey);
  Keyboard.release(baixoKey);
  Keyboard.release(direitaKey);
  Keyboard.release(esquerdaKey);
  delay(10);
  }

}
