Newbie help needed increment/deincrement button values

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
pitchoilcan
 
Posts: 23
Joined: Tue Apr 24, 2012 10:18 am

Newbie help needed increment/deincrement button values

Post by pitchoilcan »

Hello all, I've managed to cobble together from examples I found online a sketch to manipulate two values with four buttons and print their status to LCD. Now 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. And for the large project the delays aren't usable so i'm thinking "else".conditions may be needed. The "StatechangeDection" Example is starting to look like a better option.
Here's the code as it is now:

Code: Select all

#include <LiquidCrystal.h>

LiquidCrystal lcd(7,8,9,10,11,12);
const int  buttonPin2 = 3;    // the pin that the Up pushbutton is attached to
const int  buttonPin3 = 4;    // the pin that the Down pushbutton is attached to
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 button2PushCounter = 0;   // counter for the number of button presses
int buttonState3 = 0;         // current state of the button
int buttonState4 = 0;         // current state of the button
int lastButton2State = 0;     // previous state of the button
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);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT); 

  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("PadCutOff:");
  lcd.setCursor(0,1);
  lcd.print("MaxPlTime:");   
}


void loop() {
  // read the pushbutton up input pin:
  buttonState3 = digitalRead(buttonPin3);
  // compare the buttonState to its previous state
  if (buttonState3 != lastButton2State) {
    // if the state has changed, increment the counter
    if (buttonState3 == HIGH)
    {
      button2PushCounter++;
      lcd.setCursor(10,1);

      lcd.print(button2PushCounter);
    }

    delay(50);

  }
  // save the current state as the last state,
  //for next time through the loop   
  lastButton2State = buttonState3;
  // read the pushbutton down input pin:
  buttonState4 = digitalRead(buttonPin2);
  // compare the buttonState to its previous state
  if (buttonState4 != lastButton2State) {
    // if the state has changed, decrement the counter
    if (buttonState4 == HIGH)
    {
      --button2PushCounter;
      lcd.setCursor(10,1);
      lcd.print(button2PushCounter);
    }
    delay(50); 
  }
  // 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(10,0);
      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;
      lcd.setCursor(10,0);
      lcd.print(buttonPushCounter);
    }
    delay(50);    
    if (buttonPushCounter < 126)
    {
      lcd.setCursor(13,0);
      lcd.print("   ");
    }
    if (buttonPushCounter <= 0)
    {
      lcd.setCursor(13,0);
      lcd.print("OFF");
      buttonPushCounter=0;
    }
    if (buttonPushCounter >= 126)
    {
      lcd.setCursor(13,0);
      lcd.print("Max");
      buttonPushCounter=126;
    }
  }
  if (button2PushCounter < 126)
  {
    lcd.setCursor(13,1);
    lcd.print("   ");
  }
  if (button2PushCounter <= 0)
  {
    lcd.setCursor(13,1);
    lcd.print("OFF");
    button2PushCounter=0;
  }
  if (button2PushCounter >= 126)
  {
    lcd.setCursor(13,1);
    lcd.print("Max");
    button2PushCounter=126;
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState6; 
  lastButton2State = buttonState4; 
}
 (pardon the lack of excerpt -newbie. Will trim in the future but this way you/I might be better able to identify problems)
Any suggestions ?
Thank you in advance.
Last edited by pitchoilcan on Thu Mar 13, 2014 11:38 am, edited 1 time in total.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Newbie help needed increment/deincrement button values

Post by adafruit_support_mike »

It looks like you're missing a close bracket in this section:

Code: Select all

   if (buttonState6 != lastButtonState) {
     // if the state has changed, decrement the counter
     if (buttonState6 == HIGH)
     {
     --buttonPushCounter;
      lcd.setCursor(10,0);
      lcd.print(buttonPushCounter);
     }
          delay(50);    
I think you want another close bracket before the 'delay()' line.

Take a quick pass through the code and clean up the indentation.. the only reason we use it is to make problems like that easier to see.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”