震動開關推動LED
當一端低於水準位置傾斜,開關尋通,類比口電壓值為 5V 左右(數位二進位表示為 1023 ),點亮 led 燈。將A0所讀到數值由0~1023轉為0~255,用意是為了提供給LED推動實縮需要的PWM,另外這不只可以使用在震動開關上面,只要是有類比變化的接可接在Arduino類比端輸入,一樣能推動。 const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out) void setup() { Serial.begin(9600); } void loop() { sensorValue = analogRead(analogInPin); outputValue = map(sensorValue, 0, 1023, 0, 255); analogWrite(analogOutPin, outputValue); Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Ser...