Paso 3: Si las declaraciones
Ahora que tenemos nuestras animaciones y configurar las bibliotecas, tenemos que reforzar nuestro bucle principal. Básicamente tenemos dos condiciones. Si se presiona el botón de inicio, reproduce una animación y envíe un mensaje de infrarrojos a otro jugador. De lo contrario, si el jugador recibe un mensaje de infrarrojos, debemos jugar la animación de recepción.
Para detectar si se ha pulsado el botón de inicio, utilizamos la función isPressed().
Para comprobar si hemos recibido algo, utilizamos la función infrared.receive(). Estamos comprobando "h" porque ese es el mensaje que te envío. Puede enviar prácticamente cualquier letra. Hemos elegido "h", que significa "alto".
Aquí es lo que nuestro código debe parecerse.
#include <Gamer.h> #include <GamerIR.h> #include <SoftwareSerial.h> #define NUMFRAMESSENDANIMATION 12 byte sendAnimation[NUMFRAMESSENDANIMATION][8] = { {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B11111111}, {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B11111111, B01111110}, {B00000000, B00000000, B00000000, B00000000, B00000000, B11111111, B01111110, B00111100}, {B00000000, B00000000, B00000000, B00000000, B11111111, B01111110, B00111100, B00011000}, {B00000000, B00000000, B00000000, B11111111, B01111110, B00111100, B00011000, B00000000}, {B00000000, B00000000, B11111111, B01111110, B00111100, B00011000, B00000000, B00000000}, {B00000000, B11111111, B01111110, B00111100, B00011000, B00000000, B00000000, B00000000}, {B11111111, B01111110, B00111100, B00011000, B00000000, B00000000, B00000000, B00000000}, {B01111110, B00111100, B00011000, B00000000, B00000000, B00000000, B00000000, B00000000}, {B00111100, B00011000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000}, {B00011000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000}, {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000}}; #define NUMFRAMESRECEIVEANIMATION 12 byte receiveAnimation[NUMFRAMESRECEIVEANIMATION][8] = { {B11111111, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000}, {B01111110, B11111111, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000}, {B00111100, B01111110, B11111111, B00000000, B00000000, B00000000, B00000000, B00000000}, {B00011000, B00111100, B01111110, B11111111, B00000000, B00000000, B00000000, B00000000}, {B00000000, B00011000, B00111100, B01111110, B11111111, B00000000, B00000000, B00000000}, {B00000000, B00000000, B00011000, B00111100, B01111110, B11111111, B00000000, B00000000}, {B00000000, B00000000, B00000000, B00011000, B00111100, B01111110, B11111111, B00000000}, {B00000000, B00000000, B00000000, B00000000, B00011000, B00111100, B01111110, B11111111}, {B00000000, B00000000, B00000000, B00000000, B00000000, B00011000, B00111100, B01111110}, {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00011000, B00111100}, {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00011000}, {B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000}}; Gamer gamer; GamerIR infrared; void setup() { gamer.begin(); } void loop() { if(gamer.isPressed(START)) { } if(infrared.receive() == "h") { } }