Untitled
unknown
plain_text
3 years ago
2.7 kB
15
Indexable
#include <Servo.h>
#include <Adafruit_LiquidCrystal.h>
Adafruit_LiquidCrystal lcd_1(0);
int Malee = 11;
int cNote = 10;
int dNote = 9;
int eNote = 8;
int fNote = 7;
int gNote = 6;
int aNote = 5;
int bNote = 4;
int cHNote = 3;
int Piezo = 2;
int pos = 0;
Servo servo;
double c = 261.63; // the frequency of note c
double d = 293.66; // the frequency of note d
double e = 329.63; // the frequency of note e
double f = 349.23; // the frequency of note f
double g = 392; // the frequency of note g
double a = 440; // the frequency of note a
double b = 493.88; // the frequency of note b
double cH = 523.25; // the frequency of note c high
unsigned long servoStartTime = 0;
unsigned long noteStartTime = 0;
bool isPlayingNote = false;
bool isPlayingMalee = false;
void setup()
{
pinMode(Malee, INPUT);
pinMode(cNote, INPUT);
pinMode(dNote, INPUT);
pinMode(eNote, INPUT);
pinMode(fNote, INPUT);
pinMode(gNote, INPUT);
pinMode(aNote, INPUT);
pinMode(bNote, INPUT);
pinMode(cHNote, INPUT);
pinMode(Piezo, OUTPUT);
Serial.begin(9600);
lcd_1.begin(16, 2);
servo.attach(12);
servo.write(0);
delay(50);
}
void playNoteWithServo(double frequency, int servoAngle)
{
servo.write(servoAngle);
tone(Piezo, frequency);
}
void stopNoteAndServo()
{
noTone(Piezo);
servo.write(0);
}
void loop()
{
int maleeState = digitalRead(Malee);
if (maleeState == HIGH && !isPlayingMalee)
{
lcd_1.setCursor(1, 0);
lcd_1.print("\"NHOO MALEE is\"");
lcd_1.setCursor(4, 1);
lcd_1.print("playing");
isPlayingMalee = true;
isPlayingNote = false;
}
else if (isPlayingMalee)
{
unsigned long elapsedTime = millis() - servoStartTime;
if (elapsedTime >= 15)
{
if (pos <= 180)
{
servo.write(pos);
pos++;
}
else
{
pos = 0;
}
servoStartTime = millis();
}
playMaleeSong();
} else
{
stopNoteAndServo();
}
}
void playMaleeSong()
{
unsigned long elapsedTime = millis() - noteStartTime;
if (elapsedTime >= 300)
{
static int noteIndex = 0;
static int notes[] = {e,d,c,d,e,e,e,0,d,d,d,0,e,g,g,0,e,d,c,d,e,e,0,d,d,e,d,c};
if (noteIndex < sizeof(notes) / sizeof(notes[0]))
{
tone(Piezo, notes[noteIndex]);
noteStartTime = millis();
noteIndex++;
}
else
{
lcd_1.setCursor(1, 0);
lcd_1.print("\"NHOO MALEE is\"");
lcd_1.setCursor(4, 1);
lcd_1.print("end");
isPlayingMalee = true;
isPlayingNote = false;
stopNoteAndServo();
}
}
}
Editor is loading...