SHARE
TWEET

Capacitive Sensor, Processing Code

a guest May 21st, 2015 251 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // this example is in the public domain
  2.  
  3.  
  4. int port = 2;
  5.  
  6. import processing.serial.*;     // import the Processing serial library
  7. import ddf.minim.*;
  8. import ddf.minim.ugens.*;
  9. import themidibus.*; //Import the library
  10.  
  11.  
  12. Serial myPort;                  // The serial port
  13. Minim minim;
  14. AudioOutput out;
  15. Oscil fm;
  16. MidiBus myBus; // The MidiBus
  17.  
  18. float bgcolor;          // Background color
  19. float fgcolor;          // Fill color
  20. float xpos, ypos;           // Starting position of the ball
  21.  
  22.  
  23. void setup() {
  24.   size(640,480);
  25.  
  26.   // List all the available serial ports
  27.   println(Serial.list());
  28.  
  29.   // I know that the first port in the serial list on my mac
  30.   // is always my  Arduino module, so I open Serial.list()[0].
  31.   // Change the 0 to the appropriate number of the serial port
  32.   // that your microcontroller is attached to.
  33.   myPort = new Serial(this, Serial.list()[port], 9600);
  34.  
  35.   // read bytes into a buffer until you get a linefeed (ASCII 10):
  36.   myPort.bufferUntil('\n');
  37.  
  38.   // draw with smooth edges:
  39.   smooth();
  40.  
  41.   // audio stuff
  42.   //
  43.   // initialize the minim and out objects
  44.   minim = new Minim( this );
  45.   out   = minim.getLineOut();
  46.   // make the Oscil we will hear.
  47.   // arguments are frequency, amplitude, and waveform
  48.   Oscil wave = new Oscil( 200, 0.8, Waves.SINE );
  49.   // make the Oscil we will use to modulate the frequency of wave.
  50.   // the frequency of this Oscil will determine how quickly the
  51.   // frequency of wave changes and the amplitude determines how much.
  52.   // since we are using the output of fm directly to set the frequency
  53.   // of wave, you can think of the amplitude as being expressed in Hz.
  54.   fm   = new Oscil( 10, 2, Waves.SINE );
  55.   // set the offset of fm so that it generates values centered around 200 Hz
  56.   fm.offset.setLastValue( 200 );
  57.   // patch it to the frequency of wave so it controls it
  58.   fm.patch( wave.frequency );
  59.   // and patch wave to the output
  60.   wave.patch( out );
  61.  
  62.   // MIDI stuff
  63.   MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
  64.   myBus = new MidiBus(this, 0, 0); // Create a new MidiBus
  65. }
  66.  
  67. void draw() {
  68.   background(bgcolor);
  69.   fill(fgcolor);
  70.   // Draw the shape
  71.   ellipse(xpos, ypos, 20, 20);
  72.   // draw using a white stroke
  73.   stroke( 255 );
  74.   // draw the waveforms
  75.   for( int i = 0; i < out.bufferSize() - 1; i++ )
  76.   {
  77.     // find the x position of each buffer value
  78.     float x1  =  map( i, 0, out.bufferSize(), 0, width );
  79.     float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
  80.     // draw a line from one buffer position to the next for both channels
  81.     line( x1, 150 + out.left.get(i)*50, x2, 150 + out.left.get(i+1)*50);
  82.     line( x1, 250 + out.right.get(i)*50, x2, 250 + out.right.get(i+1)*50);
  83.   }  
  84.  
  85.   text( "Modulation frequency: " + fm.frequency.getLastValue(), 5, 15 );
  86.   text( "Modulation amplitude: " + fm.amplitude.getLastValue(), 5, 30 );
  87. }
  88.  
  89. // serialEvent  method is run automatically by the Processing applet
  90. // whenever the buffer reaches the  byte value set in the bufferUntil()
  91. // method in the setup():
  92.  
  93. void serialEvent(Serial myPort) {
  94.   // read the serial buffer:
  95.   String myString = myPort.readStringUntil('\n');
  96.   // if you got any bytes other than the linefeed:
  97.     myString = trim(myString);
  98.  
  99.     // split the string at the commas
  100.     // and convert the sections into integers:
  101.     int sensors[] = int(split(myString, ','));
  102.  
  103.     // print out the values you got:
  104.     //for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
  105.     //  print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
  106.     //}
  107.     // add a linefeed after all the sensor values are printed:
  108.     //println();
  109.     if (sensors.length > 1) {
  110.       xpos = map(sensors[0], 0, 600, 0, width);
  111.       ypos = map(sensors[1], 0, 600, 0, height);
  112.       fgcolor = 255;
  113.      
  114.       float modulateAmount = map( ypos, 0, height, 1, 512 );
  115.       float modulateFrequency = map( xpos, 0, width, 0.1, 100 );
  116.      
  117.       fm.setFrequency( modulateFrequency );
  118.       fm.setAmplitude( modulateAmount );
  119.  
  120.       int channel = 0;
  121.       int pitch = int(constrain(map(xpos, 0, width, 0, 127), 0, 127));
  122.       int velocity = 127;
  123.       myBus.sendNoteOn(channel, pitch, velocity); // Send a Midi noteOn
  124.       delay(200);
  125.       myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi nodeOff
  126.  
  127.       channel = 1;
  128.       pitch = int(constrain(map(ypos, 0, height, 0, 127), 0, 127));
  129.       velocity = 127;
  130.       myBus.sendNoteOn(channel, pitch, velocity); // Send a Midi noteOn
  131.       delay(200);
  132.       myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi nodeOff
  133.     }
  134.     // send a byte to ask for more data:
  135.     myPort.write("A");
  136.   }
  137.  
  138.  
  139.  
  140. void delay(int time) {
  141.   int current = millis();
  142.   while (millis () < current+time) Thread.yield();
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top