| /* |
| |
| DIY Christmas Song Sketch |
| |
| Using gizDuino LIN-UNO (Arduino UNO). |
| |
| |
| */ |
| #include <eGizmo_SerialLCD.h> |
| #include "pitches.h" |
| |
| #define melodyPin 9 |
| |
| //For Arduino UNO or Gizduino V users |
| /* |
| SoftwareSerial mySerial(2,3); |
| eGizmo_SerialLCD mainDisplay(&myserial); |
| */ |
| |
| //For MEGA, X, and Plus users |
| eGizmo_SerialLCD mainDisplay(&Serial, 2, 16); //Set to 2x16 LCD |
| |
| // Jingle Bells |
| |
| int melody[] = { |
| NOTE_E5, NOTE_E5, NOTE_E5, |
| NOTE_E5, NOTE_E5, NOTE_E5, |
| NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5, |
| NOTE_E5, |
| NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, |
| NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, |
| NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5, |
| NOTE_D5, NOTE_G5 |
| }; |
| |
| int tempo[] = { |
| 8, 8, 4, |
| 8, 8, 4, |
| 8, 8, 8, 8, |
| 2, |
| 8, 8, 8, 8, |
| 8, 8, 8, 16, 16, |
| 8, 8, 8, 8, |
| 4, 4 |
| }; |
| |
| // We wish you a merry Christmas |
| |
| int wish_melody[] = { |
| NOTE_B3, |
| NOTE_F4, NOTE_F4, NOTE_G4, NOTE_F4, NOTE_E4, |
| NOTE_D4, NOTE_D4, NOTE_D4, |
| NOTE_G4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_F4, |
| NOTE_E4, NOTE_E4, NOTE_E4, |
| NOTE_A4, NOTE_A4, NOTE_B4, NOTE_A4, NOTE_G4, |
| NOTE_F4, NOTE_D4, NOTE_B3, NOTE_B3, |
| NOTE_D4, NOTE_G4, NOTE_E4, |
| NOTE_F4 |
| }; |
| |
| int wish_tempo[] = { |
| 4, |
| 4, 8, 8, 8, 8, |
| 4, 4, 4, |
| 4, 8, 8, 8, 8, |
| 4, 4, 4, |
| 4, 8, 8, 8, 8, |
| 4, 4, 8, 8, |
| 4, 4, 4, |
| 2 |
| }; |
| |
| // Santa Claus is coming to town |
| |
| int santa_melody[] = { |
| NOTE_G4, |
| NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4, |
| NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, NOTE_C5, |
| NOTE_E4, NOTE_F4, NOTE_G4, NOTE_G4, NOTE_G4, |
| NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, |
| NOTE_E4, NOTE_G4, NOTE_C4, NOTE_E4, |
| NOTE_D4, NOTE_F4, NOTE_B3, |
| NOTE_C4 |
| }; |
| |
| int santa_tempo[] = { |
| 8, |
| 8, 8, 4, 4, 4, |
| 8, 8, 4, 4, 4, |
| 8, 8, 4, 4, 4, |
| 8, 8, 4, 2, |
| 4, 4, 4, 4, |
| 4, 2, 4, |
| 1 |
| }; |
| |
| int SW_ONE = 0; |
| int SW_TWO = 0; |
| int SW_THREE = 0; |
| |
| #define LED_INDICATOR 10 |
| #define BUZZER 9 |
| #define SONG1 2 |
| #define SONG2 3 |
| #define SONG3 4 |
| #define SONG1_LED 5 |
| #define SONG2_LED 6 |
| #define SONG3_LED 7 |
| |
| |
| void setup(){ |
| //Initialize |
| mainDisplay.begin(); |
| |
| //mainDisplay.print(message, row, column); |
| //note: you can place your message/row/column in any positions |
| mainDisplay.print("Hello", 1,6); |
| mainDisplay.print("World!", 2,6); |
| |
| delay(2000); //2 seconds delay time |
| |
| mainDisplay.printOver("EVERYONE!", 2,6); |
| |
| delay(2000); //2 seconds delay time |
| |
| //Clear all |
| mainDisplay.clear(); |
| |
| delay(2000); //2 seconds delay time |
| |
| mainDisplay.scroll(1,"MERRY CHRISTMAS 2018!!!"); // Message Scrolling from left to right on the 1st row |
| |
| delay(1000); //1 sec delay time |
| |
| mainDisplay.scroll(2,"FROM: e-Gizmo Mechatronix Central"); // Message Scrolling from left to right on the 2nd row |
| //mainDisplay.print("SerialLCD Sample", 3,0); //Display message on 3rd row |
| //mainDisplay.print("**4x20 & 2x16 LCD**", 4,0); //Display message on 4th row |
| |
| // for(int i = 0; i <=10; i++){ |
| // mainDisplay.pinMode(i,OUTPUT); |
| // } |
| mainDisplay.pinMode(LED_INDICATOR,OUTPUT); |
| pinMode(BUZZER,OUTPUT); |
| pinMode(SONG1,INPUT); |
| pinMode(SONG2,INPUT); |
| pinMode(SONG3,INPUT); |
| mainDisplay.pinMode(SONG1_LED,INPUT); |
| mainDisplay.pinMode(SONG2_LED,INPUT); |
| mainDisplay.pinMode(SONG3_LED,INPUT); |
| mainDisplay.digitalWrite(SONG1_LED,LOW); |
| mainDisplay.digitalWrite(SONG2_LED,LOW); |
| mainDisplay.digitalWrite(SONG3_LED,LOW); |
| } |
| |
| void loop(){ |
| |
| SW_ONE = digitalRead(SONG1); |
| SW_TWO= digitalRead(SONG2); |
| SW_THREE =digitalRead(SONG3); |
| if (SW_ONE == HIGH) { |
| MUSIC(1); |
| } |
| else if (SW_TWO == HIGH) { |
| MUSIC(2); |
| } |
| else if (SW_THREE == HIGH) { |
| MUSIC(3); |
| } |
| |
| } |
| int SONG = 0; |
| void MUSIC(int s) { |
| // iterate over the notes of the melody: |
| SONG = s; |
| if (SONG == 3) { |
| Serial.println(" 'We wish you a Merry Christmas'"); |
| mainDisplay.digitalWrite(SONG1_LED,HIGH); |
| mainDisplay.digitalWrite(SONG2_LED,LOW); |
| mainDisplay.digitalWrite(SONG3_LED,LOW); |
| int size = sizeof(wish_melody) / 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 / wish_tempo[thisNote]; |
| |
| BUZZ(melodyPin, wish_melody[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); |
| |
| } |
| } |
| else if (SONG == 2) { |
| Serial.println(" 'Santa Claus is coming to town'"); |
| mainDisplay.digitalWrite(SONG1_LED,LOW); |
| mainDisplay.digitalWrite(SONG2_LED,HIGH); |
| mainDisplay.digitalWrite(SONG3_LED,LOW); |
| int size = sizeof(santa_melody) / 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 = 900 / santa_tempo[thisNote]; |
| |
| BUZZ(melodyPin, santa_melody[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); |
| |
| } |
| } |
| else { |
| |
| Serial.println(" 'Jingle Bells'"); |
| mainDisplay.digitalWrite(SONG1_LED,LOW); |
| mainDisplay.digitalWrite(SONG2_LED,LOW); |
| mainDisplay.digitalWrite(SONG3_LED,HIGH); |
| int size = sizeof(melody) / 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 / tempo[thisNote]; |
| |
| BUZZ(melodyPin, melody[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) { |
| mainDisplay.digitalWrite(LED_INDICATOR, 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(BUZZER, HIGH); // write the buzzer pin high to push out the diaphram |
| delayMicroseconds(delayValue); // wait for the calculated delay value |
| digitalWrite(BUZZER, LOW); // write the buzzer pin low to pull back the diaphram |
| delayMicroseconds(delayValue); // wait again or the calculated delay value |
| } |
| mainDisplay.digitalWrite(LED_INDICATOR, LOW); |
| |
| } |
| |
| |
| |
| |
Leave a Comment