Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Melody Player - Creating a Musical Arduino Project

Importance and Utility of the Melody Player

The Melody Player is a fascinating Arduino project that allows you to create and play melodies using a speaker or buzzer. It combines the fun of music with the versatility of Arduino programming, making it an excellent choice for hobbyists, students, and anyone interested in electronic music.

The Melody Player project offers several advantages and utilities. Firstly, it provides an opportunity to learn and practice coding in Arduino, enhancing your programming skills. Secondly, it allows you to explore the world of music and experiment with different melodies, tones, and rhythms. Lastly, it can be integrated into various projects such as interactive installations, automated musical instruments, or even as a component in a larger electronic system.

Project: Melody Player

The Melody Player project aims to create a simple yet functional device that can play melodies using an Arduino board and a speaker or buzzer. The objectives of this project include:

  1. Generating musical tones using Arduino's built-in tone() function.
  2. Storing and playing pre-defined melodies.
  3. Allowing user input to select and play different melodies.
  4. Providing an option to control the tempo or speed of the melodies.

To achieve these objectives, the project will utilize the Arduino board, a speaker or buzzer, and a few passive components such as resistors and capacitors.

List of Components:

  1. Arduino Uno (1x) - Link to purchase
  2. Speaker or Buzzer (1x) - Link to purchase
  3. Resistors (220 ohm, 1x; 10k ohm, 1x)
  4. Capacitors (10uF, 1x)

Examples:

Example 1: Playing a Simple Melody

#include <pitches.h>

// Melody notes in the format of note name followed by duration
int melody[] = {
  NOTE_C4, 4, NOTE_G3, 4, NOTE_G3, 4, NOTE_A3, 4, NOTE_G3, 2, 0, 4
};

// Note durations in milliseconds
int noteDurations[] = {
  200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200
};

void setup() {
  for (int i = 0; i < sizeof(melody) / sizeof(int); i += 2) {
    int noteDuration = 1000 / noteDurations[i / 2];
    tone(8, melody[i], noteDuration);
    delay(noteDuration * 1.3);
    noTone(8);
  }
}

void loop() {
  // Melody plays once in the setup() function, so no need for anything in loop()
}

Explanation: This example demonstrates how to play a simple melody using the tone() function. The melody is defined using note names and durations. The for loop iterates through the melody array, calculates the note duration, and plays each note using the tone() function. The delay() function is used to create pauses between notes, and the noTone() function is called to stop the sound.

Example 2: User Input Melody Selection

#include <pitches.h>

int buttonPin = 2;
int melodyIndex = 0;

int melody1[] = {NOTE_C4, 4, NOTE_D4, 4, NOTE_E4, 4, NOTE_F4, 4};
int melody2[] = {NOTE_G4, 4, NOTE_A4, 4, NOTE_B4, 4, NOTE_C5, 4};

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    melodyIndex++;
    if (melodyIndex > 1) {
      melodyIndex = 0;
    }
    playMelody();
    delay(500);
  }
}

void playMelody() {
  int* melody;
  int noteDuration = 200;

  if (melodyIndex == 0) {
    melody = melody1;
  } else if (melodyIndex == 1) {
    melody = melody2;
  }

  for (int i = 0; i < sizeof(melody) / sizeof(int); i += 2) {
    noteDuration = 1000 / melody[i + 1];
    tone(8, melody[i], noteDuration);
    delay(noteDuration * 1.3);
    noTone(8);
  }
}

Explanation: This example demonstrates how to select and play different melodies based on user input. The button connected to pin 2 is used to toggle between two melodies. The playMelody() function plays the selected melody, and the loop() function checks for button press and calls the playMelody() function accordingly.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.