2016年11月28日 星期一

WEEK13車子動起來啦

1.會動的車子


程式碼
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);
}

2.開啟2-4的範例
按下按鈕燈會亮!!!
3.開啟Serial Monitor

1是沒按按鈕0是按下按鈕


按下按鈕燈泡會亮










4.把程式移到arduino&processing裡面
用按鈕控制車子移動
processing程式碼
import processing.serial.*;
Serial myPort;  // Create object from Serial class
int val; 

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) {  // If data is available,
    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);
}
arduino程式碼2-4去改der
void setup(){
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(13, OUTPUT); 

}

void loop(){
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  int sensorVa3 = digitalRead(3);
  
  if (sensorVal == LOW) Serial.println('0');
  if (sensorVa3 == LOW) Serial.println('1');
  
}
5.20行做車子



沒有留言:

張貼留言