Arduino + Star Wars = Imperial March 8-bit version

OutOfMemory256
245 views

Arduino + Star Wars = Imperial March 8-bit version

In this project we will learn to build a circuit, in which we can reproduce using a small speaker or buzzer plus a scale of musical notes, that reproduce the famous melody of the "Imperial March", and to improve our presentation more we will incorporate some LEDs that will flash to the beat of the music, from the movie Star War.

Objective:

To achieve a scale of musical notes through a buzzer / speaker.

Step #1: Bills of Material

As we can see, we do not need much to get this tutorial, so let's start.

Products:

Product Name SKU Price Qty
Arduino Uno R3 SMD DEV-11224 NOK255.20 1
Speaker - 0.5W 8Ohm COM-09151 NOK24.00 1
SparkFun Solder-able Breadboard - Mini PRT-12702 NOK33.60 1
Resistor Kit - 1/4W (500 total) COM-10969 NOK71.20 1
LED - Basic 5mm COM-11372 NOK7.20 1
Jumper Wires Premium M/M 20 AWG - 15.5 cm (Pack of 10) PRT-11709 NOK47.20 1
Jumper Wires Premium F/F 20 AWG - 15.5 cm (Pack of 10) PRT-11710 NOK47.20 1

Step #2: Circuit Assembly

The main image is quite descriptive but we will clarify the basic connections.
We will make a connection to the leg on the left of the buzzer that will link to the GND PIN of our Arduino UNO.

For the leg on the right, we will make a connection with the PIN 9 of our controller.

Step #3: Upload code to Arduino Uno

const int c = 261;
const int d = 294;
const int e = 329;
const int f = 349;
const int g = 391;
const int gS = 415;
const int a = 440;
const int aS = 455;
const int b = 466;
const int cH = 523;
const int cSH = 554;
const int dH = 587;
const int dSH = 622;
const int eH = 659;
const int fH = 698;
const int fSH = 740;
const int gH = 784;
const int gSH = 830;
const int aH = 880;
 
const int buzzerPin = 9; // Digital Pin 9
const int ledPin1 = 4;  // Digital Pin 4
const int ledPin2 = 6;  // Digital Pin 6 Built In Led can Change it if you want
 
int counter = 0; 
 
void setup()
{
  //Setup pin modes
  pinMode(buzzerPin, OUTPUT); // Digital Pin 9
  pinMode(ledPin1, OUTPUT);  // Digital Pin 4
  pinMode(ledPin2, OUTPUT); // Digital Pin 6 Built In Led can Change it if you want
}
 
void loop()
{
 
  //Play first section
  firstSection();
 
  //Play second section
  secondSection();
 
  //Variant 1
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 350);  
  beep(a, 125);
  beep(cH, 500);
  beep(a, 375);  
  beep(cH, 125);
  beep(eH, 650);
 
  delay(500);
 
  //Repeat second section
  secondSection();
 
  //Variant 2
  beep(f, 250);  
  beep(gS, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 500);  
  beep(f, 375);  
  beep(cH, 125);
  beep(a, 650);  
 
  delay(650);
}
 
void beep(int note, int duration)
{
  //Play tone on buzzerPin
  tone(buzzerPin, note, duration);
 
  //Play different LED depending on value of 'counter'
  if(counter % 2 == 0)
  {
    digitalWrite(ledPin1, HIGH);
    delay(duration);
    digitalWrite(ledPin1, LOW);
  }else
  {
    digitalWrite(ledPin2, HIGH);
    delay(duration);
    digitalWrite(ledPin2, LOW);
  }
 
  //Stop tone on buzzerPin
  noTone(buzzerPin);
 
  delay(50);
 
  //Increment counter
  counter++;
}
 
void firstSection()
{
  beep(a, 500);
  beep(a, 500);    
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);  
  beep(a, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
 
  beep(eH, 500);
  beep(eH, 500);
  beep(eH, 500);  
  beep(fH, 350);
  beep(cH, 150);
  beep(gS, 500);
  beep(f, 350);
  beep(cH, 150);
  beep(a, 650);
 
  delay(500);
}
 
void secondSection()
{
  beep(aH, 500);
  beep(a, 300);
  beep(a, 150);
  beep(aH, 500);
  beep(gSH, 325);
  beep(gH, 175);
  beep(fSH, 125);
  beep(fH, 125);    
  beep(fSH, 250);
 
  delay(325);
 
  beep(aS, 250);
  beep(dSH, 500);
  beep(dH, 325);  
  beep(cSH, 175);  
  beep(cH, 125);  
  beep(b, 125);  
  beep(cH, 250);  
 
  delay(350);
}

 

Step #4: Taking it a step further

// We introduce the variable where our digital signal will go to the buzzer
int speakerPin = 9;
 // We define a variable with the number of tones that it will play
int numTones = 10;
int tones [] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
// mid C C # D D # E F F # G G # A
// Above shows the equivalences between frequencies and Natural scale notes, they are not all declared but they exist.
void setup ()
{
// We generate a loop that runs through our vector. This will be responsible for entering a certain frequency to the buzzer each time, as we have declared the tone vector.
  for (int i = 0; i <numTones; i ++)
  {
    tone (speakerPin, tones [i]);
    delay (500);
  }
  noTone (speakerPin);
}
void loop ()
{
}

Step #5: EXTRA: Chariots of fire

If you want to play around a bit further, here is another one

/ We introduce the variable where our digital signal will go to the buzzer

int speakerPin = 9;
 // We define a variable with the number of tones that it will play
int numTones = 10;
int song [] = {261, 349, 392, 440, 392, 330, -10, 261, 349, 392, 440, 392, -10, -10, 261, 349, 392, 440, 392, 330, - 10, 330, 349, 330, 261, 261};
// int tones [] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};
// mid C C # D D # E F F # G G # A
// Above shows the equivalences between frequencies and Natural scale notes, they are not all declared but they exist. I use a frequency of -10 to generate a barely audible sound that replaces the silence that should sound in the melody. I invite you to share a better solution to avoid the buzzer sounding in that position.
void setup ()
{
// We generate a loop that runs through our vector. This will be responsible for entering a certain frequency to the buzzer each time, as we have declared the tone vector.
  for (int i = 0; i <numTones; i ++)
  {
    tone (speakerPin, tones [i]);
    delay (500);
  }
  noTone (speakerPin);
}
void loop ()
{
}