User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Lab 9: Musical Instrument

Submitted by Laura Paajanen on Wed, 11/12/2008 - 21:13

Assignment: Synthesis: Music Instrument (group work)

Collaborators:

Assignment: Synthesis: Music Instrument (group work)
Collaborators: RyanKaufman, xiaomeng

Description

Our musical instrument is a rotating set of arms that tap glasses of water to make sound. Each arm has a servo at the end that can be independently operated by hitting a button, and there is also a servo in the center to rotate the set of arms and change which chord of three glasses will be hit. That is activated by turning a wheel.

Components Used

  • Arduino Board
  • 4 Servo motors
  • Lots of wire
  • Breadboard
  • USB cable
  • Laptop running Arduino
  • Potentiometer
  • Cardboard, electrical tape, plastic building set parts, glasses of water

 

 

 

Our instrument

The glasses around the rotating servo arms

Our buttons, mase of floam

Turning the wheel changes the glasses that are hit.

 

Arduino Code

int servoPin = 9;         // Control pin for servo motor
int servoPin1 = 10;
int servoPin2 = 11;

int sensorPin = 0; // select the input pin for the force sensor
int sensorPin1 = 1;
int sensorPin2 = 2;

int pulseWidth = 0; // Amount to pulse the servo
int pulseWidth1 = 0;
int pulseWidth2 = 0;

// setup for the center section
int servoPins = 5; // Control pin for servo motor
int potPins = 3; // select the input pin for the potentiometer

int pulseWidths = 0; // Amount to pulse the servo
long lastPulses = 0; // the time in millisecs of the last pulse
int refreshTimes = 50; // the time in millisecs needed in between pulses
int vals; // variable used to store data from potentiometer
int minPulses = 500; // minimum pulse width


long lastPulse = 0; // the time in millisecs of the last pulse
long lastPulse1 = 0;
long lastPulse2 = 0;
int refreshTime = 20; // the time in millisecs needed in between pulses
int minPulse = 500; // minimum pulse width
int minPulse1 = 500;
int minPulse2 = 500;
int maxPulse = 2250; // maximum pulse width
int maxPulse1 = 2250;
int maxPulse2 = 2250;

int valfsr = 0; // variable to store the value coming from the sensor
int valfsr1 = 0;
int valfsr2 = 0;

int val = 0;
int val1 = 0;
int val2 = 0;

int ledPin = 2; // select the output pin for the LED
int ledPin1 = 3;
int ledPin2 = 4;

void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pinMode(servoPin1, OUTPUT);
pinMode(servoPin2, OUTPUT);
pulseWidth = minPulse; // Set the motor position to the minimum
pulseWidth1 = minPulse1;
pulseWidth2 = minPulse2;

Serial.begin(9600);


// Center section below
pinMode(servoPins, OUTPUT); // Set servo pin as an output pin
pulseWidths = minPulses; // Set the motor position to the minimum
Serial.begin(9600); // connect to the serial port
// Serial.println("servo_serial_better ready");

}

void loop() {
valfsr = analogRead(sensorPin); // read the value from the sensor, 0-1023
valfsr1 = analogRead(sensorPin1);
valfsr2 = analogRead(sensorPin2);

delay(50); // rest a little...

if (valfsr >= 50) { // if input, move servo
val = 4;
digitalWrite(ledPin, HIGH); // sets the LED on
}

if (valfsr1 >= 50) { // if input, move servo
val1 = 4;
digitalWrite(ledPin1, HIGH); // sets the LED on
}

if (valfsr2 >= 50) { // if input, move servo
val2 = 4;
digitalWrite(ledPin2, HIGH); // sets the LED on
}
// Serial.println(val2);

if (val >= 1 && val <= 9 ) {
val = val - 1; // make val go from 0-8
pulseWidth = (val * (maxPulse-minPulse) / 8) + minPulse; // convert val to microseconds
Serial.println(pulseWidth,DEC);
}

if (val1 >= 1 && val1 <= 9 ) {
val1 = val1 - 1; // make val go from 0-8
pulseWidth1 = (val1 * (maxPulse1-minPulse1) / 8) + minPulse1; // convert val to microseconds
Serial.println(pulseWidth1,DEC);
}

if (val2 >= 1 && val2 <= 9 ) {
val2 = val2 - 1; // make val go from 0-8
pulseWidth2 = (val2 * (maxPulse2-minPulse2) / 8) + minPulse2; // convert val to microseconds
Serial.println(pulseWidth2,DEC);
}

updateServo(); // update servo position
updateServo1();
updateServo2();
digitalWrite(ledPin, LOW); // sets the LED off
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);

// center section
vals = analogRead(potPins); // read the value from the sensor, between 0 - 1024

if (vals > 0 && vals <= 999 ) {
pulseWidths = vals*2 + minPulses; // convert angle to microseconds

// Serial.print("moving servo to ");
// Serial.println(pulseWidth,DEC);

}
updateServos(); // update servo position


}

void updateServo() {
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}

void updateServo1() {
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse1 >= refreshTime) {
digitalWrite(servoPin1, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth1); // Length of the pulse sets the motor position
digitalWrite(servoPin1, LOW); // Turn the motor off
lastPulse1 = millis(); // save the time of the last pulse
}
}

void updateServo2() {
// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse2 >= refreshTime) {
digitalWrite(servoPin2, HIGH); // Turn the motor on
delayMicroseconds(pulseWidth2); // Length of the pulse sets the motor position
digitalWrite(servoPin2, LOW); // Turn the motor off
lastPulse2 = millis(); // save the time of the last pulse
}
}


void updateServos() {
// pulse the servo again if the refresh time (20 ms) has passed:
if (millis() - lastPulses >= refreshTimes) {
digitalWrite(servoPins, HIGH); // Turn the motor on
delayMicroseconds(pulseWidths); // Length of the pulse sets the motor position
digitalWrite(servoPins, LOW); // Turn the motor off
lastPulses = millis(); // save the time of the last pulse
}
}