使用範例與旋鈕座結合
旋轉到大值LED會閃爍
int sensorPin = A0; // select the input pin for the potentiometerint ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
變成跑值
int sensorPin = A0; // select the input pin for the potentiometerint ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(8, OUTPUT);
}
void loop() {
int value= analogRead(A0);
tone(8,value);
int c=(value/10+'0');
Serial.write(c);
delay(100);
}
加上Processing
使球可以隨著旋鈕左右移動
import processing.serial.*;
Serial myPort;
void setup(){
size (1024,600);
myPort=new Serial(this,"COM7",9600);
}
float handX=0;
void draw(){
background(255);
if(myPort.available()>0){
int c =myPort.read();
handX=(c-'0')*10;
}
ellipse(handX,500,100,100);
}




沒有留言:
張貼留言