编辑:最终解决方案:
看到最终的解决方案被隐藏在评论中,我会把它放在这里供未来的观众使用。
我的问题是我用来停止动作的信号包括“r”,也是停止命令(我以为我很聪明,结合我的“左”命令和“停止”命令)。 C ++(arduino)代码分别读取每个字符,因此完全停止信号,而不仅仅是我想要的信号。
谢谢你 dbasnett 以了解这一点:)
好的,大家好,这是我对这个最新项目的最后一个问题。简单来说,这个想法就是通过arduino制作一个控制遥控车的程序。有用!!! (只有一个小虫子)。
简单地说,我使用箭头键或WASD来控制程序。它通过串行端口向arduino发送信号,arduino中有一些函数用于解释信号。您总共可以发送9个信号;一个用于每个方向,一个用于停止每个方向输入(例如,左侧使车轮指向左侧,左侧向左侧指向车轮直线前方)。
问题是:当我向前(或反向)和向左(或向右)时,如果我改变DIRECTION(左/右),它会一起停止。我花了大部分时间都试图解决这个问题,我只是看不出我做错了什么。
所以,我的代码:
Imports System.IO.Ports
Imports System.IO
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Shared lturn As Boolean
Shared rturn As Boolean
Shared keydelay As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "com3" 'change com port to match your Arduino port
SerialPort1.BaudRate = 115200
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim bHandled As Boolean = False
If SerialPort1.IsOpen Then
Else
SerialPort1.Open()
End If
Select Case e.KeyCode
Case Keys.Right
pbBgML.BackColor = Color.Transparent
pbBgMR.BackColor = Color.Black
SerialPort1.Write("d")
e.Handled = True
Case Keys.Left
pbBgMR.BackColor = Color.Transparent
pbBgML.BackColor = Color.Black
SerialPort1.Write("a")
e.Handled = True
Case Keys.Up
pbBgBC.BackColor = Color.Transparent
pbBgTC.BackColor = Color.Black
SerialPort1.Write("w")
e.Handled = True
Case Keys.Down
pbBgTC.BackColor = Color.Transparent
pbBgBC.BackColor = Color.Black
SerialPort1.Write("s")
e.Handled = True
Case Keys.Space
SerialPort1.Write("r")
e.Handled = True
End Select
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.Right
pbBgMR.BackColor = Color.Transparent
SerialPort1.Write("dr")
Case Keys.Left
pbBgML.BackColor = Color.Transparent
SerialPort1.Write("ar")
Case Keys.Up
pbBgTC.BackColor = Color.Transparent
SerialPort1.Write("wr")
Case Keys.Down
pbBgBC.BackColor = Color.Transparent
SerialPort1.Write("sr")
End Select
End Sub
End Class
另外,如果你想要它,arduino代码(用C ++编写):
// Car Control v. 0.2
int reversePin = 9;
int forwardPin = 8;
int leftPin = 10;
int rightPin = 11;
byte byteRead;
int time;
void forward(int time){
digitalWrite(reversePin, HIGH);
Serial.println("This is forward...");
digitalWrite(forwardPin, LOW);
delay(time);
}
void reverse(int time){
digitalWrite(forwardPin, HIGH);
Serial.println("This is reverse...");
digitalWrite(reversePin, LOW);
delay(time);
}
void left(int time){
digitalWrite(rightPin, HIGH);
Serial.println("This is left...");
digitalWrite(leftPin, LOW);
delay(time);
}
void right(int time){
digitalWrite(leftPin, HIGH);
Serial.println("This is right...");
digitalWrite(rightPin, LOW);
delay(time);
}
void off(){
Serial.println("This is stop...");
digitalWrite(leftPin, HIGH);
digitalWrite(rightPin, HIGH);
digitalWrite(reversePin, HIGH);
digitalWrite(forwardPin, HIGH);
}
void setup() {
// initialize the digital pins as an output.
pinMode(rightPin, OUTPUT);
pinMode(leftPin, OUTPUT);
pinMode(forwardPin, OUTPUT);
pinMode(reversePin, OUTPUT);
Serial.begin(115200);
Serial.print("\n\nStart...\n");
}
void loop()
{
//Turn everything off...
if (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();
switch(byteRead)
{
case 'w':
forward(5);
break;
case 'wr':
digitalWrite(forwardPin, HIGH);
break;
case 's':
reverse(5);
break;
case 'sr':
digitalWrite(reversePin, HIGH);
break;
case 'a':
left(5);
break;
case 'ar':
digitalWrite(leftPin, HIGH);
break;
case 'd':
right(5);
break;
case 'dr':
digitalWrite(rightPin, HIGH);
break;
case 'r':
off();
break;
}
}
}
对于巨大的文本块感到抱歉,但我根本不知道问题出在哪里。干杯。
答案 0 :(得分:0)
我认为你的方法有点偏僻。
我建议您使用位字段来确定每个输入使用不同的位来按下哪个输入(因为每个输入实际上是独立的)。
Private _input As Integer = 0
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
'set the bit of the required input
Select Case e.KeyCode
Case Keys.Right
_input = SetBit(_input, 0) '0001
Case Keys.Left
_input = SetBit(_input, 1) '0010
Case Keys.Up
_input = SetBit(_input, 2) '0100
Case Keys.Down
_input = SetBit(_input, 3) '1000
End Select
SerialPort1.Write(Convert.ToChar(_input))
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
'clear the bit of the de-selected input
Select Case e.KeyCode
Case Keys.Right
_input = ClearBit(_input, 0)
Case Keys.Left
_input = ClearBit(_input, 1)
Case Keys.Up
_input = ClearBit(_input, 2)
Case Keys.Down
_input = ClearBit(_input, 3)
End Select
SerialPort1.Write(Convert.ToChar(_input))
End Sub
Private Function SetBit(value As Integer, bit As Integer) As Integer
' Create a bitmask with the 2 to the nth power bit set:
Dim mask As Integer = CInt(2 ^ bit)
' Set the nth Bit:
value = value Or mask
Return value
End Function
Private Function ClearBit(value As Integer, bit As Integer) As Integer
' Create a bitmask with the 2 to the nth power bit set:
Dim mask As Integer = CInt(2 ^ bit)
' Clear the nth Bit:
value = value And Not mask
Return value
End Function
然后你需要在C ++代码中做相反的事情(这应该更加优雅)并检查无效的密钥组合(例如,一起向前和向后)
理想情况下,您希望将字节传递给串口而不是字符,但我试图尽可能保持相似
答案 1 :(得分:0)
我无法使用C代码,但VB代码可以使用单个字节作为控件
<FlagsAttribute()> _
Enum ctrl As Byte
stp = 0 'stop is no bits set
frwd = 1 << 0 '1
back = 1 << 1 '2
left = 1 << 2 '4
rght = 1 << 3 '8
mask = 255
End Enum
Private _control(0) As ctrl
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Right
_control(0) = _control(0) Or ctrl.rght
Case Keys.Left
_control(0) = _control(0) Or ctrl.left
Case Keys.Up
_control(0) = _control(0) Or ctrl.frwd
Case Keys.Down
_control(0) = _control(0) Or ctrl.back
End Select
'SerialPort1.Write(_control, 0, 1)
Debug.WriteLine(_control(0))
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.Right
_control(0) = _control(0) And (ctrl.mask Xor ctrl.rght)
Case Keys.Left
_control(0) = _control(0) And (ctrl.mask Xor ctrl.left)
Case Keys.Up
_control(0) = _control(0) And (ctrl.mask Xor ctrl.frwd)
Case Keys.Down
_control(0) = _control(0) And (ctrl.mask Xor ctrl.back)
End Select
'SerialPort1.Write(_control, 0, 1)
Debug.WriteLine(_control(0))
End Sub