So I cleaned up and modified the melody example sketch to play my own melody. Its going fine and sounds pretty damn cool. I've tried to add a "visualizer" led for each note that stays on for the entire note and is off in the time between the note. The furthest I could get was having the led blink once at the beggining of the note. Any tips?
Note: I've very easily done it by simply hooking the led up in series with the peizo, but i want to do it using a seperatae pin (i.e. on the software side of things.
heres the code
/*A simple program that plays Zelda'a Lulluby using a peizo speaker. Also has adjustable volume and speed using potentiometers. Pitch modification is also in the works.
HOOKUP: Pot 1 lead A hooked up to digital pin 8. Sweeper to positive of peizo. Lead B to ground. Peizo negative to ground. Pot 2 lead A hooked up to 5V. Sweeper to analog 0. Lead B to ground. */
#include "pitches.h"; //Include the file pitches.h which assigns specific frequencies to notes the peizo should play
int melody[] = { //Define the melody as being the notes following using those defined in pitches.h
NOTE_E4, NOTE_G4, NOTE_D4, NOTE_C4, NOTE_D4,
NOTE_E4, NOTE_G4, NOTE_D4, 0, NOTE_E4,
NOTE_G4, NOTE_D5, NOTE_C5, NOTE_G4, NOTE_F4,
NOTE_E4, NOTE_D4, 0, NOTE_E4, NOTE_G4,
NOTE_D4, NOTE_C4, NOTE_D4, NOTE_E4, NOTE_G4,
NOTE_D4, 0, NOTE_E4, NOTE_G4, NOTE_D5,
NOTE_C5, NOTE_G4};
int noteDurations[] = { //Define the note durations, 1 to 1 with melody 1 = 8 beats
2,4,2,8,8, // 2 = 4 beats (whole note)
2,4,2,4,2, // 4 = 2 beats (half note)
4,2,4,2,8, // 8 = 1 beats (quarter note)
8,2,4,2,4,
2,8,8,2,4,
2,4,2,4,2,
4,1 };
int sensorPin = 0; //Define the analog (pot.) input pin
int sensorValue = 2000; //Establish the default sensor value
int ledPin = 13; //Define the pin the led is on
void setup()
{
pinMode(ledPin,OUTPUT); //make the led pin an output
}
void loop() {
for (int thisNote = 0; thisNote < 32; thisNote++) //Make the note to be played #0. For each note value below "32," do the below and increase the note number by 1
{
sensorValue = analogRead(sensorPin); //Read the sensor pin in analog (actual value vs 1 or 0) and make the sensor value equal to that reading
int noteDuration = sensorValue*3/noteDurations[thisNote]; //Make the duration of each note (in milliseconds) twice the sensor value divided by the note durations previously established
digitalWrite(ledPin, HIGH); //Make the digital pin that the led is on HIGH, or 5V (basically: turn the led on)
tone(8, melody[thisNote],noteDuration); //Play a tone with certain pararmeters: from melody, use note #X (accoring to the first line) with the previously calculated note duration
digitalWrite(ledPin, LOW); //Make the digital pin that the led is on LOW, or 0V (basically: turn the led off)
int pauseBetweenNotes = noteDuration * 1.30; //Make the pause between notes 1.3 times the duration of the previous note
delay(pauseBetweenNotes); //Wait the previously calculated pause between notes before starting the next cycle and playing the next note
}
}
the analog is for modifying speed, no issues with that