go away spammer

Author Topic: Equipping $50 bot with simple pushbutton  (Read 2376 times)

0 Members and 1 Guest are viewing this topic.

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 153
  • Helpful? 6
Equipping $50 bot with simple pushbutton
« on: November 14, 2010, 12:27:52 PM »
I recently made the $50 robot and so far its worked well.  I've added some things to it; LEDs, a 7 segment display, an on/off switch, etc.  Now I want to equip it with a simple pushbutton that I can use to trigger actions.  I went out and bought a simple button from radioshack that will mount great on my bots chassis.  I know all I have to do as far as electronics go is connect one end to +5V, through a resistor (I have some 330ohm and some 1.62kohm laying around, would either of these be a good value?), through the button, and into an analog pin.  My problem is in the programming portion.  Do I just need to call a2dConvert8bit() on the port the button will be connected to?  Then what would be the returned value with the button pressed and not pressed?  Would it return 0 if the button is not pressed?

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Equipping $50 bot with simple pushbutton
« Reply #1 on: November 14, 2010, 01:50:39 PM »
Hi,

This really belongs in software.

I know all I have to do as far as electronics go is connect one end to +5V, through a resistor (I have some 330ohm and some 1.62kohm laying around, would either of these be a good value?), through the button, and into an analog pin.  My problem is in the programming portion.  Do I just need to call a2dConvert8bit() on the port the button will be connected to?  Then what would be the returned value with the button pressed and not pressed?  Would it return 0 if the button is not pressed?
You don't need an A/D input for that. Either the button is pressed or it ain't - quite binary.
Although it would work, 1k6 is a bit low and 5..10k would be more suitable to get the current down, but if you're OK with wasting a bit over 3mA, then connect one end of it to +5V and the other end to a digital input line. The button/switch goes from that line to ground.

You can either use an interrupt to read the switch, or you can poll it at regular intervals (that may mean holding the button down a bit longer, depending on how often you poll).
Reading the switch sitting on line L into a variable B would look something like:
(pseudo code - look up the exact syntax in whatever language you use)
  B = INPUT(L)
Now B will be either 0 or 1, depending on the switch status.

Another way of using it would be like
  IF INPUT(L)=1 THEN
    do something if button is not pressed
  END IF

or

  WHILE INPUT(L)=0
     do something until button is pressed
  END WHILE

Remember that it will read 0 when pressed and 1 otherwise. You could invert that by swapping around resistor and button, but that means running a positive wire to the chassis (which is hopefully grounded), with a slightly greater risk of shorting stuff.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 153
  • Helpful? 6
Re: Equipping $50 bot with simple pushbutton
« Reply #2 on: November 14, 2010, 02:18:31 PM »
Terribly sorry about it being in the wrong section.

Quote
You don't need an A/D input for that.
I was under the impression that digital ports could only be used for output, and analog ports could only be used for input.  Like I said, I'm very new at this and just finished the $50 bot.

Quote
then connect one end of it to +5V and the other end to a digital input line. The button/switch goes from that line to ground.
Just to make sure I understand fully, there will be a resistor between +5V and a digital pin (one of the pins over there in the bunch that the servos are connected too) and the button will be between the same digital pin that the resistor is connected to, and gnd?

So the current will flow from +5V, through the resistor, and then reach a point where it splits and half goes to the digital pin, while the other half goes to the button and finally back to gnd?  That's a little different from what I was going to do.  Glad I checked here first.  Don't want to melt my (as of now) only atmega8!

What I was going to do was connect one end to +5V and go straight through the resistor, button, and into the pin on the atmega.  I knew that didn't seem quite right.

As for the programming, I will be doing it all in C.

Quote
(pseudo code - look up the exact syntax in whatever language you use)
In SoR_Utils.h I found this:

Quote
//define port functions; example: PORT_ON( PORTD, 6);
#define PORT_ON( port_letter, number )         port_letter |= (1<<number)
#define PORT_OFF( port_letter, number )         port_letter &= ~(1<<number)
#define PORT_ALL_ON( port_letter, number )      port_letter |= (number)
#define PORT_ALL_OFF( port_letter, number )      port_letter &= ~(number)
#define FLIP_PORT( port_letter, number )      port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number )      ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number )      !( port_letter & (1<<number) )

So would the correct syntax to get the bot to do nothing until the button is pressed be:
while(PORT_IS_OFF(PORTD,3))
{
  //do something (or more likely, nothing)
}

