473,473 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

keyboard controlling arduino rc car with UDP packets (VB 2008)

1 New Member
Hi, this is my first post on these forums. Hope this is in the right place.

Doing a college project where we control an rc car wired into an Arduino over the network from a computer.

It's been brilliant doing this project and all the stuff I've learned from the electronics to the coding, and I've gotten pretty well as far as I thought I could with a lot of help from this forum and many others, so thanks for that.

Decided to do the coding in VB 2008 cos of how quick and easy it was to get things running with VB in first year, but there was no constraint on language choice, and if anyone thinks another language is better suited to this I'd love to hear thoughts on that too. Anyway, having not used VB in a couple of years this forum and a few others were invaluable for rooting out which methods and things I needed to put this together, couldn't have even gotten started without it. But I wanted to postpone posting for as long as possible and get as much done on my own as I could first, which I think I have.

I have a solution pretty much working, and I wanted to get more expert opinions on how it could be improved or implemented better (even if it should be scrapped and a different approach taken, I'm sure it's far from perfect).

Here's the skiinny:

VB app checks for held keys with GetAsyncKeyState, and various combos (of arrow keys) cause particular character to be sent in UDP packet to Arduino, which does it's thing based on the character in the packet. The held keys was chosen as the most intuitive method of controlling an rc car from the pc, and I'm using a timer's tick to run the GetAsyncKeyState, and what I suspect might be a somewhat convoluted system of variable comparisons to prevent a packet from being sent until the state of the held keys changes.
UDP was chosen for speed, though I'm really not sure what difference it would make in the real world if trying to control the rc over the internet say with live keyboard control, and whether it would be viable with UDP and not an alternative, or whether it wouldn't make a practical difference, or whether it wouldn't be viable at all!

Anyway, the thing is working, and sending packets only on initial press or release of key(s). The one glitch I've so far detected is that when releasing all the keys, which should send the "z" character and stop the car, the same character that was previously sent is re-sent, rather than the 'stop' character "z".

So here's the code, VB 2008:

__________________________________________________ ____

Expand|Select|Wrap|Line Numbers
  1. Imports System.Text
  2. Imports System.Net
  3. Imports System.Net.Sockets
  4.  
  5. Public Class Form1
  6.  
  7.     Dim GLOIP As IPAddress
  8.     Dim GLOINTPORT As Integer
  9.     Dim bytCommand As Byte() = New Byte() {}
  10.     Dim udpClient As New UdpClient
  11.     Dim controlChoice As New Integer
  12.     Dim controlChoicePrev As New Integer
  13.     Dim cc As String = "z"
  14.  
  15.  
  16.  
  17.     Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
  18.     Dim WithEvents Tmr As New Timer
  19.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.         controlChoice = 0
  21.         controlChoicePrev = controlChoice
  22.  
  23.         Tmr.Interval = 100
  24.         Tmr.Start()
  25.     End Sub
  26.     Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
  27.         Tmr.Stop()
  28.  
  29.         If Not controlChoice = controlChoicePrev Then
  30.  
  31.             'UDP_______________________________________________________
  32.  
  33.             Try
  34.                 GLOIP = IPAddress.Parse("192.168.1.177")
  35.                 GLOINTPORT = 8888
  36.                 udpClient.Connect(GLOIP, GLOINTPORT)
  37.                 bytCommand = Encoding.ASCII.GetBytes(cc)
  38.                 udpClient.Send(bytCommand, bytCommand.Length)
  39.  
  40.             Catch ex As Exception
  41.                 Console.WriteLine(ex.Message)
  42.  
  43.             End Try
  44.  
  45.             'UDP_______________________________________________________
  46.  
  47.         End If
  48.  
  49.  
  50.         If GetAsyncKeyState(Keys.Up) Or GetAsyncKeyState(Keys.Down) Then
  51.  
  52.  
  53.             Select Case True
  54.  
  55.                 Case GetAsyncKeyState(Keys.Up)
  56.  
  57.                     If (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Left)) Then
  58.                         Label1.Text = "Forwards Left"
  59.                         controlChoicePrev = controlChoice
  60.                         controlChoice = 11
  61.                         cc = "b"
  62.  
  63.                     ElseIf (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Right)) Then
  64.                         Label1.Text = "Forwards Right"
  65.                         controlChoicePrev = controlChoice
  66.                         controlChoice = 12
  67.                         cc = "c"
  68.  
  69.                     Else
  70.                         Label1.Text = "Forwards"
  71.                         controlChoicePrev = controlChoice
  72.                         controlChoice = 10
  73.                         cc = "a"
  74.  
  75.                     End If
  76.  
  77.  
  78.                 Case GetAsyncKeyState(Keys.Down)
  79.  
  80.  
  81.  
  82.  
  83.                     If (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Left)) Then
  84.                         Label1.Text = "Backwards Left"
  85.                         controlChoicePrev = controlChoice
  86.                         controlChoice = 21
  87.                         cc = "e"
  88.  
  89.                     ElseIf (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Right)) Then
  90.                         Label1.Text = "Backwards Right"
  91.                         controlChoicePrev = controlChoice
  92.                         controlChoice = 22
  93.                         cc = "f"
  94.  
  95.                     Else
  96.                         Label1.Text = "Backwards"
  97.                         controlChoicePrev = controlChoice
  98.                         controlChoice = 20
  99.                         cc = "d"
  100.  
  101.                     End If
  102.  
  103.             End Select
  104.  
  105.         Else
  106.             Label1.Text = "Stopped"
  107.             controlChoicePrev = controlChoice
  108.             controlChoice = 0
  109.  
  110.         End If
  111.  
  112.         Tmr.Start()
  113.  
  114.     End Sub
  115. End Class
