#define UP 2
#define DOWN 3
#define SEL 4
#define LF 5
#define RT 6
#define KEYDELAY 100
int i= 0;
int j =0;
int priv = 0;
char buf[32];
int t = 10;
bool LED_STATE = true;
int getKey(){
if (digitalRead(UP) == LOW) {
delay(KEYDELAY);
if(digitalRead(UP) == LOW) {
return(1);
}
}
if (digitalRead(DOWN) == LOW) {
delay(KEYDELAY);
if(digitalRead(DOWN) == LOW) {
return(2);
}
}
if (digitalRead(SEL) == LOW) {
delay(KEYDELAY);
if(digitalRead(SEL) == LOW) {
return(3);
}
}
if (digitalRead(LF) == LOW) {
delay(KEYDELAY);
if(digitalRead(LF) == LOW) {
return(4);
}
}
if (digitalRead(RT) == LOW) {
delay(KEYDELAY);
if(digitalRead(RT) == LOW) {
return(5);
}
}
return(0);
}
void setup() {
// put your setup code here, to run once:
pinMode(UP, INPUT_PULLUP);
pinMode(DOWN, INPUT_PULLUP);
pinMode(SEL, INPUT_PULLUP);
pinMode(LF, INPUT_PULLUP);
pinMode(RT, INPUT_PULLUP);
cli(); //stop interrupts for till we make the settings
/*1. First we reset the control register to amke sure we start with everything disabled.*/
TCCR1A = 0; // Reset entire TCCR1A to 0
TCCR1B = 0; // Reset entire TCCR1B to 0
/*2. We set the prescalar to the desired value by changing the CS10 CS12 and CS12 bits. */
TCCR1B |= B00000100; //Set CS12 to 1 so we get prescalar 256
/*3. We enable compare match mode on register A*/
TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
/*4. Set the value of register A to 31250*/
OCR1A = 31250; //Finally we set compare register A to this value
sei();
}
void loop() {
// put your main code here, to run repeatedly:
if(getKey()!=0) {
Serial.println("P");
}
}
ISR(TIMER1_COMPA_vect){
TCNT1 = 0;
t--;
if(t==0) { //First, set the timer back to 0 so it resets for next interrupt
LED_STATE = !LED_STATE; //Invert LED state
digitalWrite(13,LED_STATE);
t=10;
} //Write new state to the LED on pin D5
}