Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a small school robotics project.I am using Arduino(i.e IDE) for coding of Arduino and making interface in C# using Visual Studio.

Now I am able to give commands to my robot using C# interface (using buttons) in my application.

The problem that I am facing is that, I want to control my Robotic Car like the control in games. I am trying to press the UP Arrow button (of keyboard) and the robot move in forward direction, until is release that button.

What I have tried:

1
C#
private void R_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            res = "1";
            serialPort1.Write(res);

        }
        else if (e.KeyCode == Keys.Down)
        {
            res = "2";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Left)
        {
            res = "3";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Right)
        {
            res = "4";
            serialPort1.Write(res);
        }
    }


2

C#
private void R_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            res = "1";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Down)
        {
            res = "2";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Left)
        {
            res = "3";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Right)
        {
            res = "4";
            serialPort1.Write(res);
        }
        else
        {
            res = "5";
            serialPort1.Write(res);
        }

    }


3

C#
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar.Equals(38))
        {
            res = "1";
            serialPort1.Write("1");
        }
        else if (e.KeyChar.Equals(40))
        {
            res = "2";
            serialPort1.Write("2");
        }
        else if (e.KeyChar.Equals(37))
        {
            res = "3";
            serialPort1.Write("3");
        }
        else if (e.KeyChar.Equals(39))
        {
            res = "4";
            serialPort1.Write("4");
        }}


I am working on a small school robotics project.I am using Arduino(i.e IDE) for coding of Arduino and making interface in C# using Visual Studio.

Now I am able to give commands to my robot using C# interface (using buttons) in my application.

The problem that I am facing is that, I want to control my Robotic Car like the control in games. I am trying to press the UP Arrow button (of keyboard) and the robot move in forward direction, until is release that button.

I have tried:

1

private void R_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            res = "1";
            serialPort1.Write(res);

        }
        else if (e.KeyCode == Keys.Down)
        {
            res = "2";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Left)
        {
            res = "3";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Right)
        {
            res = "4";
            serialPort1.Write(res);
        }
    }
2

private void R_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        {
            res = "1";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Down)
        {
            res = "2";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Left)
        {
            res = "3";
            serialPort1.Write(res);
        }
        else if (e.KeyCode == Keys.Right)
        {
            res = "4";
            serialPort1.Write(res);
        }
        else
        {
            res = "5";
            serialPort1.Write(res);
        }

    }
3

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar.Equals(38))
        {
            res = "1";
            serialPort1.Write("1");
        }
        else if (e.KeyChar.Equals(40))
        {
            res = "2";
            serialPort1.Write("2");
        }
        else if (e.KeyChar.Equals(37))
        {
            res = "3";
            serialPort1.Write("3");
        }
        else if (e.KeyChar.Equals(39))
        {
            res = "4";
            serialPort1.Write("4");
        }}
Now my question is:

"How can I give commands to Arduino by pressing the Desired Button of Keyboard, and that operation (i.e commands runs) until I release that button"

Please Help I'm in trouble. Thanks in Advance.
Posted
Updated 6-May-17 1:55am
Comments
[no name] 6-May-17 6:44am    
I am trying to read a description of an actual problem in your posting.
Stack Holder 6-May-17 6:47am    
Now my question is:

"How can I give commands to Arduino by pressing the Desired Button of Keyboard, and that operation (i.e commands runs) until I release that button"

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900