ScoopGracie
Published © CC0

Anchors Aweigh

Play Anchors Away to commemorate Veteran's Day 2018. Go Navy!

BeginnerFull instructions provided1,866
Anchors Aweigh

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Any Arduino with a pin 13 should work; if there is no pin 13, you should be able to just change the pin in the code.
×1
Breadboard (generic)
Breadboard (generic)
Only needed if male-to-female wires are not available.
×1
Buzzer
Buzzer
An 8 ohm speaker should also work and may sound better.
×1
Jumper wires (generic)
Jumper wires (generic)
Male-to-male or male-to-female wires are needed; if male-to-female are used, the breadboard is not needed.
×2

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram

Schematic

Code

Anchors Aweigh

Arduino
/*
 * Based on the sketch found at https://gist.github.com/elubow/7844436.
 * The original sketch plays "Jingle Bells" repeatedly on pin 5.
 * This version plays "Anchors Aweigh" once on pin 13.
 * No licensing information was found for the original sketch. This version
 * (or at least the modifications, if the original is under any license) is
 * Public Domain.
 * 
 * Notes:
 *  Changed song to "Anchors Aweigh"
 *  Added line to prevent repeated playing
 *  Changed beats[] from int to float
 *  Added notes :
 *    D above high C* : D
 *    E above high C* : E
 *    F Sharp         : F
 --------------
      * Note: This is high C, not "high seas." Get it? Navy?
 */
int speakerPin = 13;
int length = 51;
char notes[] = "cegaeaCDgCaCagabCFadCbgfdcegaeaCDgCaCagabcEgFgDgfgC";
float beats[] = { 2, 1, 1,   1.5, 0.5, 2,  2, 1, 1,   4,  2, 1, 1,   1, 1, 1, 1,  1, 1, 1, 1,  1, 1, 1, 1,  2, 1, 1,    1.5, 0.5, 2,    2, 1, 1,   4,  2, 1, 1,   1, 1, 1, 1, .75, .25, .5, .125, .75, .25, .5, .5,  4};

int tempo = 300;
void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}
void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956, 901, 849, 1390 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 11; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}
void setup() {
  pinMode(speakerPin, OUTPUT);
}
void loop() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }

    // pause between notes
    delay(tempo / 2);
  }
  while (true) {}
}

Credits

ScoopGracie

ScoopGracie

1 project • 0 followers

Comments