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
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");
}}
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.