Go Down

Topic: Increment and decrement with two push buttons using LCD and arduino Uno (Read 9 times) previous topic - next topic

Palliser

Try this:
Code: [Select]
#include <LiquidCrystal.h>


LiquidCrystal lcd(7,8,9,10,11,12);



const int  buttonPin = 5;    // the pin that the Up pushbutton is attached to

const int  buttonPin1 = 6;    // the pin that the Down pushbutton is attached to


// Variables will change:


int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState5 = 0;         // current state of the button
int buttonState6 = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup() {


  // initialize the button pin as a input:
   
   pinMode(buttonPin, INPUT);
   
   pinMode(buttonPin1, INPUT);
   
   
   lcd.begin(16,2);
   
   lcd.setCursor(0,1);
   
   lcd.print("Volume:");
   
}


void loop() {


   // read the pushbutton up input pin:
   
   buttonState5 = digitalRead(buttonPin);

  // compare the buttonState to its previous state

   if (buttonState5 != lastButtonState) {
     
     // if the state has changed, increment the counter
     
     if (buttonState5 == HIGH)
     
     {
       
      buttonPushCounter++;
      lcd.setCursor(7,1);
           
      lcd.print(buttonPushCounter);
     }
     
     

   }
   // save the current state as the last state,
  //for next time through the loop
   lastButtonState = buttonState5;
   
     // read the pushbutton down input pin:
   
   buttonState6 = digitalRead(buttonPin1);

  // compare the buttonState to its previous state

   if (buttonState6 != lastButtonState) {
     
     // if the state has changed, decrement the counter
     
     if (buttonState6 == HIGH)
     
     {
       
      buttonPushCounter--;
      lcd.setCursor(7,1);
         
      lcd.print(buttonPushCounter);
     }
     
             
   }
   // save the current state as the last state,
  //for next time through the loop
   lastButtonState = buttonState6;
   
   
}

P18F4550

Quote
"--" decrementing, but I can't figure it out


instead of -- use -=1 or -=2 or any decrement you like, sam for incrementing +=1

Aaron_dyer

Thanks to both of you....I used both of the information given...my code now is shown below...


Its working...I just have to tweak it a little with some delays etc...



#include <LiquidCrystal.h>


LiquidCrystal lcd(7,8,9,10,11,12);



const int  buttonPin = 5;    // the pin that the Up pushbutton is attached to

const int  buttonPin1 = 6;    // the pin that the Down pushbutton is attached to


// Variables will change:


int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState5 = 0;         // current state of the button
int buttonState6 = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup() {


  // initialize the button pin as a input:
   
   pinMode(buttonPin, INPUT);
   
   pinMode(buttonPin1, INPUT);
   
   
   lcd.begin(16,2);
   
   lcd.setCursor(0,1);
   
   lcd.print("Volume:");
   
}


void loop() {


   // read the pushbutton up input pin:
   
   buttonState5 = digitalRead(buttonPin);

  // compare the buttonState to its previous state

   if (buttonState5 != lastButtonState) {
     
     // if the state has changed, increment the counter
     
     if (buttonState5 == HIGH)
     
     {
       
      buttonPushCounter++;
      lcd.setCursor(7,1);
           
      lcd.print(buttonPushCounter);
     }
     
      delay(50);

   }
   // save the current state as the last state,
  //for next time through the loop
   lastButtonState = buttonState5;
   
     // read the pushbutton down input pin:
   
   buttonState6 = digitalRead(buttonPin1);

  // compare the buttonState to its previous state

   if (buttonState6 != lastButtonState) {
     
     // if the state has changed, decrement the counter
     
     if (buttonState6 == HIGH)
     
     {
       
      buttonPushCounter-=1;
      lcd.setCursor(7,1);
         
      lcd.print(buttonPushCounter);
     }
     
         delay(50);   
         
         
         if (buttonPushCounter < 10)
         
         {
             lcd.setCursor(8,1);
         
              lcd.print("  ");
         }
         
         
         if (buttonPushCounter <= 0)
         
         {
           
           
            lcd.setCursor(7,1);
           
            lcd.print("OFF");
           
         }
         
         
         
         if (buttonPushCounter >= 25)
         
         {
           
           lcd.setCursor(7,1);
           
           lcd.print("Max");
           
         }
         
       
   }
   
   
   // save the current state as the last state,

  //for next time through the loop

   lastButtonState = buttonState6;
   
   
}

Nick Gammon

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.


Please use code tags in future, it makes your code easier to follow.

Nick Gammon

Also the double-spacing is distracting. This isn't an essay.

Go Up