Send keyboard strokes through computer to arduino

Hi,

I have not coded anything yet, and I have been searching on the web quite a lot with no luck. I don't know if it is possible or if I am not searching the correct thing.

I have and Arduino Mega 2560. Basically what I want to do is have my keyboard connected to my computer act like a piano. I want each key to represent a different note and when I press that key, it sends that certain command to my arduino, which then processes it and tells a speaker to play a certain note.

The only thing in particular that I require guidance and/or help on is getting the keyboard to send the commands through the computer, and to my arduino.

I know the serial monitor can do this quite easily, but I don't want to have to push enter every single time I want to play a note.

I have windows 7, 64-bit os.

Thank you for any help.

Why bother going thru the computer? Just go direct from a keypad to the arduino. I created a 13-note polyphony code example recently - search "playing notes with micros" How many notes are you interested in?

You can open a serial port for communication with either CreateFile or plain old fopen.

See this thread here: http://forum.arduino.cc/index.php?topic=178913.msg1327822#msg1327822 So, just wait for keystrokes on the PC, before sending them to the arduino with a call to WriteFile, fwrite or fprintf.

As for "why bother going through the computer?" Since you have a pc and an Arduino with the usb cable, then you've already got all of the hardware. Seems like a no brainer to me!

I’d opt for portability.
Can find all kinds of USB wallwarts to power the Mega

That’s what I used in my prototype, got a blue one that says “enercell” on it.

piano13keyBurstFix.ino (11.2 KB)

Raieth: Basically what I want to do is have my keyboard connected to my computer act like a piano.

Do you realise that you could do the whole thing entirely on a PC with far less effort? In fact you can probably find some freeware that does this with zero development effort.

PeterH: Do you realise that you could do the whole thing entirely on a PC with far less effort? In fact you can probably find some freeware that does this with zero development effort.

:roll_eyes: This question just seems wrong on so many levels.. People do things for all kinds of reasons.

Do you ask people at sporting events if they realize that they could have just watched it on the telly? :P

enhzflep:
:roll_eyes:
This question just seems wrong on so many levels… People do things for all kinds of reasons.

Do you ask people at sporting events if they realize that they could have just watched it on the telly? :stuck_out_tongue:

The OP didn’t say they wanted to solve the problem for the fun of it, or as a learning exercise. They said:

what I want to do is have my keyboard connected to my computer act like a piano

The solution being proposed is not the most sensible way to solve that problem, which is why I asked whether they had considered the more obvious solution. I don’t regard that question as wrong, I regard it as potentially saving the OP a considerable amount of time and effort if they were pursuing this approach out of ignorance. Perhaps they really do have some good reason for doing it this way (‘because I thought it would be fun’ is a perfectly good reason) but I wouldn’t assume that.

Indeed. You're absolutely correct - there was no mention of intent - be it exercise, learning or otherwise. I've clearly made assumptions here, as it appears you have.

For reference, there [u]are[/u] thousands of software-only midi solutions for the pc. "VST host freeware" returns 880,000 results in Google. But looking purely at the question title (never mind the content) - I feel these wouldn't be an appropriate suggestion to the question posed.

Anyway, thank-you for reminding me of the value of seeking clarification before making assumptions.:thumbs-up: Good day to you sir. :)

Thank you everyone for your responses.

Enhzflep, you are correct with what I am doing here. This is purely for experience and to have a little fun. I already have a real keyboard that I can play actual music on, and was just doing this for the learning experience and for the fun of it. The resource you gave me was great and was what I was looking for to getting started.

I thought about getting extra components to make it portable because I thought that would be cool but I am currently a poor college student. So I decided to save what money I had.

I have one more question. I have been looking at key events with visual c++ and commands such as processkeyeventargs but I am unsure of how to implement them. I find plenty of definitions of what they do, but cannot seem to figure out how they work within actual code.

Thank you for any help.

Although you're only a poor student, if you can afford a Mega perhaps you might consider saving up $25 for a USB host shield, which would make it possible to eliminate the PC and give you a standalone solution.

Raieth: Thank you everyone for your responses. ... I have one more question. I have been looking at key events with visual c++ and commands such as processkeyeventargs but I am unsure of how to implement them. I find plenty of definitions of what they do, but cannot seem to figure out how they work within actual code.

You're welcome, to give is to receive!

To be honest, I've never even looked at the functions you mention. I had been thinking of a simple console-app that just ran an infinite loop - i.e a while(1) type of construct. In it, I'd just call getch then send the result of that to the arduino. It'd be up to you if you checked for keystroke values on the arduino before translating them to notes, or if you converted the ascii code on the pc before sending a note number - like midi does. Just sending a byte with the value of the note would probably be my preference, since you just run the note number through a simple formula to get the required frequency. Something like this excerpt from my software synth perhaps:

pitch = 440.0f*pow(2.0f,(note-45.0f)/12.0f);

Where I generally use a range of [10..70] for note - I forget what the valid range is for midi, but my recollection is that I've used the same formula for translating a note number to an absolute frequency in Hz. (too lazy to check :grin:)

The other option would be to have a frame or dialog-based app, then just handle either the WM_KEYDOWN or WM_KEYUP messages from the main window. Perhaps you could even draw a piano keyboard and handle mouse-clicks by determining which 'key' you've clicked, before sending the value off to the mega. For an (html) example of the kind of interface I mean, try having a look at SoundToy - http://www.iquilezles.org/apps/soundtoy/ . I do note however, that it doesn't respond to mouse-clicks on the keys, only to keyboard strokes. But this is only an interface. The important thing is nabbing the keystrokes and getting the data to the beast. You can always make it look pretty when (if) you're inclined.

Nothing wrong with being cheap. (although the urban myth is now known to false) There was a wicked-funny joke about the millions of dollars spent by the US to make a pressurized biro (the Space Pen) that would work in space, in a zero-g environment, while the Russians simply took HB pencils.

While it only offers 'usb-client' functionality (no USB-host ability, apparently), V-USB is a (free) software only implementation. You can make a keyboard or mouse or joystick etc, etc HID with no hardware other than a usb socket and the arduino. You can control your mouse cursor or keyboard input with a couple of buttons connected to the mega. http://www.obdev.at/products/vusb/index.html