Offline rbtying

  • Supreme Robot
  • *****
  • Posts: 452
  • Helpful? 31
Re: Equipping $50 bot with simple pushbutton
« Reply #3 on: November 14, 2010, 05:17:47 PM »
Digital ports are also know as GPIO (general purpose input/output).  You can set them to input, and it will be either HIGH (logical 1) or LOW (logical 0).  Thus digital pins read binary.  Analog pins are slightly different - when they do input, instead of binary, they will give you something between say, 0 and 1024. 

The resistor is a pulldown - it goes from the GPIO pin to ground.  The switch goes from Vcc (+5v) to the GPIO pin.  Thus, when the switch is not pressed the pin will read LOW, and when you press the switch, it will read HIGH.  (better EE people, correct me if I'm wrong)

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 153
  • Helpful? 6
Re: Equipping $50 bot with simple pushbutton
« Reply #4 on: November 14, 2010, 07:58:52 PM »
So can I set PORTD, 3 to be used as input by opening SoR_Utils.h and changing from:
Quote
//configure ports for input or output - specific to ATmega8
void configure_ports(void)
   {
   DDRC = 0x00;  //configure all C ports for input
   PORTC = 0x00; //make sure pull-up resistors are turned off
   DDRD = 0xFF;  //configure all D ports for output
   DDRB = 0xC7;  //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')
   }

to:
Quote
//configure ports for input or output - specific to ATmega8
void configure_ports(void)
   {
   DDRC = 0x00;  //configure all C ports for input
   PORTC = 0x00; //make sure pull-up resistors are turned off
   DDRD = 0xF7;  //F7 being hex for 0b11110111
   DDRB = 0xC7;  //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')
   }

Edit: Nothing seems to work.  I put a 1.6kohm resistor between +5V and the digital pin, and the button between the pin and gnd, but I just can't get it to read the port.  I think my problem is in getting the ports set to input/output properly.  I even tried to change the two pins that my servos are on to input just in hopes that something would change (the servos should have stopped working if the pins were set to input, right?), but no.  Even when I do DDRD = 0x00;, the servos still operate like they always have.

Also, how should the pull up resistors be set for the pin I'm using for the button?
« Last Edit: November 14, 2010, 10:52:12 PM by VegaObscura »

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 153
  • Helpful? 6
Re: Equipping $50 bot with simple pushbutton
« Reply #5 on: December 06, 2010, 01:25:50 AM »
Let me make sure that I understand how this button circuit is supposed to be working.

Basically, the point is to make a voltage divider like this one:


Where Ra is the 1k6 resistor, Rb is the button (either near infinite resistance or near 0 resistance), and Vout is the microcontroller, right?

This way, when the button is NOT pressed, this means infinite resistance across Rb and therefore a full 5V across its leads, so 5V going into the digital pin on the microcontroller.  -Button not pressed, signal high

When the button IS pressed, the resistance on Rb goes to near zero, and so almost 100% of the voltage drop happens across the 1k6 resistor, and there is 0V across the leads of the button, and therefore 0V going into the microcontroller.  - Button pressed, signal low

But all this is not accounting for the internal resistance of the microcontroller.  Does this even matter?

Am I understanding all of this correctly?

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 153
  • Helpful? 6
Re: Equipping $50 bot with simple pushbutton
« Reply #6 on: December 13, 2010, 08:13:31 PM »
I've been doing some reading on the topic of pull-up resistors and came to a conclusion.

Since the atmega8 has internal pull-up resistors that can be turned on or off for input pins, shouldn't connecting a button be as simple as setting the pull-up resistors on and connecting the button between the input pin and gnd?

Basically the internal pull-up resistor is just taking the place of the external resistor that I was going to be using?

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,944
  • Helpful? 99
Re: Equipping $50 bot with simple pushbutton
« Reply #7 on: December 13, 2010, 10:19:07 PM »
Quote
Basically the internal pull-up resistor is just taking the place of the external resistor that I was going to be using?
That is correct and one reason they are a feature in many uContollers.

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 153
  • Helpful? 6
Re: Equipping $50 bot with simple pushbutton
« Reply #8 on: December 13, 2010, 10:25:15 PM »
So am I correct in saying that all I have to do on the hardware side of things is connect the leads of the button between gnd and the digital pin?

 


Get Your Ad Here

data_list