Leonardo 2 questions

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
astronomyteacher
 
Posts: 6
Joined: Fri Aug 05, 2011 1:29 pm

Leonardo 2 questions

Post by astronomyteacher »

1. Am attempting to use a magnetic reed switch as a substitute for the pushbutton in the pushbutton example. Will it need to be debounced?


2. In the Keyboard Message example, the comments say
>The circuit:
>* pushbutton attached from pin 2 to +5V
>* 10-kilohm resistor attached from pin 4 to ground

that doesn't make sense to me. Why am I attaching pin 4 to ground with a resistor when it is never initialized or used in the sketch? Shouldn't this be 10 K resistor from pin 2 to ground to keep it from reading spurious values when the button is not being pressed?

Thanks.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Leonardo 2 questions

Post by adafruit_support_bill »

1. Yes. Reed switches are subject to bounce as well.
2. Can you provide a link to the specific example you are using?

astronomyteacher
 
Posts: 6
Joined: Fri Aug 05, 2011 1:29 pm

Re: Leonardo 2 questions

Post by astronomyteacher »

Here is the link in the example sketch that came with my Arduino 1.01 download:
http://www.arduino.cc/en/Tutorial/KeyboardButton

However the link says the page does not exist, so here is the sketch.

Thanks


/*
Keyboard Button test

Sends a text string when a button is pressed.

The circuit:
* pushbutton attached from pin 2 to +5V
* 10-kilohm resistor attached from pin 4 to ground

created 24 Oct 2011
modified 27 Mar 2012
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/KeyboardButton
*/

const int buttonPin = 2; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
int counter = 0; // button push counter

void setup() {
// make the pushButton pin an input:
pinMode(buttonPin, INPUT);
// initialize control over the keyboard:
Keyboard.begin();
}

void loop() {
// read the pushbutton:
int buttonState = digitalRead(buttonPin);
// if the button state has changed,
if ((buttonState != previousButtonState)
// and it's currently pressed:
&& (buttonState == HIGH)) {
// increment the button counter
counter++;
// type out a message
Keyboard.print("You pressed the button ");
Keyboard.print(counter);
Keyboard.println(" times.");
}
// save the current button state for comparison next time:
previousButtonState = buttonState;
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Leonardo 2 questions

Post by adafruit_support_bill »

Yes. Your analysis was correct. You should have the pulldown resistor from pin 2 to ground.

If you want a good button tutorial, lesson 5 of Lady Ada's Arduino tutorials covers buttons, pullups, pulldowns & debouncing: http://www.ladyada.net/learn/arduino/lesson5.html

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”