Lab 2 – Team Chewie (Chewbacca)

Team Members
Stephen Cognetta (cognetta@)
Eugene Lee (eugenel@)
Jean Choi (jeanchoi@)
Karena Cai (kcai@)

Group Number: 14

Description
Our musical instrument was a head-banging music maker. The accelerometer, which is attached to a hat, detects when the user has done a sudden motion (like head banging). When this motion is detected, the tempo of the song that is playing will match the rate at which you are banging your head. By varying the location of touch along a linear sensor, the pitch of the song can also be changed. We thought that our project was a success because we were able to become familiar with the accelerometer while also making a cool project that created music with the motion of the user’s body. As an extension of this project, we could somehow attach the accelerometer to someone’s feet so that when they exercised, the beat of the music would match the rate at which they were running. We also thought that our design could be improved so it was less visible and less bulky.

PROTOTYPE 1 : Ambient Music Generator

This instrument by default emits an ambient noise. When a sudden jerk in the accelerometer is detected, it will emit a dolphin noise. Such a device could be attached to children’s toys that make playing with toys even more fun!
Used CHuck and Processing.
PROTOTYPE 2 : Two-Dimensional Music Player

This instrument uses a two-dimensional sensing technique to change both the pitch and the tempo of a song. If force is applied to the FSR, the tempo decreases. Meanwhile, by sliding the FSR along the soft potentiometer (linear sensor), the pitch will vary.

Head-Banging Music Maker. Final project for this lab. We created a hat that would respond to the movements of the head, if the head jerks fast, the music would play faster, and vice versa. It won’t play at all if the head isn’t moved.

List of Parts
– Arduino Uno
– jumper wires
– accelerometer
– Buzzer
– Hat

Instructions on how to recreate the Head-Banging Music Maker
We attach the buzzer and accelerometer to the device as shown in the picture below. (basically, the accelerometer is wired in as was done in Step 5 of the lab, using the AREF pin, and the buzzer is hooked up to the arduino board). Then attach to the hat as shown. Most of the work is performed in the code, which is shown below.

ttt

hat

Head Bangin’ Source Code

/*
  Graph

 A simple example of communication from the Arduino board to the computer:
 the value of analog input 0 is sent out the serial port.  We call this "serial"
 communication because the connection appears to both the Arduino and the
 computer as a serial port, even though it may actually use
 a USB cable. Bytes are sent one after another (serially) from the Arduino
 to the computer.

 You can use the Arduino serial monitor to view the sent data, or it can
 be read by Processing, PD, Max/MSP, or any other program capable of reading 
 data from a serial port.  The Processing code below graphs the data received 
 so you can see the value of the analog input changing over time.

 The circuit:
 Any analog input sensor is attached to analog in pin 0.

 created 2006
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe and Scott Fitzgerald

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Graph
 */

// ******************************************************
// CHORD INITIALIZATION
// ******************************************************

const int C = 24,
          D = 27,
          E = 30,
          F = 32,
          G = 36,
          A = 40,
          B = 45,
          C2 = 48,
          H = 0;

int twinkleStar[] = { C, C, G, G, A, A, G, H,
                      F, F, E, E, D, D, C, H,
                      G, G, F, F, E, E, D, H,
                      G, G, F, F, E, E, D, H,
                      C, C, G, G, A, A, G, H,
                      F, F, E, E, D, D, C, H};
int songLength = 48;
int songStep = 0;

int singleNote[] = { 1, 1, 1, 1 };
int majorChord[] = { 4, 5, 6, 0 };
int minorChord[] = { 10, 12, 15, 0 };
int seventhChord[] = { 20, 25, 30, 36 };
int majorChordLength = 3;
int *chords[] = { singleNote, majorChord, minorChord, seventhChord };
const int chordsLength = 4;

int chordType = 0;                // changes between chords[]
int arpStep = 0;                  // changes between chord frequencies

// ******************************************************
// INPUT INITIALIZATION
 // ******************************************************
const int linPin = A1;
const int xPin = A5;
const int yPin = A4;
const int zPin = A3;
const int tonePin = 10;
const int groundpin = A2; // analog input pin 2
const int powerpin = A0; // analog input pin 0
const int proxPin = A6; // analog input pin 6

// ******************************************************
// MAIN LOOP
// ******************************************************

unsigned int tempo;
unsigned int frequency;
unsigned int chordFrequency;
int currentBang;
int bangInterval = 0;
int minimumBangInterval = 70;
int maximumBangInterval = 200;

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);

  pinMode(groundpin, OUTPUT);
 pinMode(powerpin, OUTPUT);
 digitalWrite(groundpin, LOW); 
 digitalWrite(powerpin, HIGH);
}

int prevX = 400;
boolean rising = false;
boolean falling = false;
int mini, maxi, diff;

void loop() {
  int linReading = analogRead(linPin);
  int xReading = analogRead(xPin);
  int yReading = analogRead(yPin);
  int zReading = analogRead(zPin);

  // send the value of analog input 0:
  //int potReading = analogRead(potPin);
 // int btnReading = digitalRead(btnPin);
  // send the value of analog input 0:

  // wait a bit for the analog-to-digital converter 
  // to stabilize after the last reading:
  delay(2);

  int x, y, z;
  x = xReading;
  y = yReading;
  z = zReading;
  if (prevX < x) {
    rising = true;
    falling = false;
  } else {
    rising = false;
    falling = true;
  }
  prevX = x;
  if (falling == true)    mini = x;
  if (rising == true)     maxi = x;
  diff = maxi - mini;

  /*Serial.print(x);
  Serial.print("\t");
  Serial.print(prevX);
  Serial.print("\t");
  Serial.print(mini);
  Serial.print("\t");
  Serial.print(maxi);
  Serial.print("\t");
  Serial.println(diff);*/

  int * chord = twinkleStar;

  //measure the time
  if (bangInterval < maximumBangInterval)     bangInterval++;   //when head bang is detected      if (diff > 80 && bangInterval > minimumBangInterval) {
    currentBang = bangInterval;
    bangInterval = 0;

  //  Serial.print(tempo);
  //  Serial.print(",");
    Serial.println(chordFrequency);

    unsigned int tempo = currentBang*2;
    unsigned int duration = tempo - tempo / 20;

    float chordFactor = (float)chord[songStep] / (float)chord[0];
    if (linReading < 1020)
      frequency = 500;
    chordFrequency = frequency * chordFactor;
    tone(tonePin, chordFrequency, duration);

     Serial.print(songStep);
    Serial.print(" ");
    Serial.print(chordFrequency);
    Serial.print(" ");
    Serial.print(chordFactor);
    Serial.print(" ");
    Serial.println(currentBang);

    delay(tempo);
    songStep = songStep < songLength - 1 ? songStep + 1 : 0;

     chordFactor = (float)chord[songStep] / (float)chord[0];
    /*if (linReading < 1020)
      frequency = linReading;*/
      frequency = 500;
    chordFrequency = frequency * chordFactor;
    tone(tonePin, chordFrequency, duration);

    Serial.print(songStep);
    Serial.print(" ");
    Serial.print(chordFrequency);
    Serial.print(" ");
    Serial.print(chordFactor);
    Serial.print(" ");
    Serial.println(currentBang);

    songStep = songStep < songLength - 1 ? songStep + 1 : 0;
  }

}