1.Arduino *1
2.麵包板 *1
3.可變電阻(本範例是使用10K-轉動式可變電阻)
1.可變電阻腳位定義
2.可變電阻需要透過類比腳位來讀取電壓值。
由0~1023
可變電阻VCC ---》麵包板VCC
可變電阻GND---》麵包皮GND
可變電阻輸出---》Arduino A0
Arduino 5V ---》麵包板VCC
Arduino GND --》麵包板GND
基本上我是拿Arduino範例 AnlogInput 來加上序列埠顯示讀取到的值
Pin13為Arduino上的LED,所以不用再額外接LED。
#include <SoftwareSerial.h> 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); Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); Serial.println(sensorValue); // 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); }
沒有留言:
張貼留言