int timer = 100; // The higher the number, the slower the timing. //for the piezo int speakerPin = 5; int breadboardPin = 4; int length = 3; // the number of notes char notes[] = "ccg"; // a space represents a rest int beats[] = { 1, 1, 1 }; int tempo = 150; //piezo int end //piezo code void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e' }; int tones[] = { 2600, 700, 519 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } //end piezo void setup() { // use a for loop to initialize each pin as an output: for (int thisPin = 7; thisPin < 14; thisPin++) { pinMode(thisPin, OUTPUT); pinMode(speakerPin, OUTPUT); //piezo pinMode(4, OUTPUT); } } void loop() { digitalWrite(breadboardPin, HIGH); //stuff on small breadboard // loop from the lowest pin to the highest: for (int thisPin = 7; thisPin < 14; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } // loop from the highest pin to the lowest: for (int thisPin = 13; thisPin >= 7; thisPin--) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } for (int i = 0; i < length; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } digitalWrite(breadboardPin, LOW); delay(200); }