In the following links you can find the second part of the Paper Electronics Session
In the following CODE you can find song “Jingle Bell” melody
/* Melody Plays the “Jingle Bell” melody and blink and LED to the melody */ --> #include "pitches.h" #define LOUDSPEAKER 0 #define LED 1 // notes for "Jingle Bell" melody 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 }; void setup() { pinMode(LOUDSPEAKER, OUTPUT); pinMode(LED, OUTPUT); playTone(); } void loop() { // no need to repeat the melody. } void playTone(){ 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]; playNote(LOUDSPEAKER, melody[thisNote], noteDuration); playNote(LED, 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: playNote(LOUDSPEAKER, 0, noteDuration); playNote(LED, 0, noteDuration); */ } } void playNote(int targetPin, long frequency, long length){ 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 } }
Open the > Sketch > Examples > tone Melody
Erase the part of the first sketch and past the skectch that is in the following part
verify the right configuration from the Attiny85 under the menu tools and double check the connections
Run the sketch using the option "uploading using Programmer"