实际上我们正在开发一个基于Arduino和Processing的项目。基本上解释一下,我们有多个爆震传感器(或piezzos)。我们的目标是每次“敲击”时发出一个声音文件,但我们遇到了一些从piezzo获取数据的问题。我们处理Arduino草图和Processing草图,但我们没有设法在两个草图之间建立路径。所以我们的问题是:在处理草图中,如何获得arduino草图的piezzo值,并在每次敲击时启动声音文件?
Arduino Sketch:
// 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();
}
请原谅我可怜的英语!
答案 0 :(得分:3)
在Arduino草图中,串行端口的波特率设置为9600(Serial.begin(9600)
),但在处理中,波特率为19200(port = new Serial(this, portname, 19200)
)。您需要更改这些以使波特率匹配。