我有以下情况。我尝试通过在Visual C ++ 2017中制作的arduino IDE和窗口应用程序来控制直流电机。我使用串行端口将Visual Studio中的数据处理成Arduino。这是我想要实现的目标:
我正在使用mouse_down事件(当我用鼠标点击按钮时,会发生一些事情),但是在按下时它只发送一个符号(U,D,R,L),导致DC电机只需移动一点点。如何让它运行并发送char直到按钮被释放?我应该使用某种循环吗?
我尝试使用键盘上的num键以相同的方式控制它。问题是,键不起作用,直到焦点放在它们上面(它们被鼠标按下/徘徊)。有没有办法摆脱焦点?
感谢您的帮助。
代码段 arduino code
data=Serial.read();
analogWrite(motorPin1,motorSpeed);
analogWrite(motorPin2,motorSpeed);
xValues=analogRead(xPin);
if((xValues>xIdleHigh) or (data=='R')) {
motorSpeed=map(xValues,xIdleHigh,xRight,motorSpeedLow,motorSpeedHigh);
analogWrite(motorPin1,motorSpeed);
analogWrite(motorPin2,motorSpeedLow);
Serial.print("TURNING RIGHT...");
Serial.print("\n");
delay(50);
}
xValues=analogRead(xPin);
if((xValues<xIdleLow) or (data=='L')) {
motorSpeed=map(xValues,xIdleLow,xLeft,motorSpeedLow,motorSpeedHigh);
analogWrite(motorPin1,motorSpeedLow);
analogWrite(motorPin2,motorSpeed);
Serial.print("TURNING LEFT...");
Serial.print("\n");
delay(50);
}
yValues=analogRead(yPin);
if((yValues<=yUp) or (data=='U')) {
if(pos<150) {
pos+=1;
}
Serial.print("BARREL UP");
Serial.print("\n");
delay(50);
} else if((yValues>=yDown) or (data=='D')) {
if(pos>30) {
pos-=1;
}
Serial.print("BARREL DOWN");
Serial.print("\n");
delay(50);
}
servo.write(pos);
delay(50);`
private: System::Void button1_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyCode == Keys::NumPad6) {
serialPort1->Open();
serialPort1->Write("R");
serialPort1->Close();
}
}
private: System::Void button1_KeyUp(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyCode == Keys::NumPad6) {
serialPort1->Open();
serialPort1->Write("X");
serialPort1->Close();
}
}
private: System::Void button1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
serialPort1->Open();
serialPort1->Write("R");
serialPort1->Close();
}
private: System::Void button1_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
serialPort1->Open();
serialPort1->Write("X");
serialPort1->Close();
}
private: System::Void button2_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyCode == Keys::NumPad4) {
serialPort1->Open();
serialPort1->Write("L");
serialPort1->Close();
}
}
private: System::Void button2_KeyUp(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyCode == Keys::NumPad4) {
serialPort1->Open();
serialPort1->Write("X");
serialPort1->Close();
}
}
private: System::Void button2_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
serialPort1->Open();
serialPort1->Write("L");
serialPort1->Close();
}
private: System::Void button2_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
serialPort1->Open();
serialPort1->Write("X");
serialPort1->Close();
}
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button3_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
serialPort1->Open();
serialPort1->Write("U");
serialPort1->Close();
}
private: System::Void button4_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
serialPort1->Open();
serialPort1->Write("D");
serialPort1->Close();
}`