Lab 5: Embarrassingly Obscure Video Game Song Tribute

Submitted by m-craig on Tue, 03/05/2013 - 12:12

This lab is as an homage to Cid Highwind--a pilot character from the RPG Final Fantasy VII (1997) who is probably best remembered for the fact that he swears way more than any videogame character needs to.

A Piezo Speaker plays the first two bars of the melody of Cid's theme song from the game (original here: http://www.youtube.com/watch?v=yGxnlQ3pgok, composed by Nobuo Uematsu) in a continuous loop, and a potentiometer can be used to vary the tempo of the melody.  Output from the serial port is sent to Processing, which displays a picture of Cid each time the melody plays: the faster the song plays, the smaller Cid's picture gets; the slower the song, the larger the picture.  Playing the song at its correct tempo (around 250 in Arduino) displays the picture at roughly its original size.

 

Components Used:

1 - Arduino UNO
1 - Solderless Breadboard
1 - USB Power Supply
1 - Piezo Speaker
1 - Potentiometer
1 - JPEG Image of Cid Highwind (culled from Google Images)

 

Arduino Code:

// Cid Highwind's Theme - Variable Tempo (original melody by Nobuo Uematsu)

int speakerPin = 7;
int potPin = A0;

int length = 22; // the number of notes
char notes[] = "fCbC CDECg gDCD DEFDa "; // melody of Cid's theme; a space
                                                                             represents a rest
int beats[] = { 6, 3, 3, 4, 1, 3, 3, 5, 1, 5, 2, 6, 3, 3, 4, 1, 3, 3, 5, 1, 5, 3 };  // number
                                                                                          of beats for each note
int potVal = 0;

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', 'G', 'A', 'B'};  
  int tones[] = { 3830, 3400, 3038, 2864, 2550, 2272, 2028, 1915, 1700, 1519, 1430, 1275, 1136, 1014};

  // play the tone corresponding to the note name
  for (int i = 0; i < 14; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(speakerPin, OUTPUT);
}

void loop() {    // plays the melody, setting the tempo to potVal
  Serial.println(potVal);
  potVal = analogRead(potPin);
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * potVal / 12); // rest
    } else {
      playNote(notes[i], beats[i] * potVal);
    }

    // pause between notes
    delay(potVal / 24);
  }
}

 

Processing Code:

PImage cid;
import processing.serial.*;
String portname = "COM3";
Serial port;

float x = 0;

void setup() {
  size(550,800);
  background(255,255,255);
  cid = loadImage("cidhighwind.jpg");
  port = new Serial(this,portname,9600);
  port.bufferUntil('\n');
}

void draw() {
    image(cid,100,75,x*2,x*2.666);
}

void serialEvent(Serial port) {
  x = float(port.readStringUntil('\n'));
  background(255,255,255);
}

cid2.jpg
cidhighwind.jpg
lab5.JPG
0
Your rating: None
Drupal theme by Kiwi Themes.