Hi,
I am certain the subject indicates how new I am.
I have an UNO connected to a pc via usb.
I have just started playing with the serial parts of Arduino for the last couple of days. I am happy that I have been able to use Serial.read to allow me to open serial monitor, press a chosen keyboard key, hit enter and have it illuminate the led on pin 13 of an UNO. That is a big step for me. But I must hit "enter" in order to make that happen.
What I want to do next is to learn how to make the led turn on when chosen key is pressed(without pressing enter), stay on while the key is pressed, and go off when I release it.
I am almost embarrassed as this seems so simple, but I have no idea how to do this.
After you receive a character or key press, clear the input buffer.
Have the LED only on when there is a character in the receive buffer. I don't know what librarys you are using but that's the basic idea.
You can't do exactly what you want using the Arduino's serial monitor, because the serial monitor won't actually send anything to the Arduino unless/until you hit the return/enter key. If you just hold a keyboard key down, all you'll do is fill up a buffer on the PC.
What you're trying to do would require you to use a different serial client. If you have autorepeat enabled for your keyboard, and use a client that sends each character as it is entered (I think RealTerm supports that) then you would have a steady stream of characters sent to the Arduino as long as you hold the key down. On the Arduino side, you would just need to turn the LED on each time you received a character, and turn it off if you hadn't received the next character after a while.
Keep in mind that most serial applications only send data when the key is released. If you want to send one value when the key is pressed, and another value when the key is released, which seemed to be what you wanted, you will need to write your own application to do that.
I don't have a suitable sketch running in my UNO right now, so I'm not echo-ing what I send from PC with PuTTY, but I see the Uno Rx light solid on when I hold down any key.... so PuTTY seems to send without releasing the key.
That fits with what I remember from when I last drove my robot wirelessly with 2 Wixels: robot turned as long as I held the cursor left or right key down, far as I remember.
PaulS:
Keep in mind that most serial applications only send data when the key is released. If you want to send one value when the key is pressed, and another value when the key is released, which seemed to be what you wanted, you will need to write your own application to do that.
YMMV but my experience is the reverse. Typical behaviour is to send a character when the key is pressed, and if the key remains held down and autorepeat is enabled for the keyboard then after a short delay the character is repeated at the autorepeat rate.
then after a short delay the character is repeated at the autorepeat rate.
Yep that's what I saw with PuTTY by watching the Rx light: seems a single character is sent (one distinct blink of Rx), then a beat, then Rx light stays on while key held down.
Hi,
Thanks to all for the suggestions. I did just now install Putty, and have uploaded the switchCase2 example that comes with the latest IDE on my UNO as it was my first exposure to serial anything from an online tutorial. But I definitely will not be capable of writing my own serial applications anytime soon.
But after connecting some leds to the digital outs indicated in the sketch, and pressing the appropriate keys in Putty, the expected leds do indeed illuminate without the need to press enter. Likewise, holding the key down results in the expected led turning on, remaining on, and I get the consistent scrolling of the ASCII value of the key on the Putty screen, which to me indicates it is constantly sending the key press information. I realize that there is nothing in the default sketch to turn off the led after releasing the key, so it does not concern me that it does not turn off when the key is released, I will try to figure that out in the code. Also, I noticed that it does indeed turn on the led at key press, not key release.
So I consider it a tiny small victory, thanks to all of your advices!