2018年12月13日 星期四

week14 Steven課堂筆記

從Arduino中選取旋鈕範例


使用範例與旋鈕座結合
旋轉到大值LED會閃爍
int sensorPin = A0;    // select the input pin for the potentiometer
int 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 potentiometer
int 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);
}

期末作品測試(光敏電阻)
int photocellPin = 2; // 光敏電阻 (photocell) 接在 anallog pin 2
int photocellVal = 0; // photocell variable
void setup() {
  Serial.begin(9600);
}
void loop() {
  // 讀取光敏電阻並輸出到 Serial Port
  photocellVal = analogRead(photocellPin);
  Serial.println(photocellVal);
  delay(100);   
}

沒有留言:

張貼留言