SHARE
TWEET

MusicWaveGenerator

a guest May 25th, 2016 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //We'll need this to communicate with our arduino program
  2. import processing.serial.*;
  3.  
  4. //our serial communication port that we define later
  5. Serial analogPort;
  6.  
  7. //the initial x value of our graph
  8. int xPosition=0;
  9.  
  10. //the initial audio value for our graph
  11. float audVal = 0;
  12.  
  13. void setup(){
  14.    //creates GUI window frame
  15.    size(300,200);
  16.    
  17.    //selects the first available com port to communicate with
  18.    printArray(Serial.list());
  19.    analogPort = new Serial(this, Serial.list()[0], 9600);
  20.    analogPort.bufferUntil('\n');
  21.    
  22.    //makes the GUI background black/clears it
  23.    background(0);  
  24. }
  25.  
  26. void draw(){  
  27.    
  28.    //creates a blue line from (xPosition, height) to (xPosition, height - audVal) ***NOTE: Y-AXIS IS FLIPPED***
  29.    stroke(22, 79, 250);
  30.    line(xPosition, height, xPosition, height - audVal);
  31.    
  32.    //if the xPosition is further than the width of the graph itself, let's erase everything
  33.    //and start from scratch
  34.     if(xPosition>=width){    
  35.       xPosition=0;
  36.       background(0);      
  37.     }
  38.    
  39.     //otherwise, let's start working on the next x value
  40.     else{
  41.       xPosition++;
  42.     }  
  43. }
  44.  
  45. //a new serial value was just printed by our arduino
  46. void serialEvent(Serial analogPort){
  47.    
  48.     //serial messages are always strings and we will set this
  49.     //string equal to the new line of the serial message
  50.     String serialMessage = analogPort.readStringUntil('\n');
  51.    
  52.     //if the serial message isn't empty then...
  53.     if(serialMessage!=null){
  54.      
  55.       //let's get rid of all the white space
  56.       serialMessage = trim(serialMessage);
  57.      
  58.       //let's make a number out of our string
  59.       audVal=float(serialMessage);
  60.      
  61.       //let's adjust our values in proportion
  62.       //to the dimensions of our graph
  63.       audVal=map(audVal, 0, 1023, 0, height);
  64.     }
  65. }
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
 
Top