/*
ESP
Juego de ping pong
cuando el jugador 1 presione el boton en la posicion 2 o 3 se cambia la direccion
cuando el jugador 2 presione el boton en la posicion 10 y 11 se cambia la direccion
Si el led de la posicion 1 se enciende jugador 2 gana un punto, si el led de la
posicion 12 se enciende jugador 1 gana un punto.
La velocidad aumenta cada vez que cambia de direccion y no se hace ningun
punto
ENG
Ping pong game
Only when player 1 presses the button at position 2 or 3 the direction is changed.
when player 2 presses the button at position 10 and 11 the direction is changed.
If the led in position 1 lights up player 2 wins a point,
if the led in position 12 lights up player 1 wins a point.
position 12 lights up player 1 wins one point.
The speed increases each time the direction is changed and no points are scored.
point is made
*/
#include "LedControl.h"
#define BOTONJ1 A0
#define BOTONJ2 A1
#define INTERVALOPRINCIPAL 300
LedControl lc = LedControl(13,12,11, 1);
byte leds[] = {A2,A3,A4,A5,2,3,4,5,6,7,8,9};
byte posicionEncendido = 4;
boolean direccion = true; //true va a la derecha
byte j1 = 0, j2 = 0, posValidaJ1 = 2, posValidaJ2 = 9;
byte puntosJ1 = 0, puntosJ2 = 0;
boolean estadoLed = LOW; // ledState used to set the LED
int intervalo = INTERVALOPRINCIPAL;
unsigned long millisAnterior = 0; // will store last time LED was updated
void setup() {
// put your setup code here, to run once:
for (int i = 0; i < 12; i ++) {
pinMode(leds[i], OUTPUT);
// digitalWrite(leds[i], HIGH);
// delay(200);
// digitalWrite(leds[i], LOW);
}
pinMode(BOTONJ1, INPUT_PULLUP);
pinMode(BOTONJ2, INPUT_PULLUP);
Serial.begin(115200);
lc.shutdown(0, false);
/* Set the brightness to a medium values */
lc.setIntensity(0, 8);
/* and clear the display */
lc.clearDisplay(0);
}
void encenderLed() {
}
void puntoJugador1() {
Serial.println("puntoJugador 1 ");
direccion = false;
posicionEncendido = 11;
intervalo = INTERVALOPRINCIPAL;
lc.setLed(0, 0, puntosJ1, true);
puntosJ1++;
}
void puntoJugador2() {
Serial.println("puntoJugador 2 ");
direccion = true;
posicionEncendido = 0;
intervalo = INTERVALOPRINCIPAL;
lc.setLed(0, 7, puntosJ2, true);
puntosJ2++;
}
void loop() {
// put your main code here, to run repeatedly:
unsigned long millisActuales = millis();
if (digitalRead(BOTONJ1 ) == LOW && direccion == false && (posicionEncendido == posValidaJ1 || posicionEncendido == posValidaJ1 - 1 )) {
direccion = true;
intervalo -= 50;
Serial.println("boton1 presionado");
delay(200);
}
if (digitalRead(BOTONJ2 ) == LOW && direccion == true && (posicionEncendido == posValidaJ2 || posicionEncendido == posValidaJ2 + 1 )) {
direccion = false;
intervalo -= 50;
Serial.println("boton2 presionado");
delay(200);
}
if (millisActuales - millisAnterior >= intervalo / 2) {
millisAnterior = millisActuales;
if (estadoLed == LOW) {
estadoLed = HIGH;
} else {
estadoLed = LOW;
if (direccion) {
posicionEncendido ++;
if (posicionEncendido >= 11) {
puntoJugador1();
}
}
else {
posicionEncendido --;
if (posicionEncendido <= 0) {
puntoJugador2();
}
}
}
// set the LED with the ledState of the variable:
digitalWrite(leds[posicionEncendido], !estadoLed);
}
}