影片連結:https://youtu.be/f9izK3qcMRY
介紹:運用Processing及Arduino,做出一套敲打水杯就能模擬出爵士鼓的音效。
程式碼:
const int sensorPinA=0;
const int sensorPinB=1;
const int sensorPinC=2;
const int sensorPinD=4;
const int sensorPinE=5;
const int threshold=100;
long previousMillisA = 0;
long previousMillisB = 0;
long previousMillisC = 0;
long previousMillisD = 0;
long previousMillisE = 0;
long interval =50; // interval at which to blink (milliseconds)
int i=0;
int a1=1,a2=2;
int b1=3,b2=4;
int c1=5,c2=6;
int d1=7,d2=8;
int e1=9,e2=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int valA= analogRead(sensorPinA);
int valB= analogRead(sensorPinB);
int valC= analogRead(sensorPinC);
int valD= analogRead(sensorPinD);
int valE= analogRead(sensorPinE);
if (valA >=300)
if(millis() - previousMillisA > interval)
{
previousMillisA = millis();
if(i%2==0){ Serial.write(a1);Serial.println(a1);}
if(i%2!=0){ Serial.write(a2);Serial.println(a2);}
i++;
}
if (valB >= threshold)
if(millis() - previousMillisB > interval)
{
previousMillisB = millis();
if(i%2==0){ Serial.write(b1);Serial.println(b1);}
if(i%2!=0){ Serial.write(b2);Serial.println(b2);}
i++;
}
if (valC >= threshold)
if(millis() - previousMillisC > interval)
{
previousMillisC = millis();
if(i%2==0){ Serial.write(c1);Serial.println(c1);}
if(i%2!=0){ Serial.write(c2);Serial.println(c2);}
i++;
}
if (valD >= threshold)
if(millis() - previousMillisD > interval)
{
previousMillisD = millis();
if(i%2==0){ Serial.write(d1);Serial.println(d1);}
if(i%2!=0){ Serial.write(d2);Serial.println(d2);}
i++;
}
if (valE >= threshold)
if(millis() - previousMillisE > interval)
{
previousMillisE = millis();
if(i%2==0){ Serial.write(e1);Serial.println(e1);}
if(i%2!=0){ Serial.write(e2);Serial.println(e2);}
i++;
}
}
-------------------------------
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
*/
import processing.serial.*;
import ddf.minim.*;
Minim minim;
AudioPlayer drumA1;
AudioPlayer drumA2;
AudioPlayer drumB1;
AudioPlayer drumB2;
AudioPlayer drumC1;
AudioPlayer drumC2;
AudioPlayer drumD1;
AudioPlayer drumD2;
AudioPlayer drumE1;
AudioPlayer drumE2;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
minim = new Minim(this);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
// myPort = new Serial(this, "/dev/cu.usbmodem1411", 9600);
myPort = new Serial(this, "COM3", 9600);
drumA1= minim.loadFile("kick.mp3",128);
drumA2=minim.loadFile("kick.mp3",128);
drumB1= minim.loadFile("snare.mp3",128);
drumB2=minim.loadFile("snare.mp3",128);
drumC1= minim.loadFile("hat.mp3",128);
drumC2=minim.loadFile("hat.mp3",128);
drumD1= minim.loadFile("crash.mp3",128);
drumD2=minim.loadFile("crash.mp3",128);
// drumE1= minim.loadFile("crash.mp3",128);
// drumE2=minim.loadFile("crash.mp3",128);
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
background(205,170,125);
if (val == 1) {
drumA1.rewind();
drumA1.play();
fill(20);
}
if (val == 2) {
drumA2.rewind();
drumA2.play();
fill(255);
}
if (val == 3) {
drumB1.rewind();
drumB1.play();
fill(20);
}
if (val == 4) {
drumB2.rewind();
drumB2.play();
fill(255);
}
if (val == 5) {
drumC1.rewind();
drumC1.play();
fill(20);
}
if (val == 6) {
drumC2.rewind();
drumC2.play();
fill(255);
}
if (val == 7) {
drumD1.rewind();
drumD1.play();
fill(20);
}
if (val == 8) {
drumD2.rewind();
drumD2.play();
fill(255);
}
/*if (val == 9) {
drumE1.rewind();
drumE1.play();
fill(20);
}
if (val == 0) {
drumE2.rewind();
drumE2.play();
fill(255);
}*/
// rect(50, 50, 100, 100);
}
/*
// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.
int switchPin = 4; // Switch connected to pin 4
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.write(1); // send 1 to Processing
} else { // If the switch is not ON,
Serial.write(0); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}
*/
沒有留言:
張貼留言