processing
import processing.serial.*;
import ddf.minim.*;
char val='Z';
Minim minim;
AudioPlayer pianoA, pianoB, pianoC, pianoD, pianoE;
Serial myPort;
void setup()
{
String portName = Serial.list()[0];
myPort = new Serial(this, "COM5", 9600);
minim = new Minim(this);
pianoA=minim.loadFile("C.MP3");
pianoB=minim.loadFile("D.MP3");
pianoC=minim.loadFile("E.MP3");
pianoD=minim.loadFile("F.MP3");
pianoE=minim.loadFile("G.MP3");
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.readChar(); // read it and store it in val
} else {
val = 'Z';
}
if(val=='C'){pianoA.rewind();pianoA.play();}//倒帶;播放
if(val=='D'){pianoB.rewind();pianoB.play();}
if(val=='E'){pianoC.rewind();pianoC.play();}
if(val=='F'){pianoD.rewind();pianoD.play();}
if(val=='G'){pianoE.rewind();pianoE.play();}
}
arduino
#include <CapacitiveSensor.h>
/*
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Uses a high value resistor e.g. 10M between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
*/
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor cs_4_9 = CapacitiveSensor(4,9);
CapacitiveSensor cs_13_10 = CapacitiveSensor(13,10);
int push=0;
long total1old=100;
void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);
}
void loop()
{
long start = millis();
long total1 = cs_4_2.capacitiveSensor(30);
long total2 = cs_4_6.capacitiveSensor(30);
long total3 = cs_4_8.capacitiveSensor(30);
long total4 = cs_4_9.capacitiveSensor(30);
long total5 = cs_13_10.capacitiveSensor(30);
if(total1>7000) Serial.print("C");
if(total2>7000) Serial.print("D");
if(total3>7000) Serial.print("E");
if(total4>7000) Serial.print("F");
if(total5>7000) Serial.print("G");
// Serial.print(millis() - start); // check on performance in milliseconds
// Serial.print("\t"); // tab character for debug windown spacing
//
// Serial.print(total1); // print sensor output 1
//// total1=total1;
//// if(total1-total1old>0&&push==0){Serial.write(1);push=1;}
//// if(total1-total1old<0&&push==1){push=0;}
//// total1old=total1;
//
// Serial.print("\t");
// Serial.print(total2); // print sensor output 2
// Serial.print("\t");
// Serial.println(total3); // print sensor output 3
// arbitrary delay to limit data to serial port
}




沒有留言:
張貼留言