Загрузка данных


#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Настройки экрана (проверь адрес 0x27 или 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Пины для датчиков (берём D0-выходы)
const int sensor1 = 3; // датчик слева
const int sensor2 = 4; // датчик справа

// Пин для кнопки сброса
const int resetButton = 2;

// Счёт
int score1 = 0;
int score2 = 0;

// Чтобы избежать многократного счёта при удержании
bool lastState1 = HIGH;
bool lastState2 = HIGH;

void setup() {
  // Экран
  lcd.init();
  lcd.backlight();
  lcd.clear();

  // Пины
  pinMode(sensor1, INPUT);
  pinMode(sensor2, INPUT);
  pinMode(resetButton, INPUT_PULLUP);

  // Serial монитор
  Serial.begin(9600);
  Serial.println("Система запущена!");

  // Первичное отображение
  updateScore();
}

void loop() {
  // Читаем датчики
  int state1 = digitalRead(sensor1);
  int state2 = digitalRead(sensor2);

  // Печать состояний в Serial Monitor
  Serial.print("Датчик 1: ");
  Serial.print(state1);
  Serial.print("   |   Датчик 2: ");
  Serial.println(state2);

  // Датчик 1 сработал (LOW при срабатывании)
  if (state1 == LOW && lastState1 == HIGH) {
    score1++;
    updateScore();
    Serial.println("ГОЛ! Слева +1");
    delay(200); // антидребезг
  }
  lastState1 = state1;

  // Датчик 2 сработал
  if (state2 == LOW && lastState2 == HIGH) {
    score2++;
    updateScore();
    Serial.println("ГОЛ! Справа +1");
    delay(200);
  }
  lastState2 = state2;

  // Проверка кнопки сброса
  if (digitalRead(resetButton) == LOW) {
    score1 = 0;
    score2 = 0;
    updateScore();
    Serial.println("Счёт сброшен!");
    delay(300); // защита от дребезга
  }
}

// Функция для обновления счёта
void updateScore() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("LEFT: ");
  lcd.print(score1);

  lcd.setCursor(0, 1);
  lcd.print("RIGHT: ");
  lcd.print(score2);
}