[TriEmbed] [OBSOLETE trianglearduino:1095] Problem with AtTiny85 and buttons

Adam Haile email at adamhaile.net
Tue Jul 15 09:32:15 CDT 2014


Joe,

Easiest way to do the mentioned bias resistors is to use the one's built
into the AT chip :)
Every IO pin on the AT series chips have an internal pull-up resistor
ranging from 10k-50k ohm (depending on the chip model).

To enable it, you set the pin mode to input and then write a high bit to
it. Sounds weird, but that's just the way the resisters work:

pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH); //sets internal pull-up

Then, simply wire your button to "pull" that pin to ground. i.e. hook one
side of the button to buttonPin and the other to ground. When the button
closes the bias is now ground because it has the least resistance and your
micro will see LOW. See the following for more:
http://arduino.cc/en/Tutorial/DigitalPins

For debouncing, if you aren't too worried about not doing it the most
efficient way try this: http://playground.arduino.cc/code/bounce

At its simplest, you can do something like this:

uint8_t _oldState = HIGH;
uint8_t _curState = _oldState;
long bounceThreshold = 100;//wait 100ms, increase or decrease depending on
how sesitive you want the debouncing

_curState = digitalRead(buttonPin);
if(_curState != _oldState)
{
delay(bounceThreshold);
if(_curState == digitalRead(buttonPin))
 {
if(_curState)
{
// turn LED on:
 }
else
{
// turn LED off:
 }
_oldState = _curState;
}
}

It's pretty basic, but it gets the job done in most cases. Note, this
should make it so the LED is on only while the button is actually down.
It's not a toggle (press on/press off). To to that, in the if(_curSate)
section just flip a boolean (ledState != ledState) and then at the end of
that whole if statement do digitalWrite(ledPin, ledState);

There's many ways to skin this one... personally I use external interrupts
with buttons... but that's a whole other lesson. Though, side note. The
reason the internal bias resistors are pull-up is that the chips are set to
only be able to be woken from sleep when the external interrupt goes LOW.
You can configure the interrupts to react to LOW or HIGH, but being woken
from sleep is a special case and it looks like they simplified the chip
circuit by requiring that a wake be a LOW pulse.





On Tue, Jul 15, 2014 at 10:15 AM, Glen Smith <mrglenasmith at gmail.com> wrote:

> I'm guessing that you have 2 problems. One is due to your input switch
> bouncing. When a mechanical switch closes it does not simply change from
> false to true. For a few milliseconds (immediately for us slow humans but
> forever for a u-processor running this little bit of code) it bounces back
> & forth between the two states. Regardless of how fast or hard you push
> that switch, the Tiny85 will see many of these changes.
>
> The other thing you are running into is called a floating input. When you
> set the button as an input, you need to use a resistor to bias the input to
> return to a known level. Think of it like a lever, unless a spring pushes
> or pulls the lever to a known state, when you let go of it, the lever will
> simply stay where you left it. In the case of your input, you left it high,
> so the charge on the input slowly dissipates, and a second or so later the
> Tiny85 sees it as a low. These biasing resistors are called pull-up &
> pull-down resistors.
> On Jul 15, 2014 6:58 AM, "Bothari" <bothari at gmail.com> wrote:
>
>> Group,
>>
>> I've been learning about hardware for the last couple of months, but I've
>> come up against a problem with inputs.  I put together a little IR gun with
>> the AtTiny85, but I can't get the trigger to fire the gun.  I've reduced
>> the problem to one AtTiny85 with a resistor and LED, but I still don't
>> understand what's going on.
>>
>> With the below code (Blink from the examples) I can touch the #2 pin high
>> and the light on pin 3 comes on, but it stays on for like a second or so
>> before going off.  I think it should go off immediately.
>>
>> I added 3 blinks on the LED on startup to make sure I know when it
>> resets, and the fuse bits for the oscillator are set to 8mHz.  That works
>> as expected.
>>
>> Any ideas?
>> Joe
>>
>> const int buttonPin = 2;     // the number of the pushbutton pin
>> const int ledPin =  3;      // the number of the LED pin
>>
>> // variables will change:
>> int buttonState = 0;         // variable for reading the pushbutton status
>>
>> void setup() {
>>   // initialize the LED pin as an output:
>>   pinMode(ledPin, OUTPUT);
>>    digitalWrite(ledPin, LOW);
>>   // initialize the pushbutton pin as an input:
>>   pinMode(buttonPin, INPUT);
>>
>>   //Show that we rebooted
>>   digitalWrite(ledPin, HIGH);
>>   delay(300);
>>   digitalWrite(ledPin, LOW);
>>   delay(300);
>>
>>   digitalWrite(ledPin, HIGH);
>>   delay(300);
>>   digitalWrite(ledPin, LOW);
>>   delay(300);
>>
>>   digitalWrite(ledPin, HIGH);
>>   delay(300);
>>   digitalWrite(ledPin, LOW);
>>   delay(300);
>> }
>>
>> void loop(){
>>   // read the state of the pushbutton value:
>>   buttonState = digitalRead(buttonPin);
>>
>>   // check if the pushbutton is pressed.
>>   // if it is, the buttonState is HIGH:
>>   if (buttonState == HIGH) {
>>     // turn LED on:
>>     digitalWrite(ledPin, HIGH);
>>   }
>>   else {
>>     // turn LED off:
>>     digitalWrite(ledPin, LOW);
>>   }
>> }
>>
>> --
>> "There are only two industries that refer to their customers as ‘users’."
>> - Edward Tufte
>>
>>  --
>> NOTICE: This list is OBSOLETE. For details about the replacement TriEmbed
>> list go here:
>> http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "trianglearduino" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to trianglearduino+unsubscribe at googlegroups.com.
>> Visit this group at http://groups.google.com/group/trianglearduino.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> _______________________________________________
> Triangle, NC Embedded Computing mailing list
> TriEmbed at triembed.org
> http://mail.triembed.org/mailman/listinfo/triembed_triembed.org
> TriEmbed web site: http://TriEmbed.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.triembed.org/pipermail/triembed_triembed.org/attachments/20140715/aec00040/attachment.htm>


More information about the TriEmbed mailing list