2020年12月21日 星期一

Carousel~W15

 

PVector user;//外面宣告,setup & draw看得到
void setup(){
  size(600,600);
  user = new PVector(10, 20);//只在setup()
}
void draw(){
background(255);
ellipse( user.x, user.y,20,20);//用它的值
}



憤怒鳥

PVector user;

PVector Y, v=null;

void setup(){

  size(600, 400);

  user = new PVector(100, 300);//(2)只在setu

  Y = new PVector(100, 300);

}

void draw(){

  background(255);

  line( user.x, user.y, Y.x, Y.y);

  textSize(40);

  fill(255,0,0); text("Y", Y.x, Y.y);

  fill(255); ellipse( user.x, user.y,20,20);

  if(v!=null) user.add(v);

}

void mouseDragged(){

  user.x=mouseX;

  user.y=mouseY;

}

void mouseReleased(){

PVector diff = PVector. sub(Y,user);

v = diff.div(10);

}


傳輸速率

void setup(){

pinMode (2, INPUT_PULLUP) ; //拉高的Input

Serial.begin (9600);//開始設定USB的傳輸速度

}

void loop() {//1000Hz (1000 fps)

int x = analogRead (A0);//0...1023

int y = analogRead(A1);//0...1023

int sw= digitalRead(2);//HIGH(放開)or LOW(按下去)

Serial.write (x);

Serial.write ('+') ;

Serial.write(y) ;

Serial.write ('+') ;

Serial.write (sw) ;

Serial.write(' ');

delay (20) ;//1000ms/20ms => 50fps,變慢了

}



void setup()
{
  pinMode(2, INPUT_PULLUP);//拉高的Input
  Serial.begin(9600);//開始設定USB的傳輸速度
}
void loop() {//1000Hz (1000 fps)
  int x = analogRead(A0);//0...1023
  int y = analogRead(A1);//0...1023
  int sw= digitalRead(2);//HIGH(放開)
  
  Serial.write (x/4);//第1個byte
  Serial.write(y/4);//第2個byte
  Serial.write (sw); //第3個byte
  delay(20);//1000ms/20ms => 50fps,變慢了
}

processing

import processing.serial.*;

Serial myPort;

void setup(){

size(256,256);

myPort = new Serial(this, "COM6",9600);

}

int bal1X=0,bal1Y=0;

void draw()

{

 if(myPort.available()>=3)

 {

   int x = myPort.read();

   int y = myPort.read();

   int sw = myPort.read();

   bal1X=x;

   bal1Y=y;

 }

 ellipse(bal1X,bal1Y,10,10);

}



沒有留言:

張貼留言