Operácie

Šach

Z SensorWiki

Záverečný projekt predmetu DTV / LS2023 - Peter Marosi, Botond Csóka Botond

Zadanie

Šachovnica a figúrky
Časovač

Cieľom tohto projektu bolo vytvoriť kompletný súpravu pre hru šachy, ktorá by spájala estetiku, funkčnosť a jednoduché ovládanie. Táto dokumentácia bude slúžiť ako podrobný záznam nášho postupu a výsledkov, a poskytne podklady pre ďalšie vylepšenia a použitie projektu.

Literatúra:



Analýza a opis riešenia

Opíšte sem čo a ako ste spravili, ak treba, doplňte obrázkami...

RGB LED.

Nezabudnite doplniť schému zapojenia!

Schéma zapojenia LCD displeja.


Algoritmus a program

Algoritmus programu je....


#include <LiquidCrystal.h>

LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
const int buttonPin = 9;
const int buttonPin2 = 10;
int buttonState = 0;
int buttonState2 = 0;
int gameTimeIndex = 0;
float gameTimes[] = {3, 5, 15, 60};              // Time in minutes
int currentPlayer = 0;                           // Current player 0/1
unsigned long startTime = 0;                     // Start Time
unsigned long elapsedTime = 0;                   // Elapsed Time
bool isInMenu = true;
unsigned long player1ElapsedTime = 0;
unsigned long player2ElapsedTime = 0;
 unsigned long remainingTime=0;
 unsigned long isondisplay=0;

void setup() {
  lcd.begin(16, 2);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {

  if (isInMenu) {                                // Menu
    
    if (isondisplay==0){
    lcd.clear();
    lcd.print("Choose time");
    lcd.setCursor(0, 1);
    if (gameTimes[gameTimeIndex] < 10) {
      lcd.print("0");
    }
    lcd.print(gameTimes[gameTimeIndex]);
    lcd.print(":00");
    isondisplay=1;
    }
    if (buttonState != digitalRead(buttonPin)) {
                                                 // BTN 1, change time intervals
      isondisplay=0;
      buttonState = !buttonState;
      if (buttonState == LOW) {
        gameTimeIndex = (gameTimeIndex + 1) % (sizeof(gameTimes) / sizeof(gameTimes[0]));
        delay(500);                               // Delay to prevent rebounce
      }
    }

    if (buttonState2 != digitalRead(buttonPin2)) {
                                                 // BTN 2, Start
      buttonState2 = !buttonState2;
      if (buttonState2 == LOW) {
        currentPlayer = 0;
        elapsedTime = 0;
        startTime = millis();
        isInMenu = false;
        lcd.clear();
        lcd.print("Player 1");
        delay(1000);                             // Delay to display the message
      }
    }
  }
     else {
       isondisplay=0;
                                                 // Game mode
    if (currentPlayer == 0) {
                                                 // Current P0
      unsigned long currentTime = millis();
      elapsedTime += currentTime - startTime;
      startTime = currentTime;
      remainingTime = (gameTimes[gameTimeIndex] * 60 - elapsedTime / 1000) * 1000;
    } else {
                                                 // Current P1
      player2ElapsedTime += currentTime - startTime;
      startTime = currentTime;
      remainingTime = (gameTimes[gameTimeIndex] * 60 - player2ElapsedTime / 1000) * 1000;
    }

    


 if (remainingTime <= 0) {                       // Time's up

  lcd.clear();
      if(currentPlayer==0){
        lcd.print("Player 1");
      }
      else{
        lcd.print("Player 2");
      }
  lcd.setCursor(0, 1);
  lcd.print("Time's Up");
  delay(1000);                                   // Delay to display the message
  
  while (digitalRead(buttonPin) == LOW && digitalRead(buttonPin2) == LOW) {  }

                                                 // Waiting for button press
  delay(320);
  isInMenu = true;
  lcd.clear();
  return;

}
     else {
                                                 // Display Time
      
      lcd.setCursor(0, 1);
      lcd.print("Time: ");
      if (remainingTime / 1000 / 60 < 10) {
        lcd.print("0");
      }
      lcd.print(remainingTime / 1000 / 60);
      lcd.print(":");
      if ((remainingTime / 1000) % 60 < 10) {
        lcd.print("0");
      }
      lcd.print((remainingTime / 1000) % 60);
      
    }

    if (buttonState != digitalRead(buttonPin) && buttonState2 != digitalRead(buttonPin2)) {
                                                 // BTN1 & BTN2 pressed, back to menu
      buttonState = !buttonState;
      buttonState2 = !buttonState2;
           if (buttonState == LOW && buttonState2 == LOW) {
        isInMenu = true;
        lcd.clear();
      }
    }

   if (buttonState2 != digitalRead(buttonPin2) && currentPlayer==0) {
                                                 // BTN2, change players
  buttonState2 = !buttonState2;
  if (buttonState2 == LOW) {
    currentPlayer = 1;
    startTime = millis();
    lcd.setCursor(0, 0);
    lcd.print("Player 2        "); // Player 2 text
  }
}

if (buttonState != digitalRead(buttonPin) && currentPlayer == 1) {
                                                 // BTN1, change players
  buttonState = !buttonState;
  if (buttonState == LOW) {
    currentPlayer = 0;
    startTime = millis();
    lcd.setCursor(0, 0);
    lcd.print("Player 1        "); // Player 1 text
  }
    }
  }
}


Pridajte sem aj zbalený kompletný projekt, napríklad takto (použite jednoznačné pomenovanie, nemôžeme mať na serveri 10x zdrojaky.zip:

Zdrojový kód: zdrojaky.zip


Overenie

Na používanie našej aplikácie stačia dve tlačítka a postup používania je opísaný v sekcii popis riešenia. Na konci uvádzame fotku záverečnej obrazovky pred resetom. Vypísaný je tu priemerný čas a najlepší čas.

Aplikácia.

Video:

Kľúčové slová 'Category', ktoré sú na konci stránky nemeňte.