Click here to Skip to main content
15,891,513 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Concurrency violation or Insert Pin
IgaBaro28-Mar-13 3:11
IgaBaro28-Mar-13 3:11 
AnswerRe: Concurrency violation or Insert Pin
Eddy Vluggen28-Mar-13 7:15
professionalEddy Vluggen28-Mar-13 7:15 
QuestionHow make steganography in video (.AVI) Pin
Khairul Imam25-Mar-13 11:42
Khairul Imam25-Mar-13 11:42 
AnswerRe: How make steganography in video (.AVI) Pin
GuyThiebaut25-Mar-13 22:45
professionalGuyThiebaut25-Mar-13 22:45 
QuestionI want to do all the functionalty of painting on an image?? Pin
Lolo Hyun25-Mar-13 6:55
Lolo Hyun25-Mar-13 6:55 
AnswerRe: I want to do all the functionalty of painting on an image?? Pin
Richard MacCutchan25-Mar-13 7:39
mveRichard MacCutchan25-Mar-13 7:39 
AnswerRe: I want to do all the functionalty of painting on an image?? Pin
Eddy Vluggen25-Mar-13 8:06
professionalEddy Vluggen25-Mar-13 8:06 
Generalkeyboard controlling arduino rc car with UDP packets (VB 2008) Pin
Jose Sallamanca20-Mar-13 8:59
Jose Sallamanca20-Mar-13 8:59 
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:



VB
Imports System.Text
Imports System.Net
Imports System.Net.Sockets

Public Class Form1

    Dim GLOIP As IPAddress
    Dim GLOINTPORT As Integer
    Dim bytCommand As Byte() = New Byte() {}
    Dim udpClient As New UdpClient
    Dim controlChoice As New Integer
    Dim controlChoicePrev As New Integer
    Dim cc As String = "z"



    Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
    Dim WithEvents Tmr As New Timer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        controlChoice = 0
        controlChoicePrev = controlChoice

        Tmr.Interval = 100
        Tmr.Start()
    End Sub
    Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
        Tmr.Stop()

        If Not controlChoice = controlChoicePrev Then

            'UDP_______________________________________________________

            Try
                GLOIP = IPAddress.Parse("192.168.1.177")
                GLOINTPORT = 8888
                udpClient.Connect(GLOIP, GLOINTPORT)
                bytCommand = Encoding.ASCII.GetBytes(cc)
                udpClient.Send(bytCommand, bytCommand.Length)

            Catch ex As Exception
                Console.WriteLine(ex.Message)

            End Try

            'UDP_______________________________________________________

        End If


        If GetAsyncKeyState(Keys.Up) Or GetAsyncKeyState(Keys.Down) Then


            Select Case True

                Case GetAsyncKeyState(Keys.Up)

                    If (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Left)) Then
                        Label1.Text = "Forwards Left"
                        controlChoicePrev = controlChoice
                        controlChoice = 11
                        cc = "b"

                    ElseIf (GetAsyncKeyState(Keys.Up)) And (GetAsyncKeyState(Keys.Right)) Then
                        Label1.Text = "Forwards Right"
                        controlChoicePrev = controlChoice
                        controlChoice = 12
                        cc = "c"

                    Else
                        Label1.Text = "Forwards"
                        controlChoicePrev = controlChoice
                        controlChoice = 10
                        cc = "a"

                    End If


                Case GetAsyncKeyState(Keys.Down)




                    If (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Left)) Then
                        Label1.Text = "Backwards Left"
                        controlChoicePrev = controlChoice
                        controlChoice = 21
                        cc = "e"

                    ElseIf (GetAsyncKeyState(Keys.Down)) And (GetAsyncKeyState(Keys.Right)) Then
                        Label1.Text = "Backwards Right"
                        controlChoicePrev = controlChoice
                        controlChoice = 22
                        cc = "f"

                    Else
                        Label1.Text = "Backwards"
                        controlChoicePrev = controlChoice
                        controlChoice = 20
                        cc = "d"

                    End If

            End Select

        Else
            Label1.Text = "Stopped"
            controlChoicePrev = controlChoice
            controlChoice = 0

        End If

        Tmr.Start()

    End Sub
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...
AnswerRe: keyboard controlling arduino rc car with UDP packets (VB 2008) Pin
MicroVirus27-Mar-13 4:11
MicroVirus27-Mar-13 4:11 
Questionremove or disconnect disk/sotrage Pin
V1rus9r20-Mar-13 3:51
V1rus9r20-Mar-13 3:51 
AnswerRe: remove or disconnect disk/sotrage Pin
Richard MacCutchan20-Mar-13 4:04
mveRichard MacCutchan20-Mar-13 4:04 
GeneralRe: remove or disconnect disk/sotrage Pin
V1rus9r20-Mar-13 6:40
V1rus9r20-Mar-13 6:40 
QuestionAccess VBA Timing Problem Pin
Ingo19-Mar-13 2:12
Ingo19-Mar-13 2:12 
AnswerRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 3:37
mveDave Kreskowiak19-Mar-13 3:37 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 4:41
Ingo19-Mar-13 4:41 
GeneralRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 7:18
mveDave Kreskowiak19-Mar-13 7:18 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 7:25
Ingo19-Mar-13 7:25 
GeneralRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 8:10
mveDave Kreskowiak19-Mar-13 8:10 
GeneralRe: Access VBA Timing Problem Pin
Ingo20-Mar-13 0:03
Ingo20-Mar-13 0:03 
AnswerRe: Access VBA Timing Problem Pin
GuyThiebaut19-Mar-13 5:04
professionalGuyThiebaut19-Mar-13 5:04 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 6:49
Ingo19-Mar-13 6:49 
GeneralRe: Access VBA Timing Problem Pin
GuyThiebaut19-Mar-13 7:21
professionalGuyThiebaut19-Mar-13 7:21 
GeneralRe: Access VBA Timing Problem Pin
Ingo19-Mar-13 7:28
Ingo19-Mar-13 7:28 
GeneralRe: Access VBA Timing Problem Pin
Dave Kreskowiak19-Mar-13 8:09
mveDave Kreskowiak19-Mar-13 8:09 
AnswerRe: Access VBA Timing Problem Pin
Maciej Los19-Mar-13 6:54
mveMaciej Los19-Mar-13 6:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.