The brick consists of a high sensitivity microphone and onboard Low Voltage Audio Power Amplifier. These measure the amplitude of sound in the environment. This data can then be used to start the effects on the screen, so in my case the storm starting on the screen.
The code for this would be very simple, it would just be a matter of telling the Arduino sensor that when the audio level reaches a certain level (eg 10DB so that normal ambient sounds do not effect the game) the animation starts.
I used similar code in my work for idat209 (see my post on light sensors).
The cost for the device would be around £20 and it would come with the necessary code to do it. Sadly it would take over a week to come as it is from Japan.
I have written up a code for a device to display sound levels with Arduino but not to do anything with them, this would be the part I'd need with the device.
The code would be similar to this...
/* ****************************************************
Arduino Code
**************************************************** */
int analogValue;
int val;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read analog input
analogValue = analogRead(0);
// send values to processing
Serial.println(analogValue, DEC);
// pause for 10 milliseconds:
delay(10);
}
/* ****************************************************
Processing Code
**************************************************** */
// import the Processing serial library
import processing.serial.*;
// declare a font variable
PFont font48;
int linefeed = 10;
// Linefeed in ASCII
Serial myPort;
// The serial port
// value recieved from the serialport / arduino
int sensorValue;
// mapped value
float sensorValueMap;
// – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – Setup
void setup() {
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(linefeed);
size (800, 600);
background (0);
//smooth();
//load font;
font48 = loadFont(“alask_48.vlw”);
textFont(font48);
}
// – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – Serial Event
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(linefeed);
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
//println(myString);
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ‘,’));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
//print(“Sensor ” + sensorNum + “: ” + sensors[sensorNum] + “\n”);
// sensor
sensorValue = sensors[0];
//sensorValueSmooth = sensors[1];
}
}
}
// – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – Draw a DB level viewer
void draw() {
// set the black background
background(0);
// run the displayText() function
displayText();
// map the received values
sensorValueMap = map(sensorValue, 0, 1024, 0, 800);
// draw a rectangle based on the variable sensorValueMap
rect (0, 100, width, sensorValueMap);
}
// – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – Display Text
void displayText() {
text(“Sensor Value”, 20, 80);
text(sensorValue, 450, 80);
}
// – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – - – Save image
void keyPressed(){
if(key==’s’)
saveFrame(“sound-######.png”);
}
//------------ end of code
No comments:
Post a Comment