Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Interactive LED Game with 8x8 Matrix and TM1637 Display

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Interactive LED Game with 8x8 Matrix and TM1637 Display

Circuit Documentation

Summary

This circuit is designed to interface an Arduino UNO with an 8x8 LED matrix, a 4x4 keypad, a TM1637 4-digit display, a tactile switch button, and a power supply. The Arduino UNO serves as the central microcontroller, controlling the LED matrix and the TM1637 display, and reading inputs from the keypad and the tactile switch button. The circuit is powered by a 9V battery connected through a power jack.

Component List

8x8 LED Matrix

4x4 Keypad

Power Jack

TM1637

Tactile Switch Button

9V Battery

Arduino UNO

Wiring Details

8x8 LED Matrix

4x4 Keypad

Power Jack

TM1637

Tactile Switch Button

Arduino UNO

Code Documentation

#include <LedControl.h>
#include <TM1637Display.h>

// Piny MAX7219 (panel LED 8x8)
#define DATA_IN   10  // DIN
#define CLK       8   // CLK
#define LOAD      9   // CS

// Piny TM1637 (4-cyfrowy wyświetlacz)
#define TM1637_CLK 12
#define TM1637_DIO 11

// Piny dla panelu przycisków (4x4)
const int rowPins[4] = {3, 2, 1, 0};    // Rzędy: r1, r2, r3, r4
const int colPins[4] = {4, 5, 6, 7};    // Kolumny: c1, c2, c3, c4

// Pin dla przycisku startowego
#define START_BUTTON_PIN A0

// Obiekty sterujące wyświetlaczem i panelem LED
LedControl lc = LedControl(DATA_IN, CLK, LOAD, 1);  
TM1637Display display(TM1637_CLK, TM1637_DIO);      

// Flagi i zmienne gry
bool gameActive = false;  // Czy gra jest aktywna
int score = 0;            // Wynik gracza
int currentSquare = -1;   // Aktualny kwadrat, który świeci
unsigned long lightDuration = 2000; // Czas świecenia LED (ms)
unsigned long interval = 3000;      // Odstęp między kolejnymi światłami (ms)
unsigned long minLightDuration = 700;  // Minimalny czas świecenia LED (ms)
unsigned long minInterval = 600;       // Minimalny odstęp między światłami (ms)

void setup() {
  // Inicjalizacja panelu LED
  lc.shutdown(0, false);  
  lc.setIntensity(0, 8);  
  lc.clearDisplay(0);     

  // Inicjalizacja wyświetlacza TM1637
  display.setBrightness(7); 
  display.clear();          

  // Konfiguracja pinów dla przycisków
  for (int i = 0; i < 4; i++) {
    pinMode(rowPins[i], OUTPUT);
    digitalWrite(rowPins[i], HIGH);  // Rzędy w stanie wysokim
    pinMode(colPins[i], INPUT_PULLUP); // Kolumny z pull-up
  }

  // Konfiguracja przycisku startowego
  pinMode(START_BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  if (!gameActive) {
    waitForStart(); // Oczekiwanie na rozpoczęcie gry
  } else {
    playGame();     // Rozgrywka
  }
}

// Funkcja oczekująca na wciśnięcie przycisku startowego
void waitForStart() {
  display.clear(); // Wyłącz wyświetlacz
  lc.clearDisplay(0); // Wyłącz panel LED

  while (!gameActive) {
    if (digitalRead(START_BUTTON_PIN) == LOW) { // Gdy przycisk startowy jest wciśnięty
      display.showNumberDec(0); // Wyświetl 0 na początku gry
      score = 0;               // Reset wyniku
      gameActive = true;       // Rozpocznij grę
      lightDuration = 2000;    // Reset czasu świecenia LED
      interval = 3000;         // Reset odstępu między światłami
      delay(100);              // Stabilizacja wejścia
    }
  }
}

// Funkcja głównej rozgrywki
void playGame() {
  // Wybierz losowy kwadrat na panelu LED
  currentSquare = random(0, 16);  
  lightSquare(currentSquare);    

  unsigned long startTime = millis(); // Czas rozpoczęcia świecenia
  bool inputValid = false;            // Flaga dla poprawnej reakcji

  // Pętla świecenia LED
  while (millis() - startTime < lightDuration) { 
    int button = readButtons();       // Sprawdź wciśnięcie przycisków
    if (button != -1) {               // Jeśli przycisk został wciś