Pages: [1]   Go Down
Author Topic: can I use a momentary button on a analog pin?  (Read 1169 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
can I use a momentary button on a analog pin on a Arduino mega 2560.
Why? Are you out of digital pins?
Logged

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4671
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

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

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4671
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

simple button code that does not require external resistors.

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

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4671
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

the Examples > Digital I/O
http://arduino.cc/en/Tutorial/Button
is what I was going by.
« Last Edit: April 13, 2013, 11:02:22 pm by techd » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 91
Posts: 4671
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

 
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

UK
Offline Offline
God Member
*****
Karma: 17
Posts: 568
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: