2018年1月8日 星期一

week17


processing 程式碼

import processing.serial.*;
Serial myPort;
color [] Color1={#558AE3, #F2E820, #73F220},Color2={#21F7FC, #ED22CC, #FAB33F};
color [] fishC=new color[16], fishA=new color[8];
PImage imgRod,imghook;

void setup(){
  size(750,600);
  imgRod=loadImage("Rod.jpg");
  imghook=loadImage("hook.jpg");
  for(int i=0;i<16;i++){
  fishC[i]= Color1[int(random(3))];
  }
  for(int j=0;j<8;j++){
  fishA[j]= Color2[int(random(3))];
  }
  imageMode(CENTER);
  myPort = new Serial(this, "COM7", 115200);
}
float handX=0, handY=0;
boolean handPressed=false;
float a1=0,a2=0;
void draw(){
  while(myPort.available()>0){
    int now=myPort.read(), w=128,x=128,y=128;
    if(now=='w') w = myPort.read();
    if(now=='x') x = myPort.read();
    if(now=='y') y = myPort.read();
    if(x>200) handX+=3;
    if(x<50) handX-=3;
    if(y>200) handY+=3;
    if(y<50) handY-=3;
   
  }
  background(255);
  line(handX-45,handY-70,handX-50,handY+50);
  fill(255); ellipse(350,350,400,400);
  for(int i=0;i<16;i+=2){
    float x1=350+140*cos(PI/8*i+a1),y1=350+140*sin(PI/8*i+a1);
    if(dist(x1,y1,handX-50,handY+50)<25){
      fill(255,0,0);
      if(handPressed=true) fishC[i]=color(255);
    }
    else
    fill(fishC[i]); ellipse(x1,y1,50,50);
   }
 
  for(int i=1;i<16;i+=2){
    float x2=350+160*cos(PI/8*i+a1),y2=350+160*sin(PI/8*i+a1);
     if(dist(x2,y2,handX-50,handY+50)<25){
      fill(255,0,0);
      if(handPressed=true) fishC[i]=color(255);
    }
    else
    fill(fishC[i]); ellipse(x2,y2,50,50);
  }
 
 fill(255); ellipse(350,350,200,200);
  for(int j=0;j<8;j+=2){
    float x3=350+70*cos(PI/4*j+a2),y3=350+70*sin(PI/4*j+a2);
    if(dist(x3,y3,handX-50,handY+50)<25){
      fill(255,0,0);
      if(handPressed=true) fishA[j]=color(255);
    }
    else
    fill(fishA[j]); ellipse(x3,y3,50,50);
  }
 
   for(int j=1;j<8;j+=2){
     float x4=350+50*cos(PI/4*j+a2),y4=350+50*sin(PI/4*j+a2);
     if(dist(x4,y4,handX-50,handY+50)<25){
      fill(255,0,0);
      if(handPressed=true) fishA[j]=color(255);
    }
    else
    fill(fishA[j]); ellipse(x4,y4,50,50);
  }
 
  a1+=radians(-0.3);
  a2+=radians(0.3);
  image(imgRod,handX,handY,150,150);
  image(imghook,handX-50,handY+50,30,30);

}
void mouseMoved(){
  handX=mouseX; handY=mouseY;
}
void mousePressed(){
  handPressed=true;
}
void mouseReleased(){
  handPressed=false;
}

ADR 程式碼

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}

void loop() {
  Serial.write(digitalRead(2));
  int w=digitalRead(SW_pin);
  int x=analogRead(X_pin);
  int y=analogRead(Y_pin);
  Serial.write('w');
  Serial.write(w);
  Serial.write('x');
  Serial.write(x/4);
  Serial.write('y');
  Serial.write(y/4);
  delay(50);
}

沒有留言:

張貼留言