How to control a DC motor via Arduino and Visual C++

I have the following situation. I try to control a DC motor via arduino IDE and window application made in Visual C++ 2017. I’m using a serial port to process the data from Visual Studio into Arduino. Here’s the things I want to achieve:

  1. I’m using mouse_down event (when I click the button with a mouse, something happens), but it sends just one sign (U, D, R, L) during a pressing, causing a DC motor to move just a little bit. How do I make it run and send the char untill the button is released? Should I use some kind of a loop?

  2. I try to control it the same way, using a num keys on keyboard. The thing is, keys are not functional, untill the focus is put on them (they are pressed by/hovered by mouse). Is there a way to get rid of focus?

Thank you for your help.

Arduino code

#include <Servo.h>

Servo servo;
char data;
int servoPin=9;
int pos=90;
byte motorPin1=5;
byte motorPin2=6;
byte motorSpeed=0;
byte motorSpeedLow=0;
byte motorSpeedHigh=255;
int switchPin=2;
int switchVal;
int xPin=0;
int yPin=1;
int xValues;
int yValues;
int xRight=1023;
int xLeft=0;
int xIdleLow=540;
int xIdleHigh=580;
int yUp=200; //default 0
int yDown=800; //default 1023

void setup() {
  Serial.begin(9600);
  servo.attach(servoPin);
  servo.write(90);
  pinMode(motorPin1,OUTPUT);
  pinMode(motorPin2,OUTPUT);
  pinMode(switchPin,INPUT);
  digitalWrite(switchPin,HIGH);
}

void loop() {
  //if(Serial.available()){
    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);

    switchVal=digitalRead(switchPin);
    if(switchVal==0) {
      Serial.print("SHOOTING");
      Serial.print("\n");
      delay(50);
    }
  //}
}

Visual c++ code

 }
#pragma endregion 
 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 }
 private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
 }
 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();
 }
};
}

I’m using mouse_down event (when I click the button with a mouse, something happens), but it sends just one sign (U, D, R, L) during a pressing,

So the Arduino receives just one character when the mouse becomes pressed. However in the Arduino program you read from the Serial interface whether or not there is anything available. If nothing is available then the data variable will be set to -1. Look at your Arduino code with that in mind.

Check whether Serial data is available before reading it and the data variable will not change if you don’t read anything. You also need to send something when the mouse up event occurs to clear the value of the data variable

UKHeliBob: So the Arduino receives just one character when the mouse becomes pressed. However in the Arduino program you read from the Serial interface whether or not there is anything available. If nothing is available then the data variable will be set to -1. Look at your Arduino code with that in mind.

The thing is, when I uncomment the "if(Serial.available){ do the code}" my Arduino joystick does not work and also the motors don't react to any serial commands in serial window... What can be the issue here?

The serial input basics thread may have information that you can use to make your serial reception more reliable.

If you send one character when the mouse becomes pressed, and another when the mouse becomes released, the Arduino can start the motor moving when the first character arrives, stop the motor when the second character arrives, and do nothing when there is nothing to do.