Skip to content

Artwork

Advertisements
2 Comments leave one →
  1. February 10, 2011 3:25 pm

    Let’s see if this works. It’s an elaboration of the code I did to play “musical” tones on the piezo speaker.

    I wasn’t able to post or reply under “Posts/News.”

    // the multiplier between intervals in the equal tempered chromatic scale
    // of 12 halftone intervals is the 12th root of 2, or pow(2, 1/12) = 1.059463094359295
    // we probably only need floating point precision for values
    static float r = 1.059463094359295;
    // we’ll store a chromatic scale into an array
    float cs[12];
    // a value used by playtone to modify the timbre of a square wave
    float dc = 0.5;

    void setup() {
    Serial.begin(19200); // 19200 baud
    pinMode(13, OUTPUT); // set pin 13 to output (LCD)
    pinMode(12, OUTPUT); // set pin 12 to output (piezo speaker)
    initScale();
    }

    // intialize an array to contain frequency values of a chromatic scale from A440 to A880
    void initScale() {
    float reftone = 440;
    for (int i = 0; i < 12; i++) {
    cs[i] = reftone;
    reftone *= r;
    }
    }

    void loop() {
    // flash pin 13 one sec on, then off
    flash(13, 1000, 0);
    // microseconds of bandwidth for A440 reference tone: 1000000/440 = 2272.7
    float bw = 2272.7;
    // cycles of bw that fit in 1/4 second: (1000000 * 0.25)/2272.7 = 110
    int time = 110;
    // descending chromatic scale
    for (int i = 0; i < 12; i++) {
    buzz(time, bw, 0.5);
    bw *= r;
    time = (250000/bw);
    }
    flash(13, 250, 0);
    // ascending scale from array cs
    for (int i = 0; i < 12; i++) {
    bw = 1000000/cs[i];
    time = (250000/bw);
    buzz(time, bw, 0.25);
    }
    flash(13, 250, 0);
    nightInTunisia();
    Serial.println("fnord…");
    }

    void flash(int pin, int on, int off) {
    digitalWrite(pin, HIGH);
    delay(on);
    digitalWrite(pin, LOW);
    delay(off);
    }

    // parameters: duration in microseconds, bandwidth in microseconds
    // dutycycle is a value from 0..1 that measures the portion of the cycle
    // when the square wave is high
    void buzz(float time, float bw, float dutycycle) {
    for(int i = 0; i < time; i++) {
    digitalWrite(12, HIGH);
    delayMicroseconds(bw * dutycycle);
    digitalWrite(12, LOW);
    delayMicroseconds(bw * (1 – dutycycle));
    }
    }

    // parameters: frequency in Hz, duration in milliseconds
    void playtone(float freq, float ms) {
    // calculate bandwith, the length of one cycle of the square wave in microseconds
    float bw = 1000000/freq;
    // calculate duration in microseconds
    float time = (ms/bw) * 1000;
    buzz(time, bw, dc);
    }

    void nightInTunisia() {
    // duration of a quarter note in milliseconds
    float crotchet = 500;
    playtone(cs[0], crotchet * 0.5);
    playtone(cs[1], crotchet * 0.1666667);
    playtone(cs[4], crotchet * 0.1666667);
    playtone(cs[8], crotchet * 0.1666667);
    playtone(cs[3] * 2, crotchet * 2); // frequency * 2 for next octave
    playtone(cs[1] * 2, crotchet * 0.5);
    playtone(cs[8], crotchet * 0.5);
    playtone(cs[11], crotchet * 0.5);
    playtone(cs[0] * 2, crotchet * 2.5);
    delay(crotchet * 0.5); // eighth note rest
    dc = 0.25;
    playtone(cs[0], crotchet * 0.5);
    playtone(cs[1], crotchet * 0.1666667);
    playtone(cs[4], crotchet * 0.1666667);
    playtone(cs[8], crotchet * 0.1666667);
    playtone(cs[3] * 2, crotchet * 0.5);
    playtone(cs[3] * 2, crotchet * 1.5);
    playtone(cs[1] * 2, crotchet * 0.5);
    playtone(cs[8], crotchet * 0.5);
    playtone(cs[0] * 2, crotchet * 3.0);
    delay(crotchet * 0.5); // eighth note rest
    dc = 0.75;
    playtone(cs[0], crotchet * 0.5);
    playtone(cs[1], crotchet * 0.1666667);
    playtone(cs[4], crotchet * 0.1666667);
    playtone(cs[8], crotchet * 0.1666667);
    playtone(cs[3] * 2, crotchet * 2);
    playtone(cs[1] * 2, crotchet * 0.5);
    playtone(cs[8], crotchet * 0.5);
    playtone(cs[11], crotchet * 0.5);
    playtone(cs[0] * 2, crotchet * 2.5);
    delay(crotchet); // quarter note rest
    dc = 0.5;
    playtone(cs[0] * 2, crotchet * 0.5);
    playtone(cs[1] * 2, crotchet * 0.5);
    playtone(cs[0] * 2, crotchet * 0.083333);
    playtone(cs[1] * 2, crotchet * 0.083333);
    playtone(cs[0] * 2, crotchet * 0.083333);
    playtone(cs[10], crotchet * 0.5);
    playtone(cs[6], crotchet);
    playtone(cs[4], crotchet * 0.5);
    playtone(cs[5], crotchet * 2.5);
    delay(crotchet * 2);
    }

  2. February 10, 2011 3:38 pm

    Correction to previous post:

    playtone(cs[1], crotchet * 0.333333);
    playtone(cs[4], crotchet * 0.333333);
    playtone(cs[8], crotchet * 0.333333);

    instead of

    playtone(cs[1], crotchet * 0.1666667);
    playtone(cs[4], crotchet * 0.1666667);
    playtone(cs[8], crotchet * 0.1666667);

    in all three places where it occurs.

    My cat is after my Arduino. Already ran off with a mouthful of wires and components. The piezo is driving him nuts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: