Created
February 4, 2015 19:13
-
-
Save krobro/f85fa3236bd8457a5f08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SoundAndMusic | |
// | |
// Play music and blink the LED in synchronization | |
// | |
// We are using port S0 on the Duinobot board | |
// | |
// NOTES: | |
// - Ports S0-S5 are analog input pins | |
// - S0 is identical to Arduino A0 and so on (S5=A5) | |
// - To use these ports as Digital pins add 14 | |
// | |
#include "Arduino.h" | |
#include "pitches.h" | |
#define S0 0+14 | |
// notes in the melody: | |
int melody[] = { | |
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; | |
// note durations: 4 = quarter note, 8 = eighth note, etc.: | |
int noteDurations[] = { | |
4, 8, 8, 4,4,4,4,4 }; | |
void setup() { | |
pinMode(S0, OUTPUT); // sets the S0 port as output for the LED | |
pinMode(SPEAKER, OUTPUT); // setup the speaker | |
} | |
void loop() { | |
// iterate over the notes of the melody: | |
for (int thisNote = 0; thisNote < 8; thisNote++) { | |
// Sychronize the LED to light up when note G3 is played | |
if (melody[thisNote] == NOTE_G3) | |
{ | |
digitalWrite(S0, HIGH); | |
} | |
// 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/noteDurations[thisNote]; | |
toneDelay(SPEAKER, melody[thisNote],noteDuration); | |
// turn off the LED | |
digitalWrite(S0, LOW); | |
// to distinguish the notes, set a minimum time between them. | |
int pauseBetweenNotes = noteDuration * 0.8; | |
delay(pauseBetweenNotes); | |
} | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment