Created
February 20, 2017 20:12
-
-
Save anonymous/aefaff90a3df4746340067ade436bc90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Keyboard.h> | |
#include <SoftwareSerial.h> | |
// give it a name: Game control | |
// Pin names | |
int pin_1 = 2; //space | |
int pin_2 = 6; //up | |
int pin_3 = 12; //down | |
int pin_4 = 13; //left | |
int pin_5 = 8; //right | |
//Estas variaveis servirao para verificar o estado das portas | |
int estado_pin_1, estado_pin_2, estado_pin_3, estado_pin_4, estado_pin_5; | |
//The setup routine runs once when you press reset: | |
void setup() { | |
//All the digital doors where the buttons are connected, have to be configured as INPUT | |
pinMode(pin_1, INPUT); //Sets up digital door 8 as INPUT | |
digitalWrite(pin_1, 0); //Places value 1 in the digital door 8 | |
pinMode(pin_2, INPUT); //Sets up digital door 13 as INPUT | |
digitalWrite(pin_2, 0); //Places value 1 in the digital door 13 | |
pinMode(pin_3, INPUT); //Sets up digital door 12 as INPUT | |
digitalWrite(pin_3, 0); //Places value 1 in the digital door 12 | |
pinMode(pin_4, INPUT); //Sets up digital door 6 as INPUT | |
digitalWrite(pin_4, 0); //Places value 1 in the digital door 6 | |
pinMode(pin_5, INPUT); //Sets up digital door 2 as INPUT | |
digitalWrite(pin_5, 0); //Places value 1 in the digital door 2 | |
//Every digital door used, have to be conected to the internal resistor | |
//It's a lifting resistor | |
//Setting buttons | |
#define space 2 //Botao space na porta 2 vermelho | |
#define Up 6 //Botao up na porta 6 laranja | |
#define Down 12 //Botao down na porta 12 verde | |
#define Left 13 //Botao left na porta 13 azul | |
#define Right 8 //Botao right na porta 8 amarelo | |
char strings[6][10]={"Up","Down","Left","Right","space","\0"}; | |
//strings[0] = "Up"; | |
//strings[1] = "Down"; | |
//strings[2] = "Left"; | |
//strings[3] = "Right"; | |
//strings[4] = "space"; | |
//strings[5] = "\0"; | |
//Starting serial comunication | |
//Serial.begin(9600); //9600 is the serial comunication speed | |
//Serial.println("Starting serial comunication..."); | |
//Keyboard.begin(); //Starts a keyboard emulation | |
//Todos os botoes comecam desligados | |
digitalWrite(2, LOW); //Porta 2 space | |
digitalWrite(6, LOW); //Porta 6 up | |
digitalWrite(12, LOW); //Porta 12 down | |
digitalWrite(13, LOW); //Porta 13 left | |
digitalWrite(8, LOW); //Porta 8 right | |
} | |
//the loop routine runs over and over again forever: | |
void loop(){ | |
//Inside this function, we must check if there is a button pressed or not. | |
//int space = digitalRead(2); //Check door 2 | |
//int up = digitalRead(6); //Check door 6 | |
//int down = digitalRead(12); //Check door 12 | |
//int left = digitalRead(13); //Check door 13 | |
//int right = digitalRead(8); //Check door 8 | |
//Reading doors | |
estado_pin_1 = digitalRead(2); //space | |
estado_pin_2 = digitalRead(6); //up | |
estado_pin_3 = digitalRead(12); //down | |
estado_pin_4 = digitalRead(13); //left | |
estado_pin_5 = digitalRead(8); //right | |
if(estado_pin_1 == HIGH){ | |
Keyboard.begin(); | |
while(estado_pin_1 == HIGH){ | |
//Space is pressed | |
//Serial.println("space"); | |
Keyboard.println("space"); | |
estado_pin_1 = digitalRead(2); | |
} | |
if(estado_pin_1 == LOW){ | |
//A porta 2 recebe a informacao de que ela esta desligada | |
digitalWrite(2, LOW); | |
Keyboard.end(); | |
} | |
} | |
if(estado_pin_2 == HIGH){ | |
Keyboard.begin(); | |
while(estado_pin_2 == HIGH){ | |
//Up is pressed | |
//Serial.println("Up"); | |
Keyboard.println("Up"); // | |
estado_pin_2 = digitalRead(6); | |
} | |
if(estado_pin_2 == LOW){ | |
//A porta 6 recebe a informacao de que ela esta desligada | |
digitalWrite(6, LOW); | |
Keyboard.end(); | |
} | |
} | |
if(estado_pin_3 == HIGH){ | |
Keyboard.begin(); | |
while(estado_pin_3 == HIGH){ | |
//Down is pressed | |
//Serial.println("Down"); | |
Keyboard.println("Down"); //Envia uma string ao pc | |
estado_pin_3 = digitalRead(12); | |
} | |
if(estado_pin_3 == LOW){ | |
//A porta 12 recebe a informacao de que ela esta desligada | |
digitalWrite(12, LOW); | |
Keyboard.end(); | |
} | |
} | |
if(estado_pin_4 == HIGH){ | |
Keyboard.begin(); | |
while(estado_pin_4 == HIGH){ | |
//Left is pressed | |
//Serial.println("Left"); | |
Keyboard.println("Left"); //Envia uma string ao pc | |
estado_pin_4 = digitalRead(13); | |
} | |
if(estado_pin_4 == LOW){ | |
//A porta 13 recebe a informacao de que ela esta desligada | |
digitalWrite(13, LOW); | |
Keyboard.end(); | |
} | |
} | |
if(estado_pin_5 == HIGH){ | |
Keyboard.begin(); | |
while(estado_pin_5 == HIGH){ | |
//Right is pressed | |
//Serial.println("Right"); | |
Keyboard.println("Right"); | |
estado_pin_5 = digitalRead(8); | |
} | |
if(estado_pin_5 == LOW){ | |
//A porta 8 recebe a informacao de que ela esta desligada | |
digitalWrite(8, LOW); | |
Keyboard.end(); | |
} | |
} | |
printf("\n"); | |
delay("50000000"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment