#include <LiquidCrystal.h>
#include "pitches.h"
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int speakerPin = 5; // the speaker attach to
int length = 26; // the nuber of notes
char notes[] = "aabacdaabaecaahfcndbggfcec "; // a space represents a rest
int beats[] = {2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 4, 0, 4, 3, 1, 4, 4, 4, 8, 16 };
int tempo = 75;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'n' };
int tones[] = { 1515, 1351, 1136, 1205, 1012, 903, 852, 759 }; // play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
lcd.begin(16, 2);
lcd.print("EPPI BORDAI TO");
pinMode(LED_BUILTIN, OUTPUT);
pinMode(speakerPin, OUTPUT); // initialize the speaker as an output
pinMode(2, INPUT);
digitalWrite(2, HIGH);
}
void loop() {
lcd.setCursor(0, 1);
lcd.print("MI");
digitalWrite(LED_BUILTIN, HIGH);
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
delay(2000);
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
}
else {
playNote(notes[i], beats[i] * tempo);
// pause between notes
if (notes[i + 1] != 'n') {
delay(tempo );
}
else {
i += 2;
int digitalVal = digitalRead(2);
if(HIGH == digitalVal)
{
digitalWrite(speakerPin, HIGH); // turn the speaker off
}
else
{
digitalWrite(speakerPin, LOW); // turn the speaker on
}
}
}
}
}
Comments