LCD 计数器-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5805|回复: 2

LCD 计数器

[复制链接]
发表于 2013-2-9 08:49 | 显示全部楼层 |阅读模式
一下是编程
[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]


各位大神看看,有什么错误,为什么我有时候按键的时候数字会跳 两下
发表于 2013-2-12 21:30 | 显示全部楼层
八成是因为你的按钮机械杂波干扰,滤波即可。

硬件解决:按钮并联一个0.1u的小电容。
软件解决:在23行之后加一个delay(100)。
 楼主| 发表于 2013-2-13 03:22 | 显示全部楼层
电网 发表于 2013-2-12 21:30
八成是因为你的按钮机械杂波干扰,滤波即可。

硬件解决:按钮并联一个0.1u的小电容。

多谢啦,问题解决
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-28 00:43 , Processed in 0.157846 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表