Monday, September 27, 2010

Sound Project







For my project I decided to make a device that would increase and decrease the tempo of a looped song. I did this by, essentially, combining the analog input example and the tone music example from arduino.cc. The analog input program made it so that I could control how fast a light flashed with a nob, while the tone music program played a song one time. The wiring and layout I did for my project was the same as in both examples but the code was very different. Essentially, what I did was I altered the music program so that the song would play over and over again, I then add the code from the analog input program so that it would be able to alter the tempo of the song each time it played. If you would like to try what I did I'll be posting up the code and some more precise directions in a later post.

Analog Input
Tone Music

1 comment:

  1. /*
    Melody

    Plays a melody

    circuit:
    * 8-ohm speaker on digital pin 8

    created 21 Jan 2010
    by Tom Igoe

    http://arduino.cc/en/Tutorial/Tone

    */
    #include "pitches.h"

    int sensorPin = 0; // select the input pin for the potentiometer
    int ledPin = 11; // select the pin for the LED
    int sensorValue = 0; // variable to store the value coming from the sensor

    // 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() {
    // declare the ledPin as an OUTPUT:
    pinMode(ledPin, OUTPUT);

    // iterate over the notes of the melody:

    }

    void loop() {
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    // turn the ledPin on
    //digitalWrite(ledPin, HIGH);
    // stop the program for milliseconds:
    //delay(sensorValue);
    // turn the ledPin off:
    //digitalWrite(ledPin, LOW);
    // stop the program for for milliseconds:
    delay(sensorValue);

    for (int thisNote = 0; thisNote < 8; 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 = sensorValue/noteDurations[thisNote];
    tone(11, 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);
    }
    }

    ReplyDelete