Sunday, February 21, 2016

Musical Instrument

Three Note Piano

pictures:

above is the entire circuit (on the white cardboard) and user input area (hanging off the cardboard at the bottom)

closeup of the user input area

closeup of the circuit

This plays the notes C, D, and E. Songs like "Mary Had a Little Lamb" and "Hot Cross Buns" are possible.

Here I play "Mary Had a Little Lamb:"

Here is the source code of my program (note that I got the file pitches.h from this website: https://www.arduino.cc/en/Tutorial/toneMelody):
#include "pitches.h"
#define THRESHOLD 500

// left, center, right pins

int l_pin = 5;
int c_pin = 1;
int r_pin = 3;
int speaker_pin = 5;

void play(int note)

{
  tone(speaker_pin, note, 500);
}

void play_note(bool left, bool center, bool right)

{
  if (left)
    play(NOTE_C5);
  if (center)
    play(NOTE_D5);
  if (right)
    play(NOTE_E5);
}

void setup()

{
  Serial.begin(9600);
  pinMode(speaker_pin, OUTPUT);
}

void loop()
{
  int l_reading = analogRead(l_pin);
  int c_reading = analogRead(c_pin);
  int r_reading = analogRead(r_pin);
  
  bool l_on = l_reading > THRESHOLD;
  bool c_on = c_reading > THRESHOLD;
  bool r_on = r_reading > THRESHOLD;
  
  Serial.println((String)l_reading + " " + (String)c_reading + " " + (String)r_reading);

  play_note(l_on, c_on, r_on);
  
  delay(500);
}

Crawford's model of interaction:
input: user presses a force-sensitive sensor, computer receives input as an analog value from 0 to 1023
process: user decides what note to play next and determines which sensor to press next, computer translate the input value (from 0 to 1023) and which sensor was pressed to play the corresponding note (see source code for exact formula)
output: user listens, computer plays note via speaker

Improvements:
Originally I decided to make a trumpet, however after looking up how the key-presses are processed, there was an additional component of how air is blown into the trumpet that changes which note is played. Emulating this with the Arduino would require an additional piece (probably a potentiometer), but it'd be hard for the user to play the instrument easily. Instead I changed the design to be a three note piano.
I could add more notes (that requires more force-sensitive resistors and those don't come cheap, $7 at NYU computer store)
I could allow multiple notes to be played at once (like a real piano), this would require more speakers (and I would have to order these online probably, no idea on cost)
In terms of interactiveness, I could also use switches to act as input rather than force-sensitive resistors, this way playing the instrument would seem more "natural" to the user.

No comments:

Post a Comment