実際、私たちは Arduino と Processing に基づくプロジェクトに取り組んでいます。基本的に説明すると、複数のノック センサー (またはピエゾ) があります。私たちの目標は、ピエゾが「ノック」されるたびにサウンド ファイルを起動することですが、ピエゾからデータを取得する際にいくつかの問題が発生しました。Arduino スケッチと Processing スケッチに取り組んでいますが、2 つのスケッチの間にパスを作成することができませんでした。だから私たちの質問は次のとおりです。処理スケッチで、arduinoスケッチのピエゾの値を取得し、ノックされるたびにサウンドファイルを起動する方法は?
Arduino スケッチ:
// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor0 = A0 ; // the piezo is connected to analog pin 0
const int knockSensor1 = A1 ;
const int knockSensor2 = A2 ;
const int knockSensor3 = A3 ;
const int knockSensor4 = A4 ;
const int knockSensor5 = A5 ;
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light
void setup()
{
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
Serial.begin(9600); // use the serial port
}
void loop()
{
Ampoule(A0);
Ampoule(A1);
Ampoule(A2);
Ampoule(A3);
Ampoule(A4);
Ampoule(A5);
}
void Ampoule (int input)
{
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(input);
// if the sensor reading is greater than the threshold:
if (sensorReading > threshold)
{
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("PiezoKnock!");
Serial.write(10);
}
else
{
ledState = LOW;
digitalWrite(ledPin, ledState);
}
delay(10); // delay to avoid overloading the serial port buffer
}
Processing Sketch (私たちが見つけたオープンソースの例で、理論的にはまさに私たちが探していたものを手に入れました)
/**
* Arduino Sounds
*
* Play WAV or MP3 files when piezo knocks from an Arduino running the
* "PiezoKnock" sketch or when a computer keyboard key is pressed.
*
* Taken from the Minim "trigger" sketch:
*
* This sketch demonstrates how to use the <code>trigger</code> method of an <code>AudioSample</code>. <br />
* <code>AudioSample</code>s can only be triggered, not cue'd and looped
* or anything else you might do with an <code>Playable</code> object. The advantage, however, is that
* an <code>AudioSample</code> can be retriggered while it is still playing, which will cause the sample to
* overlap with itself .
*/
import ddf.minim.*;
import processing.serial.*;
String portname = "/dev/tty.usbserial-A4001qa8"; // or "COM8"
Serial port; // Create object from Serial class
AudioSample sounds[];
String sound_names[] =
{
"cat.wav",
"fx.mp3",
"electric_wrench.wav",
"wehoa.mp3",
"oriental_gong_2.wav",
"yipee.wav",
"car_brake.wav"
// find more wav or mp3 files and put them in the "data" directory
};
void setup()
{
size(400, 400);
background(0);
stroke(255);
// always start Minim before you do anything with it
Minim.start(this);
Minim.debugOn();
sounds = new AudioSample[sound_names.length];
for( int i=0; i< sound_names.length; i++ )
{
sounds[i] = Minim.loadSample(sound_names[i], 512);
}
// Open the port that the board is connected to and use the same speed (19200 bps)
port = new Serial(this, portname, 19200);
}
void draw()
{
// do the drawing on events
}
void soundball()
{
int r = int(random(sounds.length));
println("picked sound #"+r);
sounds[r].trigger(); // play a random sound
int x = int(random(0,300));
int y = int(random(0,300));
fill(240,0,0);
ellipse(x,y, 40,40);
fill(30,0,0);
ellipse(x,y, 8,8);
}
void serialEvent(Serial p)
{
char inByte = port.readChar();
println("received char: "+ inByte);
if( inByte == '!' ) // '!' is end of "knock!"
{
soundball();
}
}
void keyPressed()
{
if(key == 't')
{
background(40,40,40); // erase screen
}
soundball();
}
void stop()
{
// always close Minim audio classes when you are done with them
for( int i=0; i<sounds.length; i++ )
{
sounds[i].close();
}
super.stop();
}
私の下手な英語を許してください!