類比訊號輸入練習
跟隨佑來認真教練習類比訊號輸入
影片
程式碼
int sensor = A0;
int sensorRead = 0;
int newdata = 0;
int bright = 0;//亮度值
int printout = 0;//印出條件
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //設定傳輸位元
}
void loop() {
// put your main code here, to run repeatedly:
sensorRead = analogRead(sensor);//類比訊號源 亦可直接打A3:
newdata = map(sensorRead, 0, 1023, 0,500);//設定newdata 的數據由0~1023轉成0~500:
bright = map(newdata ,0 ,500 ,0 ,255);//設定亮度輸出值由0~500轉成0~255:
analogWrite(3 ,bright);//設定類比輸出到A3與輸出數值:
if (printout !=newdata ){ //設定電壓有變才印出數值:
Serial.print("V=");
Serial.print(newdata);
Serial.print(";L=");
Serial.println(bright);
delay(500);//設定電壓有變時取樣間隔時間:
}
printout = newdata;
delay(20);
}
留言
張貼留言