我正在创建一个程序,该程序从超声波传感器/arduino 获取串行信息并将其发送到处理,这将播放 crickets.mp3 或 growl.mp3,具体取决于输入的值。
如果值 <10 应该播放 crickets.mp3 如果值 >10 应该播放咆哮.mp3
照原样,代码将播放 growl.mp3,但不会播放 crickets.mp3
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer player, player2;
Serial myPort; // Create object from Serial class
String dataFromArduino; // Data received from the serial port
String convertedDataFromArduino;
boolean soundactivated = false;
boolean sound2activated = false;
void setup() {
size(640, 480);
minim = new Minim(this);
player = minim.loadFile("crickets.mp3");
player2 = minim.loadFile("growl.mp3");
printArray(Serial.list());
String portName = Serial.list()[4]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw() {
player.pause();
dataFromArduino = myPort.readStringUntil('\n'); // read it and store it in message
if (dataFromArduino != null) {
convertedDataFromArduino = trim(dataFromArduino);
println(convertedDataFromArduino);
//Music calls
if (int(convertedDataFromArduino) < 10 && (soundactivated == false)) {
player.loop();
println("crickets");
soundactivated = true;
sound2activated = false;
} else if (int(convertedDataFromArduino) >= 11 && (sound2activated == false)) {
player2.loop();
println("growl");
sound2activated = true;
soundactivated = false;
}
//loop();
}
}