#include "pitches.h"
const int segmentPins[7] = {2, 3, 4, 5, 6, 7, 8};
const int buzzerPin = 9; // Pin connected to the buzzer
const int digit[10][7] = {
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 0, 1, 0, 0} // 9
};
// Notes of the melody
int melody[] = {
NOTE_E4, NOTE_E4, NOTE_E4, // Jingle bells, jingle bells, jingle all the way
NOTE_E4, NOTE_E4, NOTE_E4, // Jingle bells, jingle bells, jingle all the way
NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4, // Oh, what fun it is to ride
NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4, // In a one-horse open sleigh
NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4 // Hey, jingle bells
};
// Note durations: 4 = quarter note, 8 = eighth note, etc.
int noteDurations[] = {
8, 8, 4, // Jingle bells, jingle bells, jingle all the way
8, 8, 4, // Jingle bells, jingle bells, jingle all the way
8, 8, 8, 8, 4, // Oh, what fun it is to ride
8, 8, 8, 8, 8, 8, 8, 8, 8, // In a one-horse open sleigh
8, 8, 8, 8, 8, 8 // Hey, jingle bells
};
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
noTone(buzzerPin); // Ensure the buzzer is off initially
}
void loop() {
for (int number = 9; number >= 0; number--) {
displayNumber(number);
delay(1000); // Wait for 1 second
}
playMelody(); // Play "Jingle Bells" melody
}
void displayNumber(int number) {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], digit[number][i]);
}
}
void playMelody() {
for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}