IMPORTANT - robotarm with VB and arduino

im having a problem. for my final work for school i’m trying to controle a robotarm with my computer.
i code in VB on the pc so i used it for my work.

now i’m trying to put a loop so the robot wil make some moves automaticly until i say to stop it.
my problem is ones i put it in the loop i cant make it stop anymore.

do any of you guys and girls have a idea to make it stop?

i am using the ladyada motorshield.

ps sorry for my bad englisch i’m dutch

code vb: main form

Imports System.IO
Imports System.IO.Ports
Imports System.Threading


Public Class Form1
    Dim compoort As String
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Sub GetSerialPortNames()
        For Each sp As String In My.Computer.Ports.SerialPortNames
            ComboBox1.Items.Add(sp)
        Next
        ComboBox1.SelectedIndex = 0
        compoort = ComboBox1.SelectedItem
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GetSerialPortNames()
        SerialPort1.Close()
        SerialPort1.PortName = compoort 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseHover

        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.MouseLeave, Button4.MouseLeave, Button2.MouseLeave, Button3.MouseLeave, Btntest.MouseLeave, Button5.MouseLeave, Button6.MouseLeave, Button7.MouseLeave
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        SerialPort1.Write("5")
        SerialPort1.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.MouseHover
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("3")
        SerialPort1.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.MouseHover
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("2")
        SerialPort1.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.MouseHover
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("4")
        SerialPort1.Close()
    End Sub



    Private Sub Btntest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btntest.MouseHover
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("7")
        SerialPort1.Close()
    End Sub



    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.MouseHover
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("6")
        SerialPort1.Close()
    End Sub


    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.MouseHover
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("8")
        SerialPort1.Close()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.MouseHover
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("9")
        SerialPort1.Close()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        SerialPort1.PortName = ComboBox1.SelectedItem
    End Sub



    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim msg1 As String
        msg1 = MsgBox("de positie waarin de robot arm zich nu bevind is de startpositie. ben u zeker dat u het automatisch programma wilt starten", vbYesNo, "WARNING")
        If msg1 = vbYes Then
            Form2.Show()
            Timer1.Enabled = True
        ElseIf MsgBoxResult.No Then
            MsgBox("stel start positie in en probeer opnieuw")
        End If
    End Sub

    Public Sub stopauto()

        Timer2.Enabled = True

        Form2.Close()
        MsgBox("autorun gestopt")

    End Sub
   
    Public Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Try
            SerialPort1.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        SerialPort1.Write("5")
        SerialPort1.Close()
    End Sub
End Class

code 2nd form:

Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form1.Timer1.Enabled = False
        Form1.stopauto()
    End Sub
End Class

aruino code:

#include <AFMotor.h>

  AF_DCMotor MGrab(1, MOTOR12_1KHZ); 
  AF_DCMotor MOperSwing(2, MOTOR12_1KHZ);
  AF_DCMotor MUnderSwing(3, MOTOR12_1KHZ);
  AF_DCMotor MBase(4, MOTOR12_1KHZ);
  
void setup() {
  Serial.begin(9600);          
  Serial.println("Motor test!");
  MGrab.setSpeed(200);  
  MOperSwing.setSpeed(200);  
  MUnderSwing.setSpeed(200);
  MBase.setSpeed(200);
}

void loop(){
 while (Serial.available() == 0); 
 int val = Serial.read() - '0'; 
 
if (val == 1) 
{
  MUnderSwing.run(FORWARD);
 }
 else if (val == 5)
 {
 MUnderSwing.run(RELEASE); 
 MBase.run(RELEASE);
 MGrab.run(RELEASE);
 MOperSwing.run(RELEASE);
 
 }
 else if (val == 3) 
 {
  MBase.run(FORWARD);
 }
 else if (val == 7)
 {
  MGrab.run(BACKWARD);
 }
  else if (val == 4) 
  {
  MBase.run(BACKWARD);
 }
 else if (val == 8)
 {
 MOperSwing.run(FORWARD);
 }
  else if (val == 2)
 {
 MUnderSwing.run(BACKWARD);
 }
 else if (val == 6)
 {
 MGrab.run(FORWARD);
 }
 else if (val == 9)
 {
 MOperSwing.run(BACKWARD);
 
 }
 
 else if (val == 0)
   {
  MUnderSwing.run(BACKWARD);      // turn it on going forward
  delay(800);
  MUnderSwing.run(RELEASE);
  delay(800);
  MGrab.run(BACKWARD);      // stopped
  delay(300);
  MGrab.run(RELEASE);      // stopped
  delay(800);
  MUnderSwing.run(FORWARD);      // turn it on going forward
  delay(800);
  MUnderSwing.run(RELEASE);
  delay(800);
  MBase.run(FORWARD);
  delay(500);
  MBase.run(RELEASE);
  delay(800);
  MUnderSwing.run(BACKWARD);      // turn it on going forward
  delay(800);
  MUnderSwing.run(RELEASE);
  delay(800);
  MGrab.run(FORWARD);      // stopped
  delay(300);
  MGrab.run(RELEASE);
  delay(800);
  MUnderSwing.run(FORWARD);      // turn it on going forward
  delay(800);
  MUnderSwing.run(RELEASE);
  delay(800);
  MBase.run(BACKWARD);
  delay(500);
  MBase.run(RELEASE);
delay(800);
   }
 {
  Serial.flush();
 }
}