Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //We'll need this to communicate with our arduino program
- import processing.serial.*;
- //our serial communication port that we define later
- Serial analogPort;
- //the initial x value of our graph
- int xPosition=0;
- //the initial audio value for our graph
- float audVal = 0;
- void setup(){
- //creates GUI window frame
- size(300,200);
- //selects the first available com port to communicate with
- printArray(Serial.list());
- analogPort = new Serial(this, Serial.list()[0], 9600);
- analogPort.bufferUntil('\n');
- //makes the GUI background black/clears it
- background(0);
- }
- void draw(){
- //creates a blue line from (xPosition, height) to (xPosition, height - audVal) ***NOTE: Y-AXIS IS FLIPPED***
- stroke(22, 79, 250);
- line(xPosition, height, xPosition, height - audVal);
- //if the xPosition is further than the width of the graph itself, let's erase everything
- //and start from scratch
- if(xPosition>=width){
- xPosition=0;
- background(0);
- }
- //otherwise, let's start working on the next x value
- else{
- xPosition++;
- }
- }
- //a new serial value was just printed by our arduino
- void serialEvent(Serial analogPort){
- //serial messages are always strings and we will set this
- //string equal to the new line of the serial message
- String serialMessage = analogPort.readStringUntil('\n');
- //if the serial message isn't empty then...
- if(serialMessage!=null){
- //let's get rid of all the white space
- serialMessage = trim(serialMessage);
- //let's make a number out of our string
- audVal=float(serialMessage);
- //let's adjust our values in proportion
- //to the dimensions of our graph
- audVal=map(audVal, 0, 1023, 0, height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement