#include <LiquidCrystal.h>
#include "IRremote.h"
#define BUZZER_ACTIVO 4
int receiver = 13;
int resultAnterior=0;
int result=0;
int greenLed=5;
int flag=0;
int suma;
int aciertos=0;
unsigned long tiempo;
unsigned long segundos;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
int translateIR(int resultAnterior){
int result; // takes action based on IR code received
switch(results.value)
{
case 0xFFA25D: lcd.clear(); lcd.print("BORRANDO RESULTADO"); result=405; delay(500); lcd.clear(); break;
case 0xFF02FD: lcd.clear(); lcd.print("ENVIANDO"); result=404; delay(500); lcd.clear(); break;
case 0xFF22DD: lcd.clear(); resultAnterior=resultAnterior/10; result=resultAnterior; break;
/*case 0xFFE21D: lcd.clear(); Serial.println("FUNC/STOP"); lcd.print("FUNC/STOP"); break;
case 0xFF629D: lcd.clear(); Serial.println("VOL+"); lcd.print("VOL+"); break;
case 0xFF22DD: lcd.clear(); Serial.println("FAST BACK"); lcd.print("FAST BACK"); break;
case 0xFF02FD: lcd.clear(); Serial.println("PAUSE"); lcd.print("PAUSE"); break;
case 0xFFC23D: lcd.clear(); Serial.println("FAST FORWARD"); lcd.print("FAST FORWARD"); break;
case 0xFFE01F: lcd.clear(); Serial.println("DOWN"); lcd.print("DOWN"); break;
case 0xFFA857: lcd.clear(); Serial.println("VOL-"); lcd.print("VOL-"); break;
case 0xFF906F: lcd.clear(); Serial.println("UP"); lcd.print("UP"); break;
case 0xFF9867: lcd.clear(); Serial.println("EQ"); lcd.print("EQ"); break;
case 0xFFB04F: lcd.clear(); Serial.println("ST/REPT"); lcd.print("ST/REPT"); break;*/
case 0xFF6897: lcd.print("0"); if(resultAnterior==0){ result=0; }else {resultAnterior=resultAnterior*10; result=resultAnterior; }break;
case 0xFF30CF: lcd.print("1"); if(resultAnterior==0){ result=1; }else{resultAnterior=resultAnterior*10+1; result=resultAnterior;} break;
case 0xFF18E7: lcd.print("2"); if(resultAnterior==0){ result=2; }else {resultAnterior=resultAnterior*10+2; result=resultAnterior; }break;
case 0xFF7A85: lcd.print("3"); if(resultAnterior==0){ result=3; }else {resultAnterior=resultAnterior*10+3; result=resultAnterior; }break;
case 0xFF10EF: lcd.print("4"); if(resultAnterior==0){ result=4; }else {resultAnterior=resultAnterior*10+4; result=resultAnterior; }break;
case 0xFF38C7: lcd.print("5"); if(resultAnterior==0){ result=5; }else {resultAnterior=resultAnterior*10+5; result=resultAnterior; }break;
case 0xFF5AA5: lcd.print("6"); if(resultAnterior==0){ result=6; }else {resultAnterior=resultAnterior*10+6; result=resultAnterior; }break;
case 0xFF42BD: lcd.print("7"); if(resultAnterior==0){ result=7; }else {resultAnterior=resultAnterior*10+7; result=resultAnterior; }break;
case 0xFF4AB5: lcd.print("8"); if(resultAnterior==0){ result=8; }else {resultAnterior=resultAnterior*10+8; result=resultAnterior; }break;
case 0xFF52AD: lcd.print("9"); if(resultAnterior==0){ result=9; }else {resultAnterior=resultAnterior*10+9; result=resultAnterior; }break;
//case 0xFFFFFFFF: Serial.println(" REPEAT"); lcd.print("REPEAT"); break;
}
delay(500);
return result;
}
int generarSuma(){
int x=random(0,100);
int y=random(0,100);
Serial.print("Suma: ");
Serial.print(x);
Serial.print(" + ");
Serial.print(y);
Serial.println();
lcd.setCursor(0,0);
lcd.print("Suma: ");
lcd.print(x);
lcd.print(" + ");
lcd.print(y);
int suma=x+y;
return suma;
}
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn();
result=0; resultAnterior=0;
pinMode (BUZZER_ACTIVO, OUTPUT);
}
void loop() {
tiempo=millis();
segundos=tiempo/1000;
lcd.setCursor(0, 1);
int operacion=1;
if(flag==0){ suma=generarSuma(); result=0; resultAnterior=0; }
flag=1;
if (irrecv.decode(&results))
{
result=translateIR(resultAnterior);
if(result!=404) resultAnterior=result;
if(result==404)
if(suma==resultAnterior){
aciertos++;
lcd.print("CORRECTO");
digitalWrite(greenLed, HIGH);
delay(2500);
digitalWrite(greenLed, LOW);
}else{
lcd.print("MAL");
digitalWrite(BUZZER_ACTIVO, HIGH);
delay(500);
digitalWrite(BUZZER_ACTIVO, LOW);
}
suma=generarSuma();
result=0; resultAnterior=0;
}else if(result==405){
lcd.print("ACIERTOS: ");
lcd.setCursor(10, 0);
lcd.print(aciertos);
lcd.setCursor(0,1);
lcd.print("TIEMPO: ");
lcd.setCursor(8, 1);
lcd.print(segundos);
delay(10000);
lcd.clear();
}else {
lcd.setCursor(0,1);
lcd.print(result);
}
irrecv.resume();
}
}
No hay comentarios:
Publicar un comentario