使用visual studio命令ardiuno [英] Commands to ardiuno using visual studio
问题描述
我正在研究一个小型学校机器人项目。我正在使用Arduino(即IDE)编写Arduino并使用Visual Studio在C#中创建界面。
现在我可以在我的应用程序中使用C#界面(使用按钮)向我的机器人发出命令。
我面临的问题是,我想控制我的机器人汽车就像游戏中的控件一样。我正在尝试按下向上箭头按钮(键盘)并且机器人向前移动,直到释放该按钮。
我尝试过:
1
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
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
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.
推荐答案
您需要使用Control.KeyDown事件(System.Windows.Forms) [ ^ ]和 Control.KeyUp事件(System.Windows.Forms) [ ^ ],或 Control.KeyPress事件(System.Windows.Forms) [ ^ ]做什么你在问。
You need to use Control.KeyDown Event (System.Windows.Forms)[^] and Control.KeyUp Event (System.Windows.Forms)[^], or Control.KeyPress Event (System.Windows.Forms)[^] to do what you are asking.
这篇关于使用visual studio命令ardiuno的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!