Making Music on the Arduino Project

Replacing the speaker circuit

Replacing the speaker circuit


               Music is a collection of notes, so if we want to make music, then we can assemble alunannya tunes so catchy. In Arduino we could use the tone function () to create a tone. Tone function () has two input parameters required and one additional parameter. How to use the tone function (), namely:

tone (pin, frequency, duration); or tone (pin, frequency);


               Parameter pin is a pin that is connected to the speakers, the frequency is the frequency used, while the duration is long tone sounds at these frequencies.

               If no input duration, then the tone will be sounded until the next pitch is executed or when we give orders noTone (). So that we can take advantage of the delay to create a tone that is long or short.

               Duration parameter will be useful when we want to sound a tone while running other commands. because if we use a delay, then we have to wait for delay is completed in advance to run the next command.

               NoTone command () allows you to stop the tone on a specific pin, so that we can use the pin with format

noTone (pin);


               NoTone command () will be useful when we use a lot of speakers are controlled by pins. Just a note that when we execute the tone function (), then we can not use the PWM function on pin 3 and pin 11. Therefore, if you want to use PWM and tone function (), you should use another pin for PWM.

               Program on the Sketch below serves to make the scales
               Do-Re-Mi key C. Please try


// Free Tutorial Arduino
// www.ioisalman.com  

// Scales C
#define NOTE_C4 262  // DO
#define NOTE_D4 294  // RE
#define NOTE_E4 330  // MI
#define NOTE_F4 349  // FA
#define NOTE_G4 392  // SOL
#define NOTE_A4 440  // LA
#define NOTE_B4 494  // SI
#define NOTE_C5 523  // DO

// speaker ada di pin 9
const int pinSpeaker = 9;

void setup() {
  pinMode(pinSpeaker, OUTPUT);
}

void loop() {
  tone(pinSpeaker, NOTE_C4, 500);
  delay(500);
  tone(pinSpeaker, NOTE_D4, 500);
  delay(500);
  tone(pinSpeaker, NOTE_E4, 500);
  delay(500);
  tone(pinSpeaker, NOTE_F4, 500);
  delay(500);
  tone(pinSpeaker, NOTE_G4, 500);
  delay(500);
  tone(pinSpeaker, NOTE_A4, 500);
  delay(500);
  tone(pinSpeaker, NOTE_B4, 500);
  delay(500);
  tone(pinSpeaker, NOTE_C5, 500);
  delay(500);
  
  noTone(pinSpeaker);
  delay(1000);
}


               In Sketch Above, we have practiced tone function () and noTone (). In the beginning there was a part #define program that serves to replace these variables with the value of the target. For example, the variable NOTE_C4 means it is number 262. The list is a list of ringing tones standards

Subscribe to receive free email updates:

0 Response to "Making Music on the Arduino Project"

Post a Comment