#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLS 20
#define LCD_ROWS 4
#define BUZZER_PIN 9
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLS, LCD_ROWS);
const int songLength = 31;
char notes[] = "eee eee egcde fff ffee eeggfdc " ;
int beats[] = {2,2,3,1, 2,2,3,1, 2,2,3,1,4,4, 2,2,3,0, 1,2,2,2,0, 1,1,2,2,2,2,4,4};
int tempo = 100;
String lyrics[] = {
"Jingle bells",
"jingle bells",
"Jingle all the way",
"Oh, what fun it is to ride",
"In a one-horse open sleigh",
};
void setup()
{
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.backlight();
lcd.clear();
pinMode(BUZZER_PIN, OUTPUT);
}
void loop()
{
int i, b, duration;
for (i = 0; i < songLength; i++)
{
duration = beats[i] * tempo;
if (notes[i] == ' ')
{
delay(duration);
}
else
{
tone(BUZZER_PIN, frequency(notes[i]), duration);
digitalWrite(13, HIGH);
delay(duration);
}
digitalWrite(13, LOW);
delay(tempo/10);
}
lcd.setCursor(0, 0);
lcd.print("Jingle bell");
for (int i = 0; i < sizeof(lyrics) / sizeof(lyrics[0]); i++) {
lcd.setCursor(0, i % LCD_ROWS);
lcd.print(lyrics[i]);
playNote(notes[i], beats[i]);
delay(1000);
lcd.clear();
for(int i=0;i<10;i++){
lcd.scrollDisplayRight();
delay(2000);
}
}
int frequency(char note)
{
int i;
const int numNotes = 8;
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}
}
void playNote(int note, int duration) {
tone(BUZZER_PIN, note, duration);
delay(duration);
noTone(BUZZER_PIN);
}