can I use a momentary button on a analog pin?

can I use a momentary button on a analog pin on a Arduino mega 2560. I have seen how to use a pot on a analog pin to change the led's on the bar graph lab and I would like to change the pot to two momentary buttons one to move to the next pin up and one to move to the next pin down. How would I hook it up and how do I need to code it?

can I use a momentary button on a analog pin on a Arduino mega 2560.

Why? Are you out of digital pins?

I will be using 53 led's in the bar graph. I am making something like a score bored for a peg game.

The analog pins can be used as digital pins, starting with 54 and going up (or A0 to A15).

techd:
can I use a momentary button on a analog pin on a Arduino mega 2560. I have seen how to use a pot on a analog pin to change the led's on the bar graph lab and I would like to change the pot to two momentary buttons one to move to the next pin up and one to move to the next pin down. How would I hook it up and how do I need to code it?

It sounds like you don't need analog at all.

In the lab you had a pot hooked up and a bar graph led hooked up to Arduino, both separate? You read the pot then wrote to the bar graph? The only link is the code running in the AVR.

So now you want to replace the pot with 2 buttons? Does that sound hard?

so I will still do the 5v and a 220 ohm on one leg of the button and the input pin (Ao-A15) on the other leg of the button? will I need to change the code any other and declaring the analog pin as a in not a out?

You can use pullup pins with buttons, the resistors are built into the AVR chip. For quick work just plug jumpers into the pin holes and to press a button, touch it to the USB jack housing which is GND.

The pin is set to INPUT and HIGH. The HIGH comes through 20k-50k. When it is not grounded it reads HIGH, when it is grounded it reads LOW. That's the opposite of 5V-->resistor-->button-->pin.

so I will still do the 5v and a 220 ohm on one leg of the button and the input pin (Ao-A15) on the other leg of the button?

Why are you adding a resistor? Use the internal pullup resistors. Connect one leg of the switch to the digital pin (A0, for instance) and one leg to ground. Declare that A0 is an INPUT and enable the pullup resistor.

will I need to change the code any other and declaring the analog pin as a in not a out?

Yes. You won't be using analogRead(). You will be using digitalRead(), just like the switch was on the other side of the board.

simple button code that does not require external resistors.

//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Why are you adding a resistor?

I thought I had to use a resistor on the power pin to make it a digital high like in the Digital I/O learning.

Nope. Not using the pullup resistor. The pullup uses less power. The logic is just reversed.

I thought I had to use a resistor on the power pin to make it a digital high like in the Digital I/O learning.

All the words make sense. The order does not. There is no context for much of it. Some links might be useful.

the Examples > Digital I/O

is what I was going by.

Making the pin a pullup and connecting to ground through a switch is simpler and requires fewer parts and less work. There is more than one way to skin a cat, not just the way you were first shown.

Literally, my test button is a jumper wire and the USB jack housing.

Rather than use 50+ pins for individual LEDs you might also want to investigate LED drivers or shift registers to allow you to run all your LEDs with only a few Arduino pins.