coding 2 pushbuttons

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
MILLERRRR
 
Posts: 4
Joined: Sat Oct 08, 2011 1:37 am

coding 2 pushbuttons

Post by MILLERRRR »

hello
I've basically wired up the shift register circuit from the experimenters guide, but added 2 pushbuttons. One will manually clock in data, at a rate of "j", and the other button controls what value "j" becomes.
with this code ive written, the button to control the clocking in of data works, but the button to control the value of j does not. After various physical testing to see if a wiring/mechanical problem was the culprit ive decided the problem must be in the coding, although i do not understand why. Here is the section of code i have to deal with the buttons

Code: Select all

void loop(){
  val = digitalRead(inputpin);
  delay(10);
  val2 = digitalRead(inputpin);
  if (val == val2) {
    if (val != buttonstate){
      if (val == LOW){
        i = i+j;
        Serial.println(i);
      }
    }
  }

  buttonstate = val;

    val3 = digitalRead(inputpin2);
    delay(10);
    val4 = digitalRead(inputpin2);
    if (val3 == val4) {
      if (val3 != buttonstate2){
        if (val == LOW){
          j = j+1;
          Serial.println(j);
        }
      }
    }

    buttonstate2=val3;
all variables are declared earlier, I just remember reading to post the section of code where the troubles are at. If more information is needed I can provide!

User avatar
adafruit_support_bill
 
Posts: 88136
Joined: Sat Feb 07, 2009 10:11 am

Re: coding 2 pushbuttons

Post by adafruit_support_bill »

Code: Select all

          if (val3 != buttonstate2){
            if (val == LOW){
              j = j+1;
              Serial.println(j);
            }
          }
I think that second 'val' should be a 'val3'.

MILLERRRR
 
Posts: 4
Joined: Sat Oct 08, 2011 1:37 am

Re: coding 2 pushbuttons

Post by MILLERRRR »

gah! thank you, knew it would be something overlooked from copy-paste!

User avatar
baldengineer
 
Posts: 127
Joined: Sun Sep 26, 2010 11:49 pm

Re: coding 2 pushbuttons

Post by baldengineer »

Realizing you were copying code AND doing an exercise, this is still a good example of why descriptive variable names are useful. Instead of "val1, val2, etc" or "button1, button2, etc" names like "upButton" or "resetButton" are much more descriptive.

Then when you read through code, you're more likely to see silly mistakes like that.

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

Return to “Arduino”