Go Down

Topic: Use keyboard to send commands to arduino thru PC (Read 33734 times) previous topic - next topic

Xenosis

Dec 28, 2010, 10:03 pm Last Edit: Dec 28, 2010, 10:06 pm by Xenosis Reason: 1
I am ultimately trying to create a set up whereby I can connect up the arduino either via usb or ethernet to a PC and have the user/client use arrow keys or other select buttons to send command signals to the arduino, which will operate servo motors or whatever.

Basically,
How can I send keyboard command signals from a PC to the arduino board?
How can I interpret those command signals in code for the software on board the arduino as one type of input or another?

I'm pretty sure that if I get those two things nailed down I will be able to do the rest.

Any help setting it up and or advice as to what might be the easiest route to accomplishing that goal would be appreciated.

Thanks in advance

prestondocks

Hi Mate,

I am using VB.net, there is very good free version called (Visual Basic 2010 Express).

Just start a new windows form project and add a serial connection object.

Then a few lines of code opens the serial port and sends commands via the usb serial interface.

Hope this helps.

prestondocks

Just a few lines of code below to show how easy it is with VB.net
You have to add the serialport object from the tools panel to the form first.

When you receive the code on the Arduino, it will come through as numeric char values
Code: [Select]

SerialPort1.Open()
SerialPort1.Write(ScreenName & Message)


When you receive the code on the Arduino, it will come through as numeric char values, so you want to process what has been received have to read it as follows
Code: [Select]

while (Serial.available() > 0){
       SerialInput.concat(char(Serial.read()));
}

Notice the char() command. This casts the numeric character values into alphanumeric characters.

Ofcourse if you going to simply pump it direct to an LCD screen you can just send what you received with lcd.write().

Xenosis

Thanks a bunch for the info, I'll give it a go soon as I have some time.

Xenosis

Ok so I installed visual basic express 2010 and created a windows form application project and add a serialport component to it, but its just a blank form.

I am a beginner with this stuff and could use some more direction.

I am also confused by what the entire arduino code would be that i would have.

PaulS

Quote
I am a beginner with this stuff and could use some more direction.

Do you have keyboard events registered? You'll need some to know when a key is pressed or released.

When the event fires, you can determine which key was pressed, and send that key (or something else) to the serial port.

As for the Arduino, the code there depends on what you expect the Arduino to do in response to the incoming serial data.

Xenosis

How would I go about registering a select few keyboard events? just the arrow keys.

thats the idea though.

PaulS

You don't register events for specific keys. You register callbacks that happen when specific events occur. When the keyPressed or keyReleased events occur, the specific key being pressed or released is passed to the function.

You decide, based on the key that was pressed, what to do - write it to serial, ignore it, do a happy dance, etc.

Xenosis

thanks that is helpful information to know. any way you could make a small code snippet that does just that?

i understand how the process works a lot better than i can write it.

PaulS

Code: [Select]
     Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
           If(e.KeyCode == Keys.Left)
                 'Left arrow key was pressed
           Else if(e.KeyCode == Keys.Right)
                 'Right arrow key was pressed
           End If
     End Sub

     Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
           If(e.KeyCode == Keys.Down)
                 ' Down arrow was released
           End If
     End Sub


I presume you know how to set up a serial port connection.

Xenosis

so this is what i have so far.



i can successfully use the arduino code and the integrated serial monitor to send characters that display on an LCD. COM4 happens to be the serial/usb interface for the arduino board.

PaulS

Quote
I presume you know how to set up a serial port connection.

So, this was not a valid assumption?

marklar

This post may help on the serial side.  
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1293049236

* Note: this is not my code and I have not tested the code in this post.  I just ran by it a few days back so I figured I'd pass it along.  Good Luck.

Xenosis

nope, not a valid assumption. like i said, i am a beginner haha.

ill take a look at that code too, thanks marklar

Go Up