Arduino學習321.類比控制閃燈
硬體,可變電阻*1
↓打開內建範例,,檔案>範例>Analog>AnalogInput
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);
}
上傳後調整電阻試試,內建Pin13的LED燈會變快閃慢閃,
再來試著打開ArduBlock拼圖如下↓
void setup()
{
pinMode(
13 , OUTPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(
13 , HIGH );
delay(
analogRead(0) );
digitalWrite(
13 , LOW );
Serial.print( analogRead(0) );
Serial.println();
delay(
100 );
}
上傳後打開監控視窗,調整可變電阻,可得到數據,並控制LED閃燈秒差,
數位訊號只有開和關,0和1的訊號,並無法像PWM訊號調整亮度,
學習了腳位功能有何不同嗎?
沒有留言:
張貼留言