#include <LiquidCrystal.h>
#include "pitches.h"
#include <Servo.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Servo myServo;
int const potPin = A0;
int potVal;
int angle;
int length,length_Edelweiss;
int tonepin = 5;
int melody[] = {
NOTE_C5, NOTE_C5, NOTE_G5, NOTE_G5, NOTE_A5, NOTE_A5, NOTE_G5,
NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_D5, NOTE_C5,
NOTE_G5, NOTE_G5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_D5,
NOTE_G5, NOTE_G5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_D5,
NOTE_C5, NOTE_C5, NOTE_G5, NOTE_G5, NOTE_A5, NOTE_A5, NOTE_G5,
NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_D5, NOTE_C5
};
int melody_Edelweiss[] = {
NOTE_C5,NOTE_E5,NOTE_D6,NOTE_C6,NOTE_G5,NOTE_F5,
NOTE_C5,NOTE_C5,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_E5,
NOTE_C5,NOTE_E5,NOTE_D6,NOTE_C6,NOTE_G5,NOTE_F5,
NOTE_C5,NOTE_E5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5,NOTE_A6
};
float duration[] = {
1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2,
1, 1, 1, 1, 1, 1, 2
};
float duration_Edelweiss[] = {
2,1,3,2,1,3,
2,1,1,1,1,3,3,
2,1,3,2,1,3,
2,1,1,1,1,3,3
};
void setup()
{
myServo.attach(6);
Serial.begin(9600);
pinMode(tonepin, OUTPUT);
length = sizeof(melody) / sizeof(melody[0]);
length_Edelweiss = sizeof(melody_Edelweiss) / sizeof(melody_Edelweiss[0]);
lcd.begin(16, 2);
}
void loop() {
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
angle = map(potVal, 0, 1023, 0, 179);
Serial.print(", angle: ");
Serial.println(angle);
myServo.write(angle);
delay(15);
lcd.clear();
if (analogRead(A0) <= 300)
{
lcd.print("Twinkle Twinkle");
lcd.setCursor(0,1);
lcd.print("Little Star");
for (int thisNote = 0; thisNote < length; thisNote++) {
int noteDuration = 250 * duration[thisNote];
tone(tonepin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.20;
delay(pauseBetweenNotes);
}
}
else if (analogRead(A0) <= 700) {
delay(2000);
lcd.print("mute");
noTone(tonepin);
delay(2000);
}
else {
lcd.print("Edelweiss");
for (int thisNote = 0; thisNote < length_Edelweiss; thisNote++) {
int noteDuration = 250 * duration_Edelweiss[thisNote];
tone(tonepin, melody_Edelweiss[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
//noTone(tonepin);
}
delay(2000);
}
}