我一直在做一個小小的溫度計項目來學習Arduino,並且有一件惱人的事情,我不知道如何解決。在Arduino中讀取傳感器時按下按鈕
我有兩個按鈕來設置最小和最大溫度,當我按下按鈕時,它應該設置最小和最大溫度顯示。 問題在於,在閱讀溫度傳感器期間,當按下按鈕時,有時(50%的次數)按鈕不起作用。我按下它,但最低/最高溫度未設置,因爲Arduino卡在讀取溫度傳感器。
解決這類問題有什麼竅門嗎?如果我有鍵盤輸入某個數字,例如我想我會遇到同樣的問題,而且它不是「用戶友好」的。
這裏是我使用的代碼的一部分的例子:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
//variables declaration...
void setup() {
sensors.begin();
sensors.getAddress(sensor1, 0);
pinMode(buzzer, OUTPUT);
pinMode(btBuzzer, INPUT);
pinMode(btMin, INPUT);
pinMode(btMax, INPUT);
}
void loop() {
readButtons();
playBuzzer();
readTemperature();
printDisplay();
delay(150);
}
void readButtons(){
if(digitalRead(btBuzzer)){
buzzerOn = !buzzerOn;
}
if(digitalRead(btMin)){
if(tempMin == 69)
tempMin = 59;
else
tempMin++;
}
if(digitalRead(btMax)){
if(tempMax == 75)
tempMax = 63;
else
tempMax++;
}
}
void readTemperature(){
sensors.requestTemperatures();
temperature = sensors.getTempC(sensor1);
}
//lots of other methods
也許你的按鈕在延遲期間?嘗試刪除/減少改進的延遲。或者完全刪除延遲做這樣的事情https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay – Sush