Pages: [1]   Go Down
Author Topic: Help with Leonardo  (Read 598 times)
0 Members and 1 Guest are viewing this topic.
NL
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I just started with experimenting and trying out the code, etc.
But when I try to use a button or emulate a key it goes into a loop.
It keeps sendig the message.

I just used the standard available code:

Code:
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;
}
« Last Edit: July 05, 2012, 02:25:25 pm by Danton » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27089
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

We're not mind readers.
Post your code using the # button abvove.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Upstate NY
Offline Offline
Full Member
***
Karma: 2
Posts: 184
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are missing a lot of the code that you need. 

Where are you getting the code from?  Please supply a link.
Logged

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

You are missing a lot of the code that you need. 

Where are you getting the code from?  Please supply a link.

That would be funny smiley-razz.

http://arduino.cc/en/Tutorial/KeyboardMessage
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is your button wired with a pull-up or pull-down resistor?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

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

I build my setup just like the schematics.
But it also sends the commands to my Mac even when I don't have a setup and only the Leonardo connected and running the code. This is not normal in my opinion, but then again, I just started with Arduino.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But it also sends the commands to my Mac even when I don't have a setup and only the Leonardo connected and running the code. This is not normal in my opinion, but then again, I just started with Arduino.
Yes this is normal.

Input pins that are not tied to 5V or GND are called "floating."  Floating pins will randomly read HIGH or LOW.  So when your Leonardo is connected without the button and pull-down resistor shown in the schematic, it will random print keyboard characters. 

If you do not wire the button exactly as the schematic shows (with pull-down resistor), you will not get reliable operation.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

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

For this specific tutorial  the diagram calls for a 220 ohm resistor but the sketch mentions it's a 10-kilohm.
Also the sketch has a led interface using pin12 that suppose to turn off when the switch is pressed, but the schematic
doesn't reflect this connection.

Also the Link on the Sketch is wrong
IS:
http://www.arduino.cc/en/Tutorial/KeyboardButton

S/B:
http://arduino.cc/en/Tutorial/KeyboardMessage

IS this Sketch the correct one?
Logged

Pages: [1]   Go Up
Jump to: