//To Connect the Arduino with the Processing
import processing.serial.*;
int val= 0;
Serial myPort;
int N=400;
int fatness=20;
int step=10;
int bg =0;
int bg2 =0;
int bg3 =0;
float[]r= new float[N];
float[]as= new float[N];
float[]ca= new float[N];
void setup() {
myPort = new Serial(this,"/dev/tty.usbmodemfd1411",9600);//To connect the Serial port
frameRate(60);
size(1280, 920);
smooth();
for (int i = 0; i< N; i++) {
r[i] = 50+ i;
as[i] = random(-PI/90, PI/90);
ca[i] = random(-PI, PI);//Pi = half circle, two pi = whole circle)
}
}
void draw() {
background(0);
frameRate(60);
star(70, 20, 2);
//when the value of the potentiometer is larger than 100, more planets(ellipses) will appear
if (val >100) {
rotate(radians(22));
star(70, 20, 2);
}
//when the value of the potentiometer is larger than 150, a black hole is called
if (val>150 ) {//call the black hole (nearest)
frameRate(60);
float c=400;
float b=1;
fatness = fatness + step;
fill(0);
ellipse(0, 0, fatness+c, fatness+c);
star(20, 20, 2);
fill(0, 0, 0, 90);
ellipse(30, 1, fatness+b, fatness+b);
fill(0, 0, 0, 95);
ellipse(34, 2, fatness+b, fatness+b);
fill(0, 0, 0, 99);
ellipse(38,3, fatness+b, fatness+b);
fill(0, 0, 0);
ellipse(42,4, fatness+b, fatness+b); }
if (bg>=0&&bg2>=0&&bg3>=0) {
bg+=2;
bg2+=2;
bg3+=2;
}
}
void star(int o, int n, int scalingFactor ) {
for (int i = 0; i< N; i++) {
ca[i]+=as[i];
int p= 1;
//translate the angle X and Y
float x = sin(ca[i]) * r[i];
float y = cos(ca[i]) *r[i];
float k = sin(ca[0]) * r[55];
float m = cos(ca[0]) *r[0];
float g = cos(ca[p]) *r[p];
//draw the ellipse
fill(255, 246, 209, y);
triangle(val+523, 529+y, 527+x, 525+y, 531+x, 529+y);//level 1
noStroke();
rotate(radians(44));
fill(20, 15, k, y);
ellipse(val+500, m+500, 25, 25);//when the value of the potentiometer is changed, the x-coordinates of the ellipses are being changed
fill(20, m, 250, y);
ellipse(val+500, k+500, 25, 25);//when the value of the potentiometer is changed, the x-coordinates of the ellipses are being changed
ellipse(m+500, k+500, 25, 25);
fill(255, 224, 121, x);
ellipse(m+400, k+420, 10, 10);
ellipse(m+280, k+280, 2, 2);
ellipse(m+280, k+280, 2, 2);
ellipse(m+380, k+380, 2, 2);
fill(255, 226, 209, x);
ellipse(m+320, k+320, 6, 6);//level2
fill(255, 246, 209, y);
triangle(m+483, 499+k, 487+m, 495+k, 521+m, 499+k);
fill(255, 102, 0, y);
ellipse(k+450, m+450, 32, 32);
fill(255, 157, 157, x);
ellipse(m+250, k+250, 23, 23);//level3
fill(209, 165, 80, x);
ellipse(m+350, k+350, 15, 15);
fill(99, 252, 243, x);
ellipse(m+380, k+380, 30, 30);//level4
fill(255, 102, 0, y);
triangle(m+300, 305+k, 300+m, 295+k, 305+m, 299+k);
triangle(m+310, 305+k, 310+m, 295+k, 315+m, 299+k);
triangle(m+295, 295+k, 295+m, 285+k, 300+m, 289+k);
// draw the sun
fill(235, 255, 170, y);
ellipse(50, 50, 150, 150);//level6
}
}
void serialEvent(Serial myPort){
if (myPort.available()> 0){
val = myPort.read();
}
println(val);
}


沒有留言:
張貼留言