*老師的教學:
裝好Arduino的驅動程式後,開啟Arduino開啟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);
}
旋鈕的接法如下:

成功執行後,往右旋燈閃爍的速度會變慢,往左旋燈閃爍的速度會變快
閃爍的燈為第13個燈
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*測試按鈕:
匯入程式庫如下圖

執行程式碼:
#include <Bounce.h>
Bounce bouncer = Bounce(2, 50);
static int ledStatus = LOW;
void setup(){
Serial.begin(115200);
pinMode(2, INPUT);
pinMode(13, OUTPUT);
digitalWrite(13, ledStatus);
}
void loop(){
bouncer.update();
if(bouncer.read() == HIGH) {
ledStatus = bouncer.duration() >= 5000 ? LOW : HIGH;
}
else{
ledStatus = LOW;
}
digitalWrite(13, ledStatus);
}
成功執行後按下按鈕就會亮TX燈(紅色),持續按著燈會亮著,放開就會熄掉
如果快速的按下按鈕並放開數次,燈就會如預期的閃爍 ~ 測試大成功!!!
沒有留言:
張貼留言