Arduino - Tone Library

    0

    0

    Sohail Gulam

    unoarduino

    we will use the Arduino Tone Library. It is nothing but an Arduino Library, which produces square-wave of a specified frequency (and 50% duty cycle) on any Arduino pin. A duration can optionally be specified, otherwise the wave continues until the stop() function is called. The pin can be connected to a piezo buzzer or a speaker to play the tones.

    #include "pitches.h"
    
    // notes in the melody:
    int melody[] = {
    NOTE_C4, NOTE_G3,NOTE_G3, NOTE_GS3, 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() {
       // iterate over the notes of the melody:
       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 = 1000/noteDurations[thisNote];
          tone(8, melody[thisNote],noteDuration);
          //pause for the note's duration plus 30 ms:
          delay(noteDuration +30);
       }
    }
    void loop() {
       // no need to repeat the melody.
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.