|
一下是编程
[mw_shl_code=cpp,true]/* LiquidCrystal push button counter */
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2); //pins that lcd is attached to
const int buttonPin = 8; //the pin that pushbutton is attached to
const int ledPin = 13; //the pin that led is attached to
int buttonPushCounter = 0; //counter for the number of button presses
int buttonState = 0; //current state of the button
int lastButtonState = 0; //previous state of the button
void setup(){
pinMode(ledPin, OUTPUT); //initialize the button led pin as a out put
lcd.begin(16,2); //set up the lcd's number of columns and rows
lcd.setCursor(0,0); //set the column and row of the lcd print
lcd.print("PBC:"); //print a message to lcd
}
void loop(){
buttonState = digitalRead(buttonPin); //read the button input pin
//compare the buttonState to its previous state
if (buttonState != lastButtonState){
//if the state has chaged, increment the counter
if (buttonState ==HIGH){
//if the current state is HIGH then the button went from off to on
buttonPushCounter ++;
lastButtonState = buttonState; //save the current state as the laststate
lcd.setCursor (0,1); //set the column and row of the lcd print
lcd.print (buttonPushCounter); //print the number of button presses
digitalWrite (ledPin, HIGH); // if the led current state is HIGH then the led went from off to on
}
else{
lastButtonState = buttonState; //save the current state as the laststate
digitalWrite (ledPin, LOW); // if the led current state is LOW then the led will be off
}
}
}
[/mw_shl_code]
各位大神看看,有什么错误,为什么我有时候按键的时候数字会跳 两下
|
|