Force Sensor Musical Instrument and Waveform Generator

Posted by prayag

prayag's picture

In this project I have created a "Musical Instrument" that can be played by applying different force on the force sensor. Along with playing different sound, the desktop application displays the waveform of the music that is being played and the LED on the board also glows brighter and dimmer depending on the force applied. The force sensor is not very accurate (actually its very very inaccurate) so sometimes the  sound seems random but you can control some notes over a period of time.

H/W and S/W Components Used:

1. Force Sensitive Register2

2. LED

3. Wires & Register

4. Arduino Board

5. Processing Minim audio processing  library

The Processing code is given below:

import ddf.minim.*;
import processing.serial.*;
String portname = "COM3"; // or "COM5"
Serial port;
String buf="";
int count = 0;
AudioPlayer player;
Minim minim;

void setup()
{
  size(512, 200, P2D);

  minim = new Minim(this);
  
  // load a file, give the AudioPlayer buffers that are 1024 samples long
  // player = minim.loadFile("groove.mp3");
  
  // load a file, give the AudioPlayer buffers that are 2048 samples long
  player = minim.loadFile("Sa.wav", 2048);
  port = new Serial(this, portname, 9600); 
}

void draw()
{
  background(0);
  stroke(255);
  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  // note that if the file is MONO, left.get() and right.get() will return the same value
  for(int i = 0; i < player.left.size()-1; i++)
  {
    line(i, 50 + player.left.get(i)*50, i+1, 50 + player.left.get(i+1)*50);
    line(i, 150 + player.right.get(i)*50, i+1, 150 + player.right.get(i+1)*50);
  }
}

void serialEvent(Serial p) {
  int c = port.read();
  println("Read:"+c);
  //float mappedVal = map(c, 0, 1023, 0, 255);  
  //println("mappedVal:"+mappedVal);
  if(c < 15)
  {
       player = minim.loadFile("Sa.wav", 2048);
       player.play();
  }
  else if ( c > 15 && c < 30)
  {
       player = minim.loadFile("Re.wav", 2048);
       player.play();
  }
  
  else if ( c > 30 && c < 45)
  {
       player = minim.loadFile("Ga.wav", 2048);
       player.play();
  }
  
  else if ( c > 45 && c < 60)
  {
       player = minim.loadFile("Ma.wav", 2048);
       player.play();
  }
  
  else if ( c > 60 && c < 75)
  {
       player = minim.loadFile("Pa.wav", 2048);
       player.play();
  }
  
  else if ( c > 75 && c < 90)
  {
       player = minim.loadFile("Dha.wav", 2048);
       player.play();
  }
  
  else if ( c > 90 && c < 105)
  {
       player = minim.loadFile("Ni.wav", 2048);
       player.play();
  }
  
  else if ( c > 105)
  {
       player = minim.loadFile("Sa1.wav", 2048);
       player.play();
  }
  
}

void stop()
{
  // always close Minim audio classes when you are done with them
  player.close();
  minim.stop();
  
  super.stop();
}

The Arduino code is given below:

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);  
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);           

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);      
  Serial.print("\t output = ");      
  Serial.println(outputValue);   

  // wait 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);                     
}

Screenshot of the Processing window and a picture of the circuit is attached. Demo of the application http://www.youtube.com/watch?v=HoFO-SLjEcM 

 

Screen Shot with the Processing window showing the audio wave of the sound being played
Circuit Design
0
Your rating: None