voidsetup() { pinMode(2,INPUT); pinMode(8, OUTPUT); Serial.begin(9600); } voidloop() { // put your main code here, to run repeatedly: int x=analogRead(A0); int y=analogRead(A1); tone(8,x); String s=String(x)+" "+String(y); Serial.println(s); digitalRead(2); }
Processing
import processing.serial.*; Serial myPort; voidsetup() { printArray(Serial.list()); myPort = new Serial(this, "/dev/cu.usbserial-14120",9600); } voiddraw() { while (myPort.available() > 0) { println(myPort.readString()); } }
使用arduino透過serial控制會動的球球
Arduino
voidsetup() { pinMode(2,INPUT); pinMode(8, OUTPUT); Serial.begin(9600); } voidloop() { // put your main code here, to run repeatedly: int x=analogRead(A0); int y=analogRead(A1); String s=String(x)+" "+String(y); tone(8,x); Serial.println(s); digitalRead(2); delay(100); }
processing
使用 splitTokens() 分割xy字串
import processing.serial.*; Serial myPort; voidsetup() { size(1024, 1024); printArray(Serial.list()); myPort = new Serial(this, "/dev/cu.usbserial-14120",9600); } int x=512; int y=512; voiddraw() { background(0); while (myPort.available() > 0) { String now = myPort.readString(); String[] xy = splitTokens(now); x= int(xy[0]); y= int(xy[1]); } println(x,",",y); fill(255,0,0); circle(x,y, 20); }