control motor dc with visual basic 2010

hey everyone, so i making this project and i don't know how to stop motor dc after 7 seconds i mean i want it to work in certain time but it keeps spinning and when i upload it , it is keep spinning
what i'd like to make : when i upload it the motor doesnt spin and when i hit 1st button on vb the motor will spin in 7 seconds and when i hit 2nd button the motor will shut and i don't know how to program it , please help me :sweat_smile:
here is my arduino source code:

#define in1 2
#define in2 1

void setup()
{
pinMode(in1, OUTPUT);
pinMode(in2,OUTPUT);
Serial.begin(9600);
}
void loop()
{
while (Serial.available() == 0);
int val = Serial.read() - '0';
if (val == 1) {
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
}
else if (val == 2)
{
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
}
else
{
//val = val;
}
Serial.println(val);
Serial.flush(); // clear serial port
}
and my visual basic code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com8"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("2")
SerialPort1.Close()
End Sub
End Class

It looks like your Arduino code should make the motor start when it receives a 1 and stop when it receives a 2.

Is that what is happening?

You should be able to send the characters with the Serial Monitor to test the Arduino program.

At the moment there is no code in the Arduino program to limit the run time to 7 seconds.

I'm not familiar with VB so I have not looked at the VB code.

...R

A couple of points.
You appear to be using digital pin 1 as one of the motor control pins. This is generally regarded as not a good idea because it is the transmit pin used by hardware Serial.

In order to time something you need to keep the program moving but this while loop

while (Serial.available() == 0);

stops the program in its tracks until Serial data is received. At the very least you need to change the logic of your program from using while to using if and doing something if Serial data is received.

To do the 7 second timing use the principle shown in the BlinkWithoutDelay example sketch. Save the start time when the motor is turned on and check each time through loop() whether 7 seconds has passed and if it has, stop the motor.

Serial.flush() clears the output buffer not the input buffer so is doing nothing for you.