Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
2.0 kB
0
Indexable
Never
#include  <LiquidCrystal.h>
#include <Adafruit_NeoPixel.h> 

#define LED_PIN_1 10
#define LED_PIN_2 11
#define NUM_PIXELS 12
#define TRIG_SENSOR_1 6
#define ECHO_SENSOR_1 7
#define TRIG_SENSOR_2 8
#define ECHO_SENSOR_2 9

LiquidCrystal lcd(13, 12, 5, 4, 3, 2);
Adafruit_NeoPixel strip1(NUM_PIXELS, LED_PIN_1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(NUM_PIXELS, LED_PIN_2, NEO_GRB + NEO_KHZ800);

long gameStartTime = 0;
int counter = 0;

void setup(){
  //serial
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(TRIG_SENSOR_1, OUTPUT);
  pinMode(ECHO_SENSOR_1, INPUT);
  pinMode(TRIG_SENSOR_2, OUTPUT);
  pinMode(ECHO_SENSOR_2, INPUT);
}

void loop(){
  long distance1 = measureDistance(TRIG_SENSOR_1, ECHO_SENSOR_1);
  long distance2 = measureDistance(TRIG_SENSOR_2, ECHO_SENSOR_2);

  if(distance1 < 4 || distance2 < 4){
    setColor(0, 255, 0);
    gameStartTime = millis();
    counter++;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Score: ");
    lcd.print(counter);
    delay(1000);
  }else{
    if(millis() - gameStartTime > 1000){
      if(millis() - gameStartTime > 7500){
        setColor(255, 0, 0);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Game Over");
        counter = 0;
      }else{
        Serial.print("Esperando tiro");
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Esperando tiro");
        setColor(0, 0, 255);
      }
    }
  }
}

void setColor(int red, int green, int blue) {
  for (int i = 0; i < NUM_PIXELS; i++) {
    strip1.setPixelColor(i, strip1.Color(red, green, blue));
    strip2.setPixelColor(i, strip2.Color(red, green, blue));
    strip1.show();
    strip2.show();
  }
}

long measureDistance(int trigPin, int echoPin) {
  long t; // Variable para almacenar el tiempo de viaje de la onda
  long d; // Variable para almacenar la distancia
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  t = pulseIn(echoPin, HIGH);
  d = t/59;
  return d;
}