Music tempo control glove

Since my ultimate goal for this class is to make some interactive audio/music content, project one was good chance for me to learn about controling Midi file with Arduino.  This time I made a glove(which is not that fancy looking……) which can control the tempo of music by moving hands.  Basically, a Arduino with a ultra sonic sensor is attached to the glove, and it measures the distance between two hands and send the value to a  USB serial port.  At the same time, I am running a program called, “Max/MSP” which is playing Midi file and change the tempo according to the value it received from the USB serial port which is connected to Arduino.

To answer Rex’s question how I connect Arduino with Max, I used a header file called “SimpleMessageSystem” which I found in Arduino playground. (http://www.arduino.cc/playground/Code/SimpleMessageSystem)

In my program, Arduino is sending distance values to serial port com4, and Max is getting data values from com4 and translating them to appropriate values that can be applied to its patch.

As I mentioned in the class last Thursday, I was having a trouble with serial port connection.  Some times either Max or Ardino cannot recognize the serial port and gave me an error.   Thanks to professor, Garth Zegelin, who visited us last Thusday and gave me a clue that a serial port cannot be shared.  It was very simple that all I had to do was, after I upload program to Arduino, turn off the Arduino application and leave Arduino connected to serial port, and just run Max/MSP program alone so that only one program is using the serial port.

** Arduino Code **

/* Midi tempo control by using Ultrasound Sensor and Max/MSP

 *

 * Reads values from an ultrasound sensor

 * and send the values to the USB serialport.

 */

 

#include <SimpleMessageSystem.h>  // import header file

 

int ultraSoundSignal = 5; // Ultrasound signal pin

int val = 0;

int ultrasoundValue = 0;

int timecount = 0; // Echo counter

 

void setup() {

  Serial.begin(9600);  // Sets the baud rate to 9600

} // end of setup()

 

void loop() {

  timecount = 0;  // clock timing to evaluate distance

  val = 0;  // variable to store pulse value

  pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

 

 //Send low-high-low pulse to activate the trigger pulse of the sensor

 

  digitalWrite(ultraSoundSignal, LOW); // Send low pulse

  delayMicroseconds(2); // Wait for 2 microseconds

  digitalWrite(ultraSoundSignal, HIGH); // Send high pulse

  delayMicroseconds(5); // Wait for 5 microseconds

  digitalWrite(ultraSoundSignal, LOW); // Holdoff

 

 // Listening for echo pulse

 

  pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input

  val = digitalRead(ultraSoundSignal); // Append signal value to val

  while(val == LOW) { // Loop until pin reads a high value

    val = digitalRead(ultraSoundSignal);

  }

 

  while(val == HIGH) { // Loop until pin reads a low value

    val = digitalRead(ultraSoundSignal);

    timecount = timecount + 1;  // Count echo pulse time

  }

 

 // Writing out values to the serial port

 

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue

ultrasoundValue = ultrasoundValue / 5;  // scale down

 

Serial.println(ultrasoundValue);  // to monitor whether Arduino is getting signal or not.

 

delay(40); // delay the program

 

} // end of loop()

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s