Page 1 of 1

Sketch to get a button to print a letter

Posted: Thu Jun 20, 2013 9:41 am
by mikebe
Hi there,

I'm a complete novice at arduino & sketches but am fairly competent at electronics, computer stuff and software.
I have the leostick set up correctly and have uploaded the blink sketch successfully.

I'm looking for a sketch that will print a letter on the computer when I press a button connected to the leostick.

At the moment I believe I have the correct wiring to achieve this:

5V going to the A side of the button
GND going B side of the button through a 10k resistor
Digital pin 13 going to the B side of the button

I've based this on this tutorial: http://arduino.cc/en/tutorial/button

What I need is a sketch that I can compile and upload that will simply print the letter A when the button is pressed. I've scoured the internet and just can't find anything!
Any help would be greatly appreciated!
Thanks in advance,
Mike

Re: Sketch to get a button to print a letter

Posted: Sun Jun 23, 2013 12:05 pm
by mikebe
Hi tronixstuff,

thanks for the links, I just tried a few sketches and still couldn't get it to work.

I'm not sure how to convert the example link you posted into what I want, it seems to print a password, not too sure how to convert that script into just one letter. Also there seems to be nothing connected to 5V in that example?
As I said I'm a complete novice with sketches and a lot of that stuff is way over my head!

The first link you posted looks promising, I've tried combining the example button sketch in my first post with the example KeyboardWrite sketch from that link and still couldn't get it to work.

The wiring is still the same as in my first post and this is the sketch I came up with:

Code: Select all

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 13;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

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) {     
    //Send an ASCII 'A',
    Keyboard.write(65);
  }
}
What am I doing wrong? Any advice you could give me would be amazing!

Thanks again,

Mike

Re: Sketch to get a button to print a letter

Posted: Sun Jun 23, 2013 12:12 pm
by mikebe
Oh and also,

I forgot to mention, the red LED is slowly blinking at the moment but if I hold down the button the LED stays on constantly.

Not sure if that helps?

Thanks again,

Mike

Re: Sketch to get a button to print a letter

Posted: Sun Jun 23, 2013 11:16 pm
by angusgr
Hi Mike,

tronixstuff will probably have more to add here, but a couple of quick tips:

- In your code you'll need a line with "Keyboard.begin();" in the setup() section, like the example, to set the Keyboard up before you start using it.

- The LED is going red when you press the button because it's connected to the same pin (#13) as you're using for the button. The fact it's blinking probably means that your program isn't uploading successfully onto the board, so it's still running the Blink sketch. Do you get any errors in the Arduino IDE when you try to upload?

- Angus

Re: Sketch to get a button to print a letter

Posted: Thu Jul 04, 2013 9:39 am
by mikebe
Hi guys,

I've been super busy the past couple of weeks and have only just come back to this project, thanks for the replies!
Do you get any errors in the Arduino IDE when you try to upload?
Yes I did get an error, the progress bar goes almost all the way up but then it says it can't find the leostick! This is the error message I get, any ideas?

Code: Select all

Binary sketch size: 4,782 bytes (of a 28,672 byte maximum)
processing.app.debug.RunnerException: Couldn’t find a Leonardo on the selected port. Check that you have the correct port selected.  If it is correct, try pressing the board's reset button after initiating the upload.
	at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:152)
	at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:67)
	at processing.app.Sketch.upload(Sketch.java:1671)
	at processing.app.Sketch.exportApplet(Sketch.java:1627)
	at processing.app.Sketch.exportApplet(Sketch.java:1599)
	at processing.app.Editor$DefaultExportHandler.run(Editor.java:2380)
	at java.lang.Thread.run(Thread.java:680)
Try the code below, it sends "A" then waits a second when you press the button connected to D6:
Tronixstuff thanks for the code! When I try to verify / compile the sketch though I get this error:

Code: Select all

sketch_jul04a.ino: In function 'void setup()':
sketch_jul04a:4: error: 'INPUT_PULLUP' was not declared in this scope
sketch_jul04a:5: error: 'class Keyboard_' has no member named 'begin'
Thanks for all the help you've given me so far, any ideas on how to sort out these issues?

Re: Sketch to get a button to print a letter

Posted: Fri Jul 05, 2013 10:27 am
by mikebe
Hi Tronixstuff,

I had the board profile installed already and there's no slashes in front of #define HID_ENABLED

Any other ideas? Thanks for your help,

Mike

Re: Sketch to get a button to print a letter

Posted: Sun Jul 07, 2013 1:34 am
by mikebe
Hi Tronixstuff,

the first sketch I tried is in a previous post, it's this:

Code: Select all

    // constants won't change. They're used here to
    // set pin numbers:
    const int buttonPin = 13;     // the number of the pushbutton pin

    // variables will change:
    int buttonState = 0;         // variable for reading the pushbutton status

    void setup() { 
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT);     
    }

    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) {     
        //Send an ASCII 'A',
        Keyboard.write(65);
      }
    }
This sketch can be verified just fine, I also tried the sketch you posted however I got that error message when trying to verify/compile.

Any time I upload a sketch the progress bar goes almost all the way up then it says it can't find the leostick and try pressing reset on the board. I usually do this and the board keeps blinking as if it still has the blink sketch on there.

I think that's all the symptoms, let us know if that gives you any clues!

Thanks very much,

Mike

Re: Sketch to get a button to print a letter

Posted: Tue Jul 09, 2013 6:27 am
by mikebe
Hi there,

I'm running a mac, it still doesn't seem to want to upload the sketch!

Any ideas?

Re: Sketch to get a button to print a letter

Posted: Tue Jul 09, 2013 9:47 am
by mikebe
Strange - no it won't even upload the blink sketch, it gets almost all the way there but then

Code: Select all

Binary sketch size: 4,320 bytes (of a 28,672 byte maximum)
processing.app.debug.RunnerException: Couldn’t find a Leonardo on the selected port. Check that you have the correct port selected.  If it is correct, try pressing the board's reset button after initiating the upload.
	at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:152)
	at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:67)
	at processing.app.Sketch.upload(Sketch.java:1671)
	at processing.app.Sketch.exportApplet(Sketch.java:1627)
	at processing.app.Sketch.exportApplet(Sketch.java:1599)
	at processing.app.Editor$DefaultExportHandler.run(Editor.java:2380)
	at java.lang.Thread.run(Thread.java:680)

Re: Sketch to get a button to print a letter

Posted: Wed Jul 10, 2013 9:36 am
by mikebe
Hi there, I don't have another arduino!
Are you saying it's a problem with the sketch? Because It doesn't seem like any other sketch works either :(
Is there anything else I can do without another arduino to get any sketch working?