Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
timer säädöt
Browse files Browse the repository at this point in the history
  • Loading branch information
vem882 committed Oct 8, 2024
1 parent a143e4b commit 0609445
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
10 changes: 6 additions & 4 deletions Kooditoteutus/SpedenSpelit/SpedenSpelit.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void setup() {
// Ladataan korkein pistemäärä EEPROMista
highScore = EEPROM.read(0);
if (highScore == 255) {
// Alustamaton, asetetaan highScore nollaksi
// Jos Alustamaton, asetetaan highScore nollaksi
highScore = 0;
// Tallennetaan nolla EEPROMiin
EEPROM.write(0, highScore);
Expand Down Expand Up @@ -81,7 +81,7 @@ void loop() {
if (digitalRead(2) == LOW && digitalRead(3) == LOW) {
if (buttonPressTime == 0) {
buttonPressTime = millis();
} else if (millis() - buttonPressTime >= 2000) {
} else if (millis() - buttonPressTime >= 5000) {
highScore = 0;
EEPROM.write(0, highScore);
updateDisplay(highScore);
Expand All @@ -95,10 +95,12 @@ void loop() {

void initializeTimer(void) {
// Set up Timer1 for generating regular interrupts
cli();
TCCR1A = 0; // Normal operation, no PWM
TCCR1B = (1 << WGM12) | (1 << CS12); // CTC mode, prescaler 256
OCR1A = gameSpeed * 16; // Initial compare value (for 1 second intervals)
TIMSK1 |= (1 << OCIE1A); // Enable Timer1 compare interrupt
sei();
}

ISR(TIMER1_COMPA_vect) {
Expand All @@ -112,7 +114,7 @@ ISR(TIMER1_COMPA_vect) {
void initializeGame() {
currentScore = 0;
gameSpeed = 5000;
timerInterruptCount = 0;
timerInterruptCount = 1000;
gameStarted = false;
clearAllLeds();
updateDisplay(highScore); // Näytetään korkein pistemäärä alussa
Expand All @@ -122,7 +124,7 @@ void initializeGame() {
void startTheGame() {
currentScore = 0;
gameSpeed = 5000;
timerInterruptCount = 0;
timerInterruptCount = 1000;
gameStarted = true;
updateDisplay(currentScore); // Näytetään 0, kun peli alkaa
Serial.println("Game started!");
Expand Down
4 changes: 2 additions & 2 deletions Kooditoteutus/SpedenSpelit/buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ void initButtonsAndButtonInterrupts(void) {

ISR(PCINT0_vect) {
// Sources: https://docs.arduino.cc/built-in-examples/digital/Debounce/
// Allows button presses every 50 ms to get rid of button debounce
// Allows button presses every 2000 ms to get rid of button debounce
unsigned long thisInterruptTimestamp = millis();

if (thisInterruptTimestamp - lastInterruptTimestamp > 200) { // over 50 ms since last interrupt
if (thisInterruptTimestamp - lastInterruptTimestamp > 2000) { // over 50 ms since last interrupt
for (int i = 0; i < sizeof(buttonPins) / sizeof(buttonPins[0]); i++) {
if (digitalRead(buttonPins[i]) == LOW) { // checks if any pins in buttonPins is LOW
buttonNumber = i; // buttonNumber will be 0-3
Expand Down
10 changes: 5 additions & 5 deletions Kooditoteutus/SpedenSpelit/leds.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "leds.h" // Tämä riittää, älä sisällytä toista kertaa!

const int ledPins[] = {9, 10, 11, 12}; // Pins for LEDs
// LEDien alustaminen: A2, A3, A4, A5 määritellään ulostuloiksi
// LEDien alustaminen: 9, 10, 11, 12 määritellään ulostuloiksi
void initializeLeds()
{
Serial.println("Ititializing leds");
Expand All @@ -12,7 +12,7 @@ void initializeLeds()
pinMode(ledPins[i], OUTPUT);
}
setAllLeds();
delay(2000);
delay(3000);
clearAllLeds();
}

Expand Down Expand Up @@ -52,14 +52,14 @@ void show1() {
clearAllLeds();
}

// Näyttää LEDit 0-1-2-3-0-1-2-3 ... ja lisää nopeutta jokaisen kierroksen jälkeen
// Näyttää LEDit ja lisää nopeutta jokaisen kierroksen jälkeen
void show2(int rounds) {
int delayTime = 2000; // Alustava viive
int delayTime = 5000; // Alustava viive
for (int r = 0; r < rounds; r++) {
for (int i = 0; i < 4; i++) {
setLed(i); // Sytytetään yksi LED kerrallaan
delay(delayTime); // Viive
}
delayTime = max(5000, delayTime - 1000); // Pienennetään viivettä, mutta ei alle 50 ms
delayTime = max(5000, delayTime - 1000); // Pienennetään viivettä, mutta ei alle 1000 ms
}
}

0 comments on commit 0609445

Please sign in to comment.