[操作疑難] arduino 新手問題

小弟想整一個Arduino  two button number counter 而count number要有上下限(eg.上限100,下限30咁) 但係上網搵過冇類似有上下限既source, ,小弟個code暫時做到由0數到65535,希望各位ching幫幫小弟,感激不盡!
  1. #include <LiquidCrystal_I2C.h>
  2. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  3. const int  buttonPin = 5;   
  4. const int  buttonPin1 = 4;
  5. unsigned int buttonPushCounter = 0;   
  6. int buttonState5 = 0;     
  7. int buttonState6 = 0;
  8. int lastButtonState = 0;

  9. void setup() {
  10.    pinMode(buttonPin, INPUT);
  11.    pinMode(buttonPin1, INPUT);
  12.    lcd.begin(16,2);
  13.    lcd.setCursor(0,1);
  14.    lcd.print("Volume:");
  15. }


  16. void loop() {
  17.    buttonState5 = digitalRead(buttonPin);
  18.    
  19.    if (buttonState5 != lastButtonState){  
  20.      
  21.      if (buttonState5 == HIGH){
  22.       
  23.       buttonPushCounter++;
  24.       lcd.clear();
  25.       lcd.setCursor(0,1);
  26.       lcd.print("Volume:");
  27.       lcd.setCursor(7,1);
  28.       lcd.print(buttonPushCounter);   
  29.      }
  30.    }
  31.    lastButtonState = buttonState5;
  32.    delay(100);

  33.    buttonState6 = digitalRead(buttonPin1);
  34.    if (buttonState6 != lastButtonState){   
  35.     if (buttonState6 == HIGH){
  36.       buttonPushCounter--;
  37.       lcd.clear();
  38.       lcd.setCursor(0,1);
  39.       lcd.print("Volume:");
  40.       lcd.setCursor(7,1);   
  41.       lcd.print(buttonPushCounter);
  42.      }        
  43.    }
  44.    lastButtonState = buttonState6;
  45.    delay(100);
  46. }
複製代碼

更改3行code:
#5      unsigned int buttonPushCounter = 30;   
#26    if (buttonPushCounter <100)  buttonPushCounter++;
#40    if (buttonPushCounter >30)  buttonPushCounter--;

TOP

回覆 2# Charcoal99


    唔該晒c hing,得左

TOP