Pages: [1]   Go Down
Author Topic: Increment and decrement with two push buttons using LCD and arduino Uno  (Read 7619 times)
0 Members and 1 Guest are viewing this topic.
Raleigh, NC.
Offline Offline
Full Member
***
Karma: 0
Posts: 129
Addicted to Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Guys,

I am trying to configure first an Up and down or increment and decrement control for a display to show a "Volume" increasing by one or decreasing by one....I have attached my schematic and code...I can get the "Up" count working but then I tried some  "--" in my code and it didn't work..Please help....

Here is my code:


#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 buttonState = 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 input pin:
   
   buttonState = digitalRead(buttonPin);
   
   
 
  // compare the buttonState to its previous state
 
   if (buttonState != lastButtonState) {
     
     // if the state has changed, increment the counter
     
     if (buttonState == HIGH)
     
     {
       
      buttonPushCounter++;
      lcd.setCursor(7,1);
     
      lcd.print(buttonPushCounter);
     }
     

 
   }
   // save the current state as the last state,
  //for next time through the loop
   lastButtonState = buttonState;
 

}


schematic attached


* Up_Down.png (16.69 KB, 717x523 - viewed 474 times.)
Logged

Raleigh, NC.
Offline Offline
Full Member
***
Karma: 0
Posts: 129
Addicted to Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just looked at my schematic......the pull up resistors are tied to high but they are connected to GND here....disregard that mistake....
Logged

Raleigh, NC.
Offline Offline
Full Member
***
Karma: 0
Posts: 129
Addicted to Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please somebody help.....I have been racking my brain over how easy this is suppose to be but everything I try doesn't work
Logged

Venezuela
Offline Offline
God Member
*****
Karma: 16
Posts: 515
Ground, ground, always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If I were you, I would add a Serial.println() for every LCD.print() you have in your code plus the counter and state registers. If after test your volume buttons (dec/inc) everything goes right serially, then you have a hardware issue. These are just some ideas.
Logged

Raleigh, NC.
Offline Offline
Full Member
***
Karma: 0
Posts: 129
Addicted to Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just can't get over how easy this seems but its kicking my ass......I mean, I have the BUtton state and all that...and with the ++ it works great incrementing...I even took the wire from the other button I'm trying to use as the 'Down counter" and it make the count go up...so somewhere in my code there needs to have some "--" decrementing, but I can't figure it out...hardware is good...its my code I need to fix....somebody out there has got to know..
Logged

Venezuela
Offline Offline
God Member
*****
Karma: 16
Posts: 515
Ground, ground, always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:
Code:
#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;
   
   
}
Logged

0
Offline Offline
Sr. Member
****
Karma: 7
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Raleigh, NC.
Offline Offline
Full Member
***
Karma: 0
Posts: 129
Addicted to Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
   
   
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi .. i'm a newb


i tried this code ... on my arduino (gift) and it seems to go beyond  the "OFF" & "MAX"

how do i make it count from OFF to 5(MAX)  ... because this code its prints  OFF and MAX on display .. but he goes beyond that with -1 -2 -3 -4 -5 ..and when i press my +button ... i have to go from -2 or how many times i pushed the botton after OFF

and for MAX the same ..goes after my  if (buttonPushCounter >= 5)

AND i want to modify this code to control a blinking led ...

something like :
if OFF = closed led
if 1    = 1/min
if 2    = 2/min
if 3    = 3/min
if MAX= 4/min

+ to display this


PS i don't have any coding knowledge  sry  smiley-razz but im triend to learn but i got this 2 days ago ..i i dont want to trow it away because i dont know how this is done
I have browsed the forum for something similar ..cant find anything ...




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;
   
   
}
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 47
Posts: 1544
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This appears to be a new member picking up an old (> 6 months) thread.
The quote was unnecessary however.

@siko:
You need to learn some more basics.
If you have a range you want to use, you are the one to tell your Arduino how to handle that range.
So if off is 0, then do not allow the value to go below 0, and if on = 255 do not allow the value to be more than that.
Or if the value is already "out of range", force it back in range before you do anything else with it.
There are multiple ways of handling this.
Everything you need for that is already in the sketch, use your imagination to extend it to fit your needs.
(You just need to add 2 lines).
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

New York City, NY
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Arduino Rocks!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

New questions..
I need to limit the range of the values to between 0 and 127. Basically  two pairs of up/down counters, where two button take the values up and two buttons take the values down, one pair per value.  At the moment I'm having trouble once I go below 1. the up button then starts from double digits e.g. 11 or 21.
I was able to get to MAX 127, cleared the unused characters with
Quote
lcd.print("     ");
but comming up from -1 causes a problem. Also when coming down from 127 I get up to 999 after 100 coming down. So clearly this isn't the best way of doing this. Like I said before the larger project for which this intended has no delay (is a musical instrument) and the timer prescaler isn't the default ,in order to maximize the analog reads. So  "delay's" are no good.  Can I poll the button pins, put the values 0-127 in an array and do the increment/deincrement that way and what might that code look like?    
Logged

================================
http://twit.tv/floss61
Think different
Think UbuntuStudio
============================

Pages: [1]   Go Up
Jump to: