Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Christine Wu Final Project
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(14,15,16,17,18,19);
- #define NOTE_C2 65
- #define NOTE_CS2 69
- #define NOTE_D2 73
- #define NOTE_DS2 78
- #define NOTE_E2 82
- #define NOTE_F2 87
- #define NOTE_FS2 93
- #define NOTE_G2 98
- #define NOTE_GS2 104
- #define NOTE_A2 110
- #define NOTE_AS2 117
- #define NOTE_B2 123
- #define NOTE_C3 131
- #define NOTE_CS3 139
- #define NOTE_D3 147
- #define NOTE_DS3 156
- #define NOTE_E3 165
- #define NOTE_F3 175
- #define NOTE_FS3 185
- #define NOTE_G3 196
- #define NOTE_GS3 208
- #define NOTE_A3 220
- #define NOTE_AS3 233
- #define NOTE_B3 247
- #define NOTE_C4 262
- #define NOTE_CS4 277
- #define NOTE_D4 294
- #define NOTE_DS4 311
- #define NOTE_E4 330
- #define NOTE_F4 349
- #define NOTE_FS4 370
- #define NOTE_G4 392
- #define NOTE_GS4 415
- #define NOTE_A4 440
- #define NOTE_AS4 466
- #define NOTE_B4 494
- #define NOTE_C5 523
- #define NOTE_CS5 554
- #define NOTE_D5 587
- #define NOTE_DS5 622
- #define NOTE_E5 659
- #define NOTE_F5 698
- #define NOTE_FS5 740
- #define NOTE_G5 784
- #define NOTE_GS5 831
- #define NOTE_A5 880
- #define NOTE_AS5 932
- #define NOTE_B5 988
- #define NOTE_C6 1047
- #define NOTE_CS6 1109
- #define NOTE_D6 1175
- #define NOTE_DS6 1245
- #define NOTE_E6 1319
- #define NOTE_F6 1397
- #define NOTE_FS6 1480
- #define NOTE_G6 1568
- #define NOTE_GS6 1661
- #define NOTE_A6 1760
- #define NOTE_AS6 1865
- #define NOTE_B6 1976
- #define NOTE_C7 2093
- #define NOTE_CS7 2217
- #define NOTE_D7 2349
- #define NOTE_DS7 2489
- #define NOTE_E7 2637
- #define NOTE_F7 2794
- #define NOTE_FS7 2960
- #define NOTE_G7 3136
- #define NOTE_GS7 3322
- #define NOTE_A7 3520
- #define melodyPin 13
- //Fur Elise Beethoven
- int melody7[] =
- {
- NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_DS5,
- NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5,
- NOTE_A4, 0, 0, NOTE_C4,
- NOTE_E4, NOTE_A4, NOTE_B4, 0,
- 0, NOTE_E4, NOTE_GS4, NOTE_B4,
- NOTE_C5, 0, 0, NOTE_E4,
- NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_DS5,
- NOTE_E5, NOTE_B4, NOTE_D5, NOTE_C5,
- NOTE_A4, 0, 0, NOTE_C4,
- NOTE_E4, NOTE_A4, NOTE_B4, 0,
- 0, NOTE_E4, NOTE_C5, NOTE_B4,
- NOTE_A4, 0, 0, 0,
- };
- int tempo7[] =
- {
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- };
- //song B Bach minuet in G major
- int melody8[] =
- {
- NOTE_D5, 0, NOTE_G4, NOTE_A4,
- NOTE_B4, NOTE_C5, NOTE_D5, 0,
- NOTE_G4, 0, NOTE_G4, 0,
- NOTE_E5, 0, NOTE_C5, NOTE_D5,
- NOTE_E5, NOTE_FS5, NOTE_G5, 0,
- NOTE_G4, 0, NOTE_G4, 0,
- NOTE_C5, 0, NOTE_D5, NOTE_C5,
- NOTE_B4, NOTE_A4, NOTE_B4, 0,
- NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4,
- NOTE_FS4, 0, NOTE_G4, NOTE_A4,
- NOTE_B4, NOTE_G4, NOTE_B4, NOTE_A4,
- 0, 0, 0, 0,
- NOTE_D5, 0, NOTE_G4, NOTE_A4,
- NOTE_B4, NOTE_C5, NOTE_D5, 0,
- NOTE_G4, 0, NOTE_G4, 0,
- NOTE_E5, 0, NOTE_C5, NOTE_D5,
- NOTE_E5, NOTE_FS5, NOTE_G5, 0,
- NOTE_G4, 0, NOTE_G4, 0,
- NOTE_C5, 0, NOTE_D5, NOTE_C5,
- NOTE_B4, NOTE_A4, NOTE_B4, 0,
- NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4,
- NOTE_A4, 0, NOTE_B4, NOTE_A4,
- NOTE_G4, NOTE_FS4, NOTE_G4, 0,
- 0, 0, 0, 0,
- };
- int tempo8[] =
- {
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- };
- //song C Rondo Alla Turca Mozart
- int melodymozart[] =
- {
- NOTE_B4, NOTE_A4, NOTE_GS4, NOTE_A4,
- NOTE_C5, 0, 0, 0,
- NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5,
- NOTE_E5, 0, 0, 0,
- NOTE_F5, NOTE_E5, NOTE_DS5, NOTE_E5,
- NOTE_B5, NOTE_A5, NOTE_GS5, NOTE_A5,
- NOTE_B5, NOTE_A5, NOTE_GS5, NOTE_A5,
- NOTE_C6, 0, 0, 0,
- NOTE_A5, 0, NOTE_C6, 0,
- NOTE_B5, 0, NOTE_A5, 0,
- NOTE_G5, 0, NOTE_A5, 0,
- NOTE_B5, 0, NOTE_A5, 0,
- NOTE_G5, 0, NOTE_A5, 0,
- NOTE_B5, 0, NOTE_A5, 0,
- NOTE_G5, 0, NOTE_FS5, 0,
- NOTE_E5, 0, 0, 0,
- };
- int tempomozart[] =
- {
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- };
- //In the hall of the mountain king grieg
- int melody9[] =
- {
- NOTE_B3, NOTE_CS4, NOTE_D4, NOTE_E4,
- NOTE_FS4, NOTE_D4, NOTE_FS4, 0,
- NOTE_F4, NOTE_CS4, NOTE_F4, 0,
- NOTE_E4, NOTE_C4, NOTE_E4, 0,
- NOTE_B3, NOTE_CS4, NOTE_D4, NOTE_E4,
- NOTE_FS4, NOTE_D4, NOTE_FS4, NOTE_B4,
- NOTE_A4, NOTE_FS4, NOTE_D4, NOTE_FS4,
- NOTE_A4, 0, 0, 0,
- NOTE_FS4, NOTE_GS4, NOTE_AS4, NOTE_B4,
- NOTE_CS5, NOTE_AS4, NOTE_CS5, 0,
- NOTE_D5, NOTE_AS4, NOTE_D5, 0,
- NOTE_CS5, NOTE_AS4, NOTE_CS5, 0,
- NOTE_FS4, NOTE_GS4, NOTE_AS4, NOTE_B4,
- NOTE_CS5, NOTE_AS4, NOTE_CS5, 0,
- NOTE_D5, NOTE_AS4, NOTE_D5, 0,
- NOTE_CS5, 0, 0, 0,
- };
- int tempo9[] =
- {
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- };
- //Nocturne opus 9 no 2
- int melodyC[] =
- {
- NOTE_AS4, NOTE_G5, 0, 0,
- 0, NOTE_F5, NOTE_G5, NOTE_F5,
- 0, 0, NOTE_DS5, 0,
- NOTE_AS4, NOTE_G5, 0, NOTE_C5,
- NOTE_C6, 0, NOTE_G5, NOTE_AS5,
- 0, 0, NOTE_GS5, 0,
- NOTE_G5, NOTE_F5, 0, 0,
- NOTE_G5, 0, NOTE_D5, NOTE_DS5,
- 0, 0, NOTE_C5, 0,
- 0, NOTE_AS4, NOTE_D6, NOTE_C6,
- NOTE_AS5, NOTE_GS5, NOTE_G5, NOTE_GS5,
- NOTE_C5, NOTE_D5, NOTE_DS5, 0,
- };
- int tempoC[] =
- {
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 16, 16, 16, 16,
- 16, 16, 12, 12,
- };
- //traumerei
- int melody0[] =
- {
- 0, 0, NOTE_C4, 0,
- NOTE_F4, 0, 0, 0,
- 0, NOTE_E4, NOTE_F4, NOTE_A4,
- NOTE_C5, NOTE_F5, NOTE_F5, 0,
- 0, 0, NOTE_E5, NOTE_D5,
- NOTE_C5, NOTE_F5, NOTE_G4, NOTE_A4,
- NOTE_AS4, NOTE_D5, NOTE_F4, NOTE_G4,
- NOTE_A4, NOTE_C5, NOTE_G4, 0,
- 0, 0, NOTE_C4, 0,
- NOTE_F4, 0, 0, 0,
- 0, NOTE_E4, NOTE_F4, NOTE_A4,
- NOTE_C5, NOTE_A5, NOTE_A5,
- 0, NOTE_G5, NOTE_F5, NOTE_E5,
- NOTE_F5, NOTE_A5, NOTE_D5, NOTE_F5,
- NOTE_E5, NOTE_C5, NOTE_E4, NOTE_DS5,
- NOTE_D5, NOTE_G4, NOTE_E5, 0,
- NOTE_C5, 0, 0, 0,
- };
- int tempo0[] =
- {
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- };
- #include <Keypad.h>
- const int piezzo=13;
- int ReadValue;
- int toneval;
- const byte ROWS = 4;
- const byte COLS = 4;
- char hexaKeys[ROWS][COLS] = {
- {'D','C','B','A'},
- {'#','9','6','3'},
- {'0','8','5','2'},
- {'*','7','4','1'}
- };
- byte rowPins[ROWS] = {9, 8, 7, 6};
- byte colPins[COLS] = {5, 4, 3, 2};
- //initialize an instance of class NewKeypad
- Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
- void setup()
- {
- Serial.begin(9600);
- pinMode (piezzo,OUTPUT);
- Serial.begin (9600);
- lcd.begin (16,2);
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- void loop()
- {
- toneval=0;
- char customKey = customKeypad.getKey();
- Serial.println(customKey);
- delay (80);
- if (customKey)
- {
- Serial.println(customKey);
- }
- if (customKey == '1')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("C");
- toneval=262;
- }
- if (customKey == '2')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("D");
- toneval=294;
- }
- if (customKey == '3')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("E");
- toneval=330;
- }
- if (customKey == 'A')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("F");
- toneval=349;
- }
- if (customKey == '4')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("G");
- toneval=392;
- }
- if (customKey == '5')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("A");
- toneval=440;
- }
- if (customKey == '6')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("B");
- toneval=494;
- }
- if (customKey == 'B')
- {
- lcd.clear();
- lcd.setCursor (7,0);
- lcd.print("C");
- toneval=523;
- }
- if (customKey == '7')
- {
- lcd.clear();
- lcd.setCursor (3,0);
- lcd.print("Fur Elise");
- lcd.setCursor (4,1);
- lcd.print ("Beethoven");
- delay(100);
- sing7(1);
- sing7(1);
- lcd.clear();
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- if (customKey == '8')
- {
- lcd.clear();
- lcd.setCursor (2,0);
- lcd.print("Minuet in G");
- lcd.setCursor (6,1);
- lcd.print ("Bach");
- delay(100);
- sing8(1);
- lcd.clear();
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- if (customKey == '9')
- {
- lcd.clear();
- lcd.setCursor (2,0);
- lcd.print("In the Hall");
- lcd.setCursor (6,1);
- lcd.print ("Grieg");
- delay(600);
- lcd.clear();
- lcd.setCursor (4,0);
- lcd.print("of the");
- lcd.setCursor (6,1);
- lcd.print ("Grieg");
- delay(600);
- lcd.clear();
- lcd.setCursor (1,0);
- lcd.print("Mountain King");
- lcd.setCursor (6,1);
- lcd.print ("Grieg");
- delay(100);
- sing9(1);
- lcd.clear();
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- if (customKey == '*')
- {
- lcd.clear();
- lcd.setCursor (0,0);
- lcd.print("Rondo Alla Turka");
- lcd.setCursor (4,1);
- lcd.print ("Mozart");
- delay(100);
- singmozart(1);
- singmozart(1);
- lcd.clear();
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- if (customKey == 'C')
- {
- lcd.clear();
- lcd.setCursor (0,0);
- lcd.print("Nocturne Op9 No2");
- lcd.setCursor (6,1);
- lcd.print ("Chopin");
- delay(100);
- singC(1);
- lcd.clear();
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- if (customKey == '0')
- {
- lcd.clear();
- lcd.setCursor (3,0);
- lcd.print("Traumerei");
- lcd.setCursor (3,1);
- lcd.print ("Schumann");
- delay(100);
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- if (customKey == '#')
- {
- toneval=440;
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- if (customKey == 'D')
- {
- toneval=440;
- lcd.setCursor (1,0);
- lcd.print ("Press a button");
- lcd.setCursor (1,1);
- lcd.print ("to play music");
- }
- tone (piezzo,toneval,400);
- }
- //Fur elise beethoven
- int song7 = 0;
- void sing7(int s7)
- {
- song7 = s7;
- if (song7 == 1)
- {
- Serial.println("'____'");
- int size = sizeof(melody7) / sizeof(int);
- for (int thisNote = 0; thisNote < size; thisNote++)
- {
- int noteDuration = 1000 / tempo7[thisNote];
- buzz(melodyPin, melody7[thisNote], noteDuration);
- int pauseBetweenNotes = noteDuration * 1.30;
- delay(pauseBetweenNotes);
- buzz(melodyPin, 0, noteDuration);
- }
- }
- }
- //minuet in G bach
- int song8 = 0;
- void sing8(int s8)
- {
- song8 = s8;
- if (song8 == 1)
- {
- Serial.println("'____'");
- int size = sizeof(melody8) / sizeof(int);
- for (int thisNote = 0; thisNote < size; thisNote++)
- {
- int noteDuration = 1200 / tempo8[thisNote];
- buzz(melodyPin, melody8[thisNote], noteDuration);
- int pauseBetweenNotes = noteDuration * 1.50;
- delay(pauseBetweenNotes);
- buzz(melodyPin, 0, noteDuration);
- }
- }
- }
- //rondo alla turka
- int songmozart = 0;
- void singmozart(int smozart)
- {
- songmozart = smozart;
- if (songmozart == 1)
- {
- Serial.println("'____'");
- int size = sizeof(melodymozart) / sizeof(int);
- for (int thisNote = 0; thisNote < size; thisNote++)
- {
- int noteDuration = 900 / tempomozart[thisNote];
- buzz(melodyPin, melodymozart[thisNote], noteDuration);
- int pauseBetweenNotes = noteDuration * 1.20;
- delay(pauseBetweenNotes);
- buzz(melodyPin, 0, noteDuration);
- }
- }
- }
- //hall of the mountain king
- int song9 = 0;
- void sing9(int s9)
- {
- song9 = s9;
- if (song9 == 1)
- {
- Serial.println("'____'");
- int size = sizeof(melody9) / sizeof(int);
- for (int thisNote = 0; thisNote < size; thisNote++)
- {
- int noteDuration = 1100 / tempo9[thisNote];
- buzz(melodyPin, melody9[thisNote], noteDuration);
- int pauseBetweenNotes = noteDuration * 1.40;
- delay(pauseBetweenNotes);
- buzz(melodyPin, 0, noteDuration);
- }
- }
- }
- //nocturne opus 9 no 2
- int songC = 0;
- void singC(int sC)
- {
- songC = sC;
- if (songC == 1)
- {
- Serial.println("'____'");
- int size = sizeof(melodyC) / sizeof(int);
- for (int thisNote = 0; thisNote < size; thisNote++)
- {
- int noteDuration = 1600 / tempoC[thisNote];
- buzz(melodyPin, melodyC[thisNote], noteDuration);
- int pauseBetweenNotes = noteDuration * 1.90;
- delay(pauseBetweenNotes);
- buzz(melodyPin, 0, noteDuration);
- }
- }
- }
- //traumerei
- int song0 = 0;
- void sing0(int s0)
- {
- song0 = s0;
- if (song0 == 1)
- {
- Serial.println("'____'");
- int size = sizeof(melody0) / sizeof(int);
- for (int thisNote = 0; thisNote < size; thisNote++)
- {
- // to calculate the note duration, take one second
- // divided by the note type.
- //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
- int noteDuration = 1000 / tempo0[thisNote];
- buzz(melodyPin, melody0[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:
- buzz(melodyPin, 0, noteDuration);
- }
- }
- }
- void buzz(int targetPin, long frequency, long length)
- {
- digitalWrite(13, HIGH);
- long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
- //// 1 second's worth of microseconds, divided by the frequency, then split in half since
- //// there are two phases to each cycle
- long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
- //// multiply frequency, which is really cycles per second, by the number of seconds to
- //// get the total number of cycles to produce
- for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
- digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
- delayMicroseconds(delayValue); // wait for the calculated delay value
- digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
- delayMicroseconds(delayValue); // wait again or the calculated delay value
- }
- digitalWrite(13, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement