Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

download Arduino   Piezo Speaker = Super Mario! _ the random bit.pdf

of 8

Transcript of Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    1/8

    the random bit

    linux, electro-coisas

    Arduino + Piezo Speaker = Super Mario!

    por Tiago Gala

    Some time ago I decided to put my Arduino into the music business! I had this piezoelectric buzzer lying around and it was time to give it a trial run so I googledaround and found this (hp://www.arduino.cc/en/Tutorial/PlayMelody) in the Arduino Forum and decided to give it a go, but the code on the website had a kind ofdirty look so I rewrote it bearing optimization and code-size in mind. Next follows the Friing layout, video and code:

    About these ads

    (hp://en.wordpress.com/about-these-ads/)

    Arduino + Piezo Speaker = Super Mario! | the random bit http://therandombit.wordpress.com/2011/11/21/arduino-piezo-speaker-super-mario/

    1 of 8 01-Oct-13 1:17 AM

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    2/8

    (hp://therandombit.files.wordpress.com/2010/12/speaker2_friing.png)

    Arduino + Piezo Speaker = Super Mario! | the random bit http://therandombit.wordpress.com/2011/11/21/arduino-piezo-speaker-super-mario/

    2 of 8 01-Oct-13 1:17 AM

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    3/8

    123456

    789101112131415

    1617181920212223242526272829303132

    333435363738394041

    42434445

    //Definio dos periodos para cada nota (*0.0000001)#define C 1911#define C1 1804#define D 1703#define Eb 1607#define E 1517

    #define F 1432#define F1 1352#define G 1276#define Ab 1204#define A 1136#define Bb 1073#define B 1012#define c 955#define c1 902

    #define d 851#define eb 803#define e 758#define f 716#define f1 676#define g 638#define ab 602#define a 568#define bb 536#define b 506

    #define p 0 //pausa

    int speaker = 6; //porta do arduinolong vel = 20000;

    void setup() {pinMode(speaker, OUTPUT);

    delay(2000);

    }

    int melod[] = {e, e, e, c, e, g, G, c, G, E, A, B, Bb, A, G, e, g, a, f, g, e, c, d, B, c};int ritmo[] = {6, 12, 12, 6, 12, 24, 24, 18, 18, 18, 12, 12, 6, 12, 8, 8, 8, 12, 6, 12, 12, 6, 6, 6, 12};

    void loop() { for (int i=0; i

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    4/8

    Basically whats being done is to output a HIGH signal half of the notes period (the inverse of frequency) and then a LOW for the same amount of time, thus creatinga square wave with the notes corresponding frequency (its called PWM Pulse Width Modulation, if the HIGH and LOW times are the same, its said that the

    duty-cycle is 50%), this HIGH/LOW cycle repeats until the time calculated for that note to be heard is over. At the end of each sound there is a gap of 1000 seconds (1milisecond) to make two notes distinguishable if they happen to have the same frequency. Note periods are defined in the anglo-saxonic (hp://en.wikipedia.org/wiki/Note) notation to simplify and standardize note entering. Note duration isnt related to the relative value of each note (as it would be expected in true musicalnotation: quarter = 4, a sixteenth = 16) but instead to the duration of the note in seconds according to the formula: time_seconds = value * note_span, so if value is 20000, a note with a note_span = 12 has a duration of 20 000 * 12 = 240 000 seconds.

    In the mean time I already transcribed a few kind of well known songs into this notation, to hear them you just need to swap its array name for the equivalent melod[]and ritmo[]:

    464748495051

    525354555657585960

    6162636465

    int tempo = ritmo[i];

    long tvalue = tempo * vel;

    tocar(tom, tvalue);

    delayMicroseconds(1000); //pausa entre notas!

    }

    delay(1000);

    }

    void tocar(int tom, long tempo_value) {

    long tempo_gasto = 0;

    while (tempo_gasto < tempo_value) {

    digitalWrite(speaker, HIGH);

    delayMicroseconds(tom / 2);

    digitalWrite(speaker, LOW);

    delayMicroseconds(tom/2);

    tempo_gasto += tom;

    }

    }

    Arduino + Piezo Speaker = Super Mario! | the random bit http://therandombit.wordpress.com/2011/11/21/arduino-piezo-speaker-super-mario/

    4 of 8 01-Oct-13 1:17 AM

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    5/8

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    6/8

    Arduino + Piezo Speaker = Super Mario! | the random bit http://therandombit.wordpress.com/2011/11/21/arduino-piezo-speaker-super-mario/

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    7/8

    from sketch_aug10a.cpp:29:D:\Arduino\arduino-1.0\hardware\arduino\cores\arduino/Printable.h:36: error: expected , or before numeric constant

    RESPOSTATiago Galago diz:

    10 de Agosto de 2012 s 13:25It looks like something might be wrong with your software, can you use/compile any of the examples offered by the Arduino IDE?

    RESPOSTABob the Gangster diz:12 de Junho de 2013 s 17:02

    This code worked for me.

    hp://pastebin.com/UhsHePFv

    RESPOSTAArend diz:14 de Agosto de 2012 s 22:36The name of the #defines are too short. This can cause strange problems which appear inconsequently.

    Rename the defines to something like tone_e, tone_b, tone_ab, etcetera; or work without the defines and replace the characters in the array by the correspondingvalue.

    It works for me! (Well, at least the compiler error disappeared, I couldnt check it if it worked at all since I killed my piezo speaker)

    RESPOSTABob the Gangster diz:12 de Junho de 2013 s 17:02Thanks, this was useful

    RESPOSTAArend diz:14 de Agosto de 2012 s 22:38I killed my last and only piezo speaker with my soldering iron, so dont worry about my code modification killing yours.

    RESPOSTAAtlas diz:29 de Maro de 2013 s 1:58

    add all the defines under setup section, then it will work fine

    RESPOSTA

    Arduino + Piezo Speaker Super Mario! | the random bit http://therandombit.wordpress.com/2011/11/21/arduino piezo speaker super mario/

    7 of 8 01-Oct-13 1:17 AM

    Arduino + Piezo Speaker = Super Mario! | the random bit http://therandombit.wordpress.com/2011/11/21/arduino-piezo-speaker-super-mario/

  • 7/27/2019 Arduino Piezo Speaker = Super Mario! _ the random bit.pdf

    8/8

    Atlas diz:29 de Maro de 2013 s 1:59I meant move, not add

    RESPOSTA

    Blog em WordPress.com. The Manifest Theme.

    p p p p p p p

    8 of 8 01-Oct-13 1:17 AM