__________________________________________________ _______





So like I said, I'm just wondering if anyone would comment on this and make suggestions/crits.

Many TIA

PS. I'm still pretty code illiterate, so if layman's terms and really explicit and comprehensive and well commented code snippets is not too big an ask... like when people start using terms like call and pass, invoke, etc, I can just about follow things but not always. Sorry...
Mar 20 '13 #1
1 3009
IronRazer
83 New Member
Hey kickpuncher,
I hope you don`t mind but, i rewrote some of your code and got it so it does not send the key again on release and so that when all keys are released it will send z once to stop the car. I think that`s what you where looking for.

Expand|Select|Wrap|Line Numbers
  1. Option Strict On
  2. Imports System.Text
  3. Imports System.Net
  4. Imports System.Net.Sockets
  5.  
  6. Public Class Form1
  7.  
  8.     Dim GLOIP As IPAddress
  9.     Dim GLOINTPORT As Integer
  10.     Dim bytCommand As Byte() = New Byte() {}
  11.     Dim udpClient As New UdpClient
  12.     Dim cc As String = "z"
  13.     Dim cclast As String = ""
  14.  
  15.     Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
  16.     Dim WithEvents Tmr As New Timer
  17.  
  18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  19.         Tmr.Interval = 100
  20.         Tmr.Start()
  21.     End Sub
  22.  
  23.     Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
  24.         Tmr.Stop()
  25.  
  26.         If CBool(GetAsyncKeyState(Keys.Up) Or GetAsyncKeyState(Keys.Down)) Then
  27.  
  28.             Select Case True
  29.  
  30.                 Case CBool(GetAsyncKeyState(Keys.Up))
  31.  
  32.                     If CBool((GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Left))) Then
  33.                         Label1.Text = "Forwards Left"
  34.                         cc = "b"
  35.  
  36.                     ElseIf CBool((GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Right))) Then
  37.                         Label1.Text = "Forwards Right"
  38.                         cc = "c"
  39.  
  40.                     Else
  41.                         Label1.Text = "Forwards"
  42.                         cc = "a"
  43.  
  44.                     End If
  45.  
  46.                 Case CBool(GetAsyncKeyState(Keys.Down))
  47.  
  48.                     If CBool((GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Left))) Then
  49.                         Label1.Text = "Backwards Left"
  50.                         cc = "e"
  51.  
  52.                     ElseIf CBool((GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Right))) Then
  53.                         Label1.Text = "Backwards Right"
  54.                         cc = "f"
  55.  
  56.                     Else
  57.                         Label1.Text = "Backwards"
  58.                         cc = "d"
  59.  
  60.                     End If
  61.  
  62.             End Select
  63.  
  64.         ElseIf Not cc = "z" And Not CBool(GetAsyncKeyState(Keys.Down)) And Not CBool(GetAsyncKeyState(Keys.Up)) Then
  65.             cc = "z"
  66.             Label1.Text = "Stopped"
  67.         End If
  68.  
  69.         If Not cclast = cc Then
  70.             'UDP______________________________________________ _________
  71.  
  72.             Try
  73.                 GLOIP = IPAddress.Parse("192.168.1.177")
  74.                 GLOINTPORT = 8888
  75.                 udpClient.Connect(GLOIP, GLOINTPORT)
  76.                 bytCommand = Encoding.ASCII.GetBytes(cc)
  77.                 udpClient.Send(bytCommand, bytCommand.Length)
  78.  
  79.             Catch ex As Exception
  80.                 Console.WriteLine(ex.Message)
  81.  
  82.             End Try
  83.  
  84.             'UDP______________________________________________ _________
  85.         End If
  86.  
  87.         cclast = cc
  88.  
  89.         Tmr.Start()
  90.  
  91.     End Sub
  92.  
  93. End Class
