Code for Processing

From Medien Wiki
Revision as of 16:02, 23 November 2021 by Capo0368 (talk | contribs)

import processing.serial.*;

Serial myPort;

int r;

void setup() {

 size(600, 600);        // window size
 randomSeed(second());  // initialize random number
 r = width;
 println(Serial.list());
 myPort = new Serial(this, Serial.list()[2], 9600);
 myPort.bufferUntil('\n');                          // store data in buffer
 

}

void draw()

{ 
 background(255);
 fill(random(0,255), random(0,255), random(0,255));  // fill with random color
 ellipse(width/2, height/2, r, r);                       // draw a circle
 line(width/2, 0, width/2, height);                  // draw a line
 line(0, height/2, width, height/2);                 // draw a line


 fill(0, 120);                                                  // black, transparency 120
 textAlign(CENTER, CENTER);
 textSize(r/2);
 text(r, width/2, height/2);            // express distance in the window (스케치 실행창에 거리측정값 표현)

}


void serialEvent(Serial p) { // Read Serial port value

 String response = p.readStringUntil('\n').trim();   // Read Stored Data
 r = int(response);
 r = constrain(r, 5, width) ;                        // r is MIN: 5 ~ MAX: width

}