Robofun 機器人論壇

標題: LCD 計數器問題 [打印本頁]

作者: vincent8034    時間: 2014-12-30 11:10
標題: LCD 計數器問題
本帖最後由 vincent8034 於 2014-12-30 11:13 編輯

我是arduino新手
我設計一個用按鈕來計數的程式,數字是從0>9,9以上就跳回0但我想在加入一個清除的按鈕,就是我在途中(0到9之間)按一個鈕直接跳回0
請問改如何改
以下是我的程式:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int pushButton = 7;
int x = 0;


LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
        pinMode(pushButton, INPUT);


        lcd.begin();


        lcd.backlight();
        lcd.setCursor(0,0);


}

void loop()
{

  int i;
  char a1,a2;
  i = digitalRead(pushButton);
  if(i == HIGH)
  {
    x++;
    a1=x%10;
    a2=(x/10)%10;
    lcd.setCursor(1,0);
    lcd.write(a1+'0');
    lcd.setCursor(0,0);
    lcd.write(a2+'0');

    if (x>=10) x=0;


  }
  Serial.print(x);
  delay(100);
}

作者: ates1687    時間: 2014-12-30 13:52
回復 1# vincent8034


   

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
int pushButton = 7;
int x = 0;


LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
        pinMode(pushButton, INPUT);


        lcd.begin();


        lcd.backlight();
        lcd.setCursor(0,0);


}

void loop()
{

  int i;
  char a1,a2;
  i = digitalRead(pushButton);
  if(i == HIGH)
  {
    x++;
    a1=x%10;
    a2=(x/10)%10;
    lcd.setCursor(1,0);
    lcd.write(a1+'0');
    lcd.setCursor(0,0);
    lcd.write(a2+'0');

    if (x==10){
   
    x=0;
  
    }
  Serial.print(x);
  delay(100);
}

看修改成紅色那3行看看.....





歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2