I’m gonna find them all :D

Seven nations army is a classic and that should never be questioned. It is also very repetitive which is helpful if you’re trying to make your Arduino board play it.

For this weeks assignment I created a knob steered Seven Nations Army player, where the users can go through the notes of the tune and play it themselves. I also introduced the breaks, where in-between the notes there are 0 notes, which emit no sounds. By doing that I gained manual control over the length of each note played. I also used a RED EMERGENCY BUTTON for when the speaker was getting to nerve wrecking. When the button is pressed no sounds is emitted 🙂

#include "pitches.h"

#define speaker 4
#define btn 3
#define knob 2

int whichNote = 0;
int knobValue;
int lastNote = 14;

int notes[15] = {0, NOTE_E2, 0, NOTE_E2, 0, NOTE_G2, 0, NOTE_E2, 0, NOTE_D2, 0, NOTE_C2, 0, NOTE_B1, 0};

void setup() {
  pinMode (btn, INPUT);
  Serial.begin (9600);
}

void loop() {
  if (digitalRead(btn) == LOW) {
    whichNote = map(analogRead(A0), 0, 1023, 0, 14);
    tone(4, notes[whichNote], 1000);
  }

  if (whichNote == lastNote) {
    int i, k, t;
    int n = sizeof(notes) / sizeof(notes[0]);
    for (i = 0; i < n / 2; i++) {
      t = notes[i];
      notes[i] = notes[n - i - 1];
      notes[n - i - 1] = t;
    }
    if (lastNote == 0) {
      lastNote = 14;
    }
    else {
      lastNote = 0;
    }
  }
}

 

Leave a Reply