Also just a Tip you should use this line as the very first line of code in the program.
Expand|Select|Wrap|Line Numbers
  1. Option Strict On
It will catch all the errors of type conversion. By placing it in the code it will put a little blue line with a red spot on the end under each error. Then you can put your mouse over the red spot and it will pop up a little red box. You can then click on the red box and it will tell you how to fix the error. It is a very very handy option to use.
Mar 20 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ralf Toender | last post by:
Hi! Does anybody know how to send a keystroke C#? In C++ it's done by: keybd_event ( ... ) or SendInput ( ... ). What namespace does include this counterpart? Thanks Ralf
2
by: ApexData | last post by:
Hello In order to control keyboard keystrokes in my application, I use the KeyPreview=Yes, OnKeyDown Events of every Form with great success. HOWEVER, these events are not available in REPORTS,...
5
by: jaco.versfeld | last post by:
Hi There, I have a basic TCP client and TCP server in C++. The TCP client connects to the server, and after a setup phase starts to transmit a file to the TCP server using multiple packets...
9
by: Kbalz | last post by:
I have an application that minimizes itself, and I want it to listen for certain key commands, and when they are pressed, my program can react to them. So far I've gotten my application to react...
11
by: vbguy2008 | last post by:
Hi, I am coding a Windows Form Application in VB.NET 2008. I would like to clear the keyboard buffer or at least empty all outstanding key presses queued up for my application at certain points...
3
by: mazdotnet | last post by:
Hi guys, I'm thinking of buying a laptop time this time and I was wondering if anyone has any experience with running VisualStudio 2008 and MSSQL 2005 on a laptop? Please include your processor,...
5
by: MikeB | last post by:
I have a simple form that I'm using to learn something about HTML and JavaScript. Can I associate a button on the form with a keyboard key so that when the user presses the keyboard key the form...
8
by: BD | last post by:
How can I duplicate the behavior of the operating system shortcut keys in my application? For example, my windows form has 5 controls (textboxes), the operating system will pickup which control...
4
by: id.engg | last post by:
logFileName = 'log.txt' logfile = open(logFileName, "a") class MyUDPServer(SocketServer.UDPServer): def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 8388608)...
1
by: Bhanuchander | last post by:
I am develop a RemoteDiskTop Controlling Application.In that App, i done using ip address client computer keyboard ,mouse disabled(using MFC ).but i dont know to enable those things from the server.
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.