Problem with AtTiny85 and buttons

181 views
Skip to first unread message

Bothari

unread,
Jul 15, 2014, 6:58:27 AM7/15/14
to triangl...@googlegroups.com
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

Bothari

unread,
Jul 15, 2014, 7:29:41 AM7/15/14
to triangl...@googlegroups.com
After looking at it some more, My pin #2 was floating, which made the state indeterminate.  This makes for some odd problems to track down.

So now my question is:  How do I use the internal pullup resistors to avoid this problem?  I thought I solved it with
 pinMode(buttonPin, INPUT);

but that only works if I have a resistor between pin #2 and ground.  I had not gone back to explicitly set it.

Right now the example works fine if I use
pinMode(buttonPin, INPUT_PULLUP);     

and trigger pin #2 by touching it to ground with a resistor or a wire.  I suspected the problem around the input pin weeks ago, but I couldn't figure it out.

Is there a better explanation of pull up and pull down resistors than this?  It's not really helping.

Thx, 
Joe

David Trent

unread,
Jul 15, 2014, 8:57:02 AM7/15/14
to triangl...@googlegroups.com

My best suggestion would be to include the led going low after a short time out in the loop. While this may lead to a slight blink in the led while the button's pressed, you can controlling how long it stays on from each test.

Otherwise you need to throw some logic in there to check if it's high already before setting it high some more. Debounce. It could be stacking 20 high commands up in the queue.

Void
If button on and led off
    led on
Elseif button off and led on
    led off
End loop

--
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 triangleardui...@googlegroups.com.
Visit this group at http://groups.google.com/group/trianglearduino.
For more options, visit https://groups.google.com/d/optout.

Andy Barnhart

unread,
Jul 15, 2014, 9:00:12 AM7/15/14
to triangl...@googlegroups.com
Queue? For digitalRead? I think it is the current state.

Pete Soper

unread,
Jul 15, 2014, 9:02:35 AM7/15/14
to triangl...@googlegroups.com
As an FYI this list was replaced by the TriEmbed email list (visit http://triembed.org and follow the link to the email subscription page). TriEmbed meets this Saturday at Splatspace and every 2nd Monday night at NCSU. See the web page for more details.

(But the folks with admin privileges for trianglearduino left and stopped responding to email, so it's just floating like a ghost ship on the Internet. If I can remember how to do it I'll change the "OBSOLETE" msg subject prefix to "OBSOLETE: USE http://triembed.org INSTEAD")

-Pete

Bothari

unread,
Jul 15, 2014, 9:08:27 AM7/15/14
to triangl...@googlegroups.com
Thanks for the heads up.  This address must have been from a while ago.  I'll delete it.
Have a good trip,
Joe

Ron Craig

unread,
Jul 15, 2014, 9:54:45 AM7/15/14
to triangl...@googlegroups.com, bot...@gmail.com
Hi, Joe.

Here's a section from my class that goes over pullup and pulldowns.  I hope it helps.
Yes, your investigation is right, you've got a combination of pulldown resistor and debounce issues to deal with.  These sections of the class handout deal with both. 
A link to the Word document with the pulldown and debounce material:
https://www.dropbox.com/sh/l3a8zq3l4rn6cxb/AACx9UhPlwYrd2pTG4YFFLiPa
Reply all
Reply to author
Forward
0 new messages