我目前有一個arduino LCD和一個SPDT開關連接到我的主板。 SPDT的公共引腳接地,外部引腳分別連接到數字輸入。我的程序應該遞增和遞減正在打印到LCD屏幕的計數器。我有一個輸入工作,遞增計數器,我不知道如何實現輸入代碼遞減計數器。代碼如下。Arduino LCD數字輸入計數器
謝謝
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5,4,3,2);
const byte buttonPin = 8;
int counter = 0; // set your counter to zero to begin with
byte buttonState; // the current reading from the input pin
byte lastButtonState = HIGH; // the previous reading from the input pin
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
lcd.begin(16, 2); // for 2x16 lcd display
}
void loop() {
// read the state of the switch into a local variable:
byte reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from HIGH to LOW), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) >= debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
counter ++;
Serial.println(counter);
lcd.setCursor(0, 1);
lcd.print(counter);
}
}
}
lastButtonState = reading;
}
爲什麼代碼與此不同?除了計數器的引腳和方向? – 2015-07-10 03:22:22
我將如何使這個草圖倒計時,因爲它是一個SPDT開關,它連接到兩個輸入。它不能附加到一個輸入。 – Marcus
即使是C++,還是僅僅是Arduino? – Olaf