Go Down

Topic: can I use a momentary button on a analog pin? (Read 1 time) previous topic - next topic

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?

PaulS

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

Why? Are you out of digital pins?

techd

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

PaulS

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

GoForSmoke


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?

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

techd

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?

GoForSmoke

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.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

PaulS

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

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

zoomkat

simple button code that does not require external resistors.

Code: [Select]

//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
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

techd

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

GoForSmoke

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


Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

PaulS

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

techd

#12
Apr 14, 2013, 04:20 am Last Edit: Apr 14, 2013, 06:02 am by techd Reason: 1
the Examples > Digital I/O
http://arduino.cc/en/Tutorial/Button
is what I was going by.

GoForSmoke

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.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

tack

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.

Go Up