Šach: Rozdiel medzi revíziami
Zo stránky SensorWiki
Bez shrnutí editace |
Bez shrnutí editace |
||
Riadok 58: | Riadok 58: | ||
void loop() { | void loop() { | ||
if (isInMenu) { | if (isInMenu) { // Menu | ||
if (isondisplay==0){ | if (isondisplay==0){ | ||
Riadok 72: | Riadok 72: | ||
} | } | ||
if (buttonState != digitalRead(buttonPin)) { | if (buttonState != digitalRead(buttonPin)) { | ||
// BTN 1, change time intervals | |||
isondisplay=0; | isondisplay=0; | ||
buttonState = !buttonState; | buttonState = !buttonState; | ||
Riadok 82: | Riadok 82: | ||
if (buttonState2 != digitalRead(buttonPin2)) { | if (buttonState2 != digitalRead(buttonPin2)) { | ||
// BTN 2, Start | |||
buttonState2 = !buttonState2; | buttonState2 = !buttonState2; | ||
if (buttonState2 == LOW) { | if (buttonState2 == LOW) { | ||
Riadok 91: | Riadok 91: | ||
lcd.clear(); | lcd.clear(); | ||
lcd.print("Player 1"); | lcd.print("Player 1"); | ||
delay(1000); | delay(1000); // Delay to display the message | ||
} | } | ||
} | } | ||
Riadok 97: | Riadok 97: | ||
else { | else { | ||
isondisplay=0; | isondisplay=0; | ||
// Game mode | |||
if (currentPlayer == 0) { | if (currentPlayer == 0) { | ||
// Current P0 | |||
unsigned long currentTime = millis(); | unsigned long currentTime = millis(); | ||
elapsedTime += currentTime - startTime; | elapsedTime += currentTime - startTime; | ||
Riadok 105: | Riadok 105: | ||
remainingTime = (gameTimes[gameTimeIndex] * 60 - elapsedTime / 1000) * 1000; | remainingTime = (gameTimes[gameTimeIndex] * 60 - elapsedTime / 1000) * 1000; | ||
} else { | } else { | ||
// Current P1 | |||
player2ElapsedTime += currentTime - startTime; | player2ElapsedTime += currentTime - startTime; | ||
startTime = currentTime; | startTime = currentTime; | ||
Riadok 114: | Riadok 114: | ||
if (remainingTime <= 0) { | if (remainingTime <= 0) { // Time's up | ||
lcd.clear(); | lcd.clear(); | ||
Riadok 125: | Riadok 125: | ||
lcd.setCursor(0, 1); | lcd.setCursor(0, 1); | ||
lcd.print("Time's Up"); | lcd.print("Time's Up"); | ||
delay(1000); | delay(1000); // Delay to display the message | ||
while (digitalRead(buttonPin) == LOW && digitalRead(buttonPin2) == LOW) { } | while (digitalRead(buttonPin) == LOW && digitalRead(buttonPin2) == LOW) { } | ||
// Waiting for button press | |||
delay(320); | delay(320); | ||
isInMenu = true; | isInMenu = true; | ||
Riadok 137: | Riadok 137: | ||
} | } | ||
else { | else { | ||
// Display Time | |||
lcd.setCursor(0, 1); | lcd.setCursor(0, 1); | ||
Riadok 154: | Riadok 154: | ||
if (buttonState != digitalRead(buttonPin) && buttonState2 != digitalRead(buttonPin2)) { | if (buttonState != digitalRead(buttonPin) && buttonState2 != digitalRead(buttonPin2)) { | ||
// BTN1 & BTN2 pressed, back to menu | |||
buttonState = !buttonState; | buttonState = !buttonState; | ||
buttonState2 = !buttonState2; | buttonState2 = !buttonState2; | ||
Riadok 164: | Riadok 164: | ||
if (buttonState2 != digitalRead(buttonPin2) && currentPlayer==0) { | if (buttonState2 != digitalRead(buttonPin2) && currentPlayer==0) { | ||
// BTN2, change players | |||
buttonState2 = !buttonState2; | buttonState2 = !buttonState2; | ||
if (buttonState2 == LOW) { | if (buttonState2 == LOW) { | ||
Riadok 175: | Riadok 175: | ||
if (buttonState != digitalRead(buttonPin) && currentPlayer == 1) { | if (buttonState != digitalRead(buttonPin) && currentPlayer == 1) { | ||
// BTN1, change players | |||
buttonState = !buttonState; | buttonState = !buttonState; | ||
if (buttonState == LOW) { | if (buttonState == LOW) { | ||
Riadok 187: | Riadok 187: | ||
} | } | ||
</tabs> | </tabs> | ||
Verzia z 15:38, 26. máj 2023
Záverečný projekt predmetu DTV / LS2023 - Peter Marosi, Botond Csóka Botond
Zadanie
Sem príde text zadania, ak bolo len voľne formulované, rozpíšte ho podrobnejšie
Literatúra:
Analýza a opis riešenia
Opíšte sem čo a ako ste spravili, ak treba, doplňte obrázkami...
Nezabudnite doplniť schému zapojenia!
Algoritmus a program
Algoritmus programu je....
<tab name="Arduino Code"><source lang="c++" style="background: LightYellow;">
- 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.
Video:
Kľúčové slová 'Category', ktoré sú na konci stránky nemeňte.