Dear all,
Hello there! I'm a beginner on Arduino and also a newer in this forum.
I'm currently working on a project and now I'm stuck on the button adjustment function. Within the following code, I can make a counter, countering up and down, from a range of 0-999. If the figure is 0 or 999, the screen will directly show 'OFF' or 'MAX'. However, without the delay() function in the main loop (cuz it will delay the whole program and I don't want to use it), the figures will increase/decrease a lot within one press. I need a accurate adjustment system, which means all I need is to count once up/down within one press. That is, from the time I press till release, I the counter will only count for one no matter how long I pressed!!!
Here is the code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(22,24,26,28,30,32);
const int buttonPinDown = 5;  // down
const int buttonPinUp = 6;  // up
//with an inverting schmitt trigger, so in the code ButtonDown will in
a increase loop while the ButtonUp in a decrease loop
// Variables will change:
int buttonPushCounter = 0;Â // counter up/down
int buttonState5 = 0;Â Â Â Â // current state button down
int buttonState6 = 0;Â Â Â Â // current state button up
int lastButtonState = 0;Â Â // previous state of the button
void setup() {
 // initialize the button pin as a input:Â
 pinMode(buttonPinDown, INPUT);Â
 pinMode(buttonPinUp, INPUT);  Â
 lcd.begin(16,2);Â
 lcd.setCursor(0,1);Â
 lcd.print("Volume:0");Â
}
void loop() {
 // read the pushbutton down input pin:Â
 buttonState5 = digitalRead(buttonPinDown);
 // compare the buttonState to its previous state
 if (buttonState5 != lastButtonState) { Â
  // if the state has changed, decrement the counter Â
  if (buttonState5 == LOW && buttonPushCounter<999)   Â
  buttonPushCounter=buttonPushCounter+1;Â
  lcd.setCursor(7,1);
  lcd.print(buttonPushCounter);
  lastButtonState = buttonState5;
   //delay(150);
 }
   Â
 // read the pushbutton up input pin:Â
 buttonState6 = digitalRead(buttonPinUp);
 // compare the buttonState to its previous state
 if (buttonState6 != lastButtonState) { Â
  // if the state has changed, increment the counter Â
  if (buttonState6 == LOW && buttonPushCounter>0)
   buttonPushCounter=buttonPushCounter-1;
   lcd.setCursor(7,1);
   lcd.print(buttonPushCounter);
   lastButtonState = buttonState6;
   //delay(150);  Â
 }Â
Â
Â
//Limitations addedÂ
    if (buttonPushCounter < 10)   Â
    {
      lcd.setCursor(8,1);    Â
       lcd.print(" ");
       //Serial.print(" ");
    }
    if (buttonPushCounter < 100)   Â
    {
      lcd.setCursor(9,1);   Â
       lcd.print(" ");
       //Serial.print(" ");
    }   Â
    if (buttonPushCounter < 1)   Â
    { //buttonPushCounter=0;         Â
      lcd.setCursor(7,1);     Â
      lcd.print("OFF");
      //Serial.print("OFF");
      buttonPushCounter=0;Â
    }            Â
    if (buttonPushCounter >998)   Â
    { //buttonPushCounter=999;    Â
     lcd.setCursor(7,1);     Â
     lcd.print("MAX ");
     //Serial.print("MAX");
     buttonPushCounter=999;
    }Â
    if (buttonPushCounter <1000)   Â
    {
       lcd.setCursor(10,1);   Â
       lcd.print(" ");
       //Serial.print(" ");
    }
}
Anyone can help????
Many thanks!!!!
Sharon.