Code for Processing: Difference between revisions

From Medien Wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:


int r;
int r;
                   
 
                 
void setup() {
void setup() {
  size(600, 600);        // window size
size(600, 600);        // window size


  randomSeed(second());  // initialize random number
randomSeed(second());  // initialize random number


  r = width;
r = width;


  println(Serial.list());
println(Serial.list());


  myPort = new Serial(this, Serial.list()[2], 9600);
myPort = new Serial(this, Serial.list()[2], 9600);


  myPort.bufferUntil('\n');                          // store data in buffer
myPort.bufferUntil('\n');                          // store data in buffer
    
    
}
}


void draw()
void draw()
{  
{  
  background(255);
background(255);


  fill(random(0,255), random(0,255), random(0,255));  // fill with random color
fill(random(0,255), random(0,255), random(0,255));  // fill with random color


  ellipse(width/2, height/2, r, r);                      // draw a circle
ellipse(width/2, height/2, r, r);                      // draw a circle


  line(width/2, 0, width/2, height);                  // draw a line
line(width/2, 0, width/2, height);                  // draw a line


  line(0, height/2, width, height/2);                // draw a line
line(0, height/2, width, height/2);                // draw a line




  fill(0, 120);                                                  // black, transparency 120
fill(0, 120);                                                  // black, transparency 120


  textAlign(CENTER, CENTER);
textAlign(CENTER, CENTER);


  textSize(r/2);
textSize(r/2);


  text(r, width/2, height/2);            // express distance in the window (스케치 실행창에 거리측정값 표현)
text(r, width/2, height/2);            // express distance in the window (스케치 실행창에 거리측정값 표현)


}
}




void serialEvent(Serial p) {                          // Read Serial port value
void serialEvent(Serial p) {                          // Read Serial port value
    
    
  String response = p.readStringUntil('\n').trim();  // Read Stored Data
String response = p.readStringUntil('\n').trim();  // Read Stored Data


  r = int(response);
r = int(response);


  r = constrain(r, 5, width) ;                        // r is MIN: 5 ~ MAX: width
r = constrain(r, 5, width) ;                        // r is MIN: 5 ~ MAX: width


}
}

Latest revision as of 16:29, 23 November 2021

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

}