#include "display.h"
#include "buttons.h"
#include "SpedenSpelit.h"
volatile int buttonNumber = -1;
volatile bool newTimerInterrupt = false;
byte currentLed = 0;
byte score = 0;
void setup(void)
{
Serial.begin(9600);
//initializeLeds();
initializeDisplay();
initButtonsAndButtonInterrupts();
initializeGame();
randomSeed(analogRead(A0));
}
void loop(void)
{
if(buttonNumber >= 0)
{
byte pressedButton = buttonNumber;
buttonNumber = -1;
if(pressedButton == 4)
{
startTheGame();
}
else if(pressedButton < 4)
{
checkGame(pressedButton);
}
}
if(newTimerInterrupt == true)
{
newTimerInterrupt = false;
// clearAllLeds();
currentLed = random(0, 4);
//setLed(currentLed);
}
}
void initializeTimer(void)
{
cli();
TCCR1A = 0;
TCCR1B = 0;
TCCR1B |= (1 << WGM12);
OCR1A = 15624;
TCNT1 = 0;
TIMSK1 |= (1 << OCIE1A);
sei();
}
ISR(TIMER1_COMPA_vect)
{
newTimerInterrupt = true;
}
void checkGame(byte nbrOfButtonPush)
{
if(nbrOfButtonPush == currentLed)
{
score++;
showResult(score);
// clearAllLeds();
if(score % 10 == 0)
{
OCR1A = (OCR1A * 9UL) / 10UL;
}
}
else
{
//clearAllLeds();
showResult(score);
TIMSK1 &= ~(1 << OCIE1A);
TCCR1B &= ~((1 << CS12) |
(1 << CS11) |
(1 << CS10));
newTimerInterrupt = false;
}
}
void initializeGame(void)
{
currentLed = 0;
//clearAllLeds();
showResult(score);
newTimerInterrupt = false;
}
void startTheGame(void)
{
initializeGame();
OCR1A = 15624;
TCNT1 = 0;
TIMSK1 |= (1 << OCIE1A);
TCCR1B |= (1 << CS12);
TCCR1B |= (1 << CS10);
sei();
}