You are on page 1of 4

unsigned int seconds = 0;

int countA = 0;
bool A_pressed = 0;
bool block_A;
int buttonApin = 10;
const int buzzer = 11;
const int RightTop = 9;
const int RightBottom = 7;
const int TopMiddle = 12;
const int Middle = 5;
const int BottomLeft = 13;
const int BottomMiddle = 6;
const int DP = 8;

void setup() {
// put your setup code here, to run once:

pinMode(buttonApin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 11 as an output

cli();//stop interrupts

//set timer1 interrupt at 1Hz


TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 << CS12) | (1 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);

sei();//allow interrupts

Serial.begin(9600);

ISR(TIMER1_COMPA_vect) //timer1 interrupt 1Hz


{
seconds += 1;

if (seconds == 5)
{
tone(buzzer, 1000, 30);
Serial.print(seconds);
Serial.println(" seconds have elapsed.");
Serial.println("Counter Reset");
seconds = 0;
countA = -1;

}
else
{
Serial.print(seconds);
Serial.println(" seconds have elapsed.");
}

void loop()
// put your main code here, to run repeatedly:

if (digitalRead(buttonApin) == LOW)
{
A_pressed = 1;
block_A = 1;
}
else
{
countA = countA + A_pressed;
A_pressed = 0;
if (block_A)
{
Serial.print("Switch pressed = ");
Serial.println(countA);
delay(200); // Restore this if switch bounce is a problem
}
block_A = 0;
}

if (countA == 1)
{
digitalWrite(RightTop , HIGH);
digitalWrite(RightBottom, HIGH);
digitalWrite(DP, HIGH);
}
else
{
digitalWrite(RightTop , LOW);
digitalWrite(RightBottom , LOW);
digitalWrite(DP, LOW);
}

if (countA == 2)
{
digitalWrite(TopMiddle , HIGH);
digitalWrite(RightTop , HIGH);
digitalWrite(Middle , HIGH);
digitalWrite(BottomLeft , HIGH);
digitalWrite(BottomMiddle , HIGH);
digitalWrite(DP, HIGH);
}
else
{
digitalWrite(TopMiddle , LOW);
digitalWrite(RightTop , LOW);
digitalWrite(Middle , LOW);
digitalWrite(BottomLeft , LOW);
digitalWrite(BottomMiddle , LOW);
digitalWrite(DP, LOW);
}

if (countA == 3)
{
digitalWrite(TopMiddle , HIGH);
digitalWrite(RightTop , HIGH);
digitalWrite(Middle , HIGH);
digitalWrite(RightBottom , HIGH);
digitalWrite(BottomMiddle , HIGH);
digitalWrite(DP, HIGH);
}

else
{
digitalWrite(TopMiddle , LOW);
digitalWrite(RightTop , LOW);
digitalWrite(Middle , LOW);
digitalWrite(RightBottom ,LOW);
digitalWrite(BottomMiddle ,LOW);
digitalWrite(DP, LOW);
}

You might also like