Operácie

Šach

Z SensorWiki

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

Šachovnica a figúrky
Časovač
Šachovnica

Zadanie

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:


Šachovnica

Naším hlavným cieľom pri vytváraní šachovnice bolo navrhnúť a vyrobiť vizuálne atraktívnu a funkčnú hernú plochu. Pri vytváraní našej šachovnice sme sa rozhodli použiť plexisklo, pretože sme mali pocit, že je to ideálny materiál na vytvorenie vizuálne príjemnej šachovnice. Na vytvorenie návrhu šachovnice sme využili softvér Inkscape. Nakoniec sme na samotnú výrobu šachovnice použili laserovú rezačku.

SVG File

Figúrky

Pre figúrky sme si stanovili cieľ vytvoriť ich pomocou 3D tlačiarne.


Lineup.jpg
Lineup2.jpg



Časovač

Naším cieľom bolo vytvoriť časovač, ktorý by sa ľahko používal a zobrazoval čas hry. Použili sme Arduino, tlačidlá, LCD displej a naprogramovali sme zariadenie tak, aby sme dosiahli požadovanú funkčnosť časovača.


Schéma zapojenia.

Funkcionalita

- Menu: Môžete si vybrať čas. T1 nastavuje čas, T2 je štart.

- Štart: Začne sa odpočítavať čas prvým hráčom. Ak prvý hráč stlačí svoje tlačidlo, začne sa odpočítavať čas druhého hráča.

- Hra: Predchádzajúci bod pokračuje, kým nevyprší čas jedného hráča.

- Stop: Ak sú obe tlačidlá súčasne stlačené, vráti sa späť do menu.


#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
  }
    }
  }
}

Zdrojový kód: zdrojaksach.zip

Timer.