2016年11月28日 星期一

Week13 河蟹牌 - 做惡夢

和往常一樣,先把Arduino裝好,驅動程式也設定好。


開啟 2-4 範例


開啟 4-7 範例


今天這樣接
接好會是這樣



按下按鈕,燈泡會亮


開啟 Serial Monitor
沒按按鈕的情況下,會跑很多1出來



按下按鈕,會變 0


---------------------------------------

再來打開Processing



貼上程式碼


void setup() {
size(640, 480, P3D);
rectMode(CENTER);
}
float carX=640/2, carY=320/2;
float carDir=PI, carWheel=0;
void drawCar() {
pushMatrix();
translate(carX, carY);
rotateZ(carDir);
rect(0, 0, 187, 89);
drawWheel(127/2, 89/2, 1);
drawWheel(127/2, -89/2, 1);
drawWheel(-127/2, 89/2, 0);
drawWheel(-127/2, -89/2, 0);
if (keyPressed && keyCode==UP) {
carX+= cos(carDir+carWheel);
carY+=sin(carDir+carWheel);
carDir+=carWheel/50;
} else if (keyPressed && keyCode==DOWN) {
carX-= cos(carDir+carWheel);
carY-=sin(carDir+carWheel);
carDir-=carWheel/50;
} else if (keyPressed && keyCode==RIGHT) {
carWheel+=0.01;
if (carWheel > 0.3) carWheel=0.3;
} else if (keyPressed && keyCode==LEFT) {
carWheel-=0.01;
if (carWheel < -0.3) carWheel=-0.3;
}
popMatrix();
}
void drawWheel(float x, float y, int front) {
pushMatrix();
translate(x, y);
if (front==1) rotateZ(carWheel);
rect(0, 0, 40, 20);
popMatrix();
}
void draw() {
background(100);
drawCar();
rect(0+187/2, 0+89/2, 187, 89);
rect(450+187/2, 0+89/2, 187, 89);
}


再來開啟範例 SimpleRead




如下



將範例的一行程式移至車子程式碼中  :  import processing.serial.*;





下兩行也要
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port





還有一行 myPort = new Serial(this, portName, 9600);
插入圖中位置




加上一行
  while(myPort.available() > 0){
    val = myPort.read();
  }
  println(val);




程式碼變這樣 :

import processing.serial.*;
Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() {
  size(640, 480, P3D);
  myPort = new Serial(this, "COM3", 9600);
  rectMode(CENTER);
}
float carX=640/2, carY=320/2;
float carDir=PI, carWheel=0;
void drawCar() {
  pushMatrix();
  translate(carX, carY);
  rotateZ(carDir);
  rect(0, 0, 187, 89);
  drawWheel(127/2, 89/2, 1);
  drawWheel(127/2, -89/2, 1);
  drawWheel(-127/2, 89/2, 0);
  drawWheel(-127/2, -89/2, 0);
  if (keyPressed && keyCode==UP) {
    carX+= cos(carDir+carWheel);
    carY+=sin(carDir+carWheel);
    carDir+=carWheel/50;
  } else if (keyPressed && keyCode==DOWN) {
    carX-= cos(carDir+carWheel);
    carY-=sin(carDir+carWheel);
    carDir-=carWheel/50;
  } else if (keyPressed && keyCode==RIGHT) {
    carWheel+=0.01;
    if (carWheel > 0.3) carWheel=0.3;
  } else if (keyPressed && keyCode==LEFT) {
    carWheel-=0.01;
    if (carWheel < -0.3) carWheel=-0.3;
  }
  popMatrix();
}
void drawWheel(float x, float y, int front) {
  pushMatrix();
  translate(x, y);
  if (front==1) rotateZ(carWheel);
  rect(0, 0, 40, 20);
  popMatrix();
}
void draw() {
  while (myPort.available () > 0) {
    val = myPort.read();

    if (val==48) {
      carX+= cos(carDir+carWheel);
      carY+=sin(carDir+carWheel);
      carDir+=carWheel/50;
    } else if (val==49) {
      carX-= cos(carDir+carWheel);
      carY-=sin(carDir+carWheel);
      carDir+=carWheel/50;
    }
  }
  println(val);
  background(100);
  drawCar();
  rect(0+187/2, 0+89/2, 187, 89);
  rect(450+187/2, 0+89/2, 187, 89);
}

按按鈕(0)會往前,不按按鈕(1)往後



沒有留言:

張貼留言