Description:
In this assignment, I decided to go ahead with FSR. I first placed it inside the rubber duck and visualize the sound. But, once I put the FSR in, the gap was too wide for the sound. So, I decided to synthesize the sound made by the duck using the 'Minim' library (Google search win) for playing sounds. Thus, whenever the user applies pressure on the duck, it transmits the pressure onto the FSR which in turn provides for the input data to my code in Processing. My code then visualizes the pressure applied and also makes a 'quack' sound whenever the pressure applied is above certain units.
The code on the Arduino learning page for Processing helped a lot. Also, this code: http://code.compartmental.net/tools/minim/quickstart/ helped me a lot in getting started with sound outputs in Processing.
Components Used:
1 Arduino Uno Board
1 Bread Board
1 Force Sensitive Resistor
1 Rubber Duck
1 Sponge Base
1 10K Ohm resistor
Connecting Wires
Code:
/*
Author: Priya Iyer
TUI Assignment : FSR and Processing!
This code reads the pressure applied on the duck to make sounds and to visualizes the force applied on the Serial Monitor
Code inspired and modified from:
1. http://arduino.cc/en/Tutorial/Graph and
2. http://code.compartmental.net/tools/minim/quickstart/ (for sound)
*/
import processing.serial.*;
import ddf.minim.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
Minim minim;
AudioPlayer song;
void setup () {
// set the window size:
size(400, 300);
minim = new Minim(this);
song = minim.loadFile("quack.wav");
// Serial.list()[10] is the port that I'm using
myPort = new Serial(this, Serial.list()[10], 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void stop()
{
song.close();
minim.stop();
super.stop();
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
if (inByte>=10){
//play the quack sound only if enough pressure is applied
song.play();
}
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
//song.play();
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
- Login to post comments