

程式碼:
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 sensorVal3 = digitalRead(3);
if (sensorVal == LOW)Serial.print('0');
if (sensorVal3 == LOW)Serial.print('1');
}

Processing
我們要連結File→Example→Libraries→Serial→SimpleRead

片段程式碼:
import processing.serial.*;
Serial myPort;
int val;
void setup() {
size(640, 480, P3D);
myPort = new Serial(this, "COM3", 9600);
rectMode(CENTER);
}
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);
}
完成圖:





沒有留言:
張貼留言