Public Class Form1

    Dim WithEvents RC As New iRobots.RoCar01
    Dim i As Integer = 5

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If i = 0 Then
            RC.SSDisplayOut(2, 0)
            Timer1.Enabled = False
            Call Track()
        Else
            RC.SSDisplayOut(2, i)
            i -= 1
        End If

    End Sub

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

    Sub Track()
        Dim iOldC, iC As Integer
        iOldC = 7
        ' iOldC 用來記錄前一次 RoCAR 的位置，初始值為7，表示 RoCAR 尚未移動
        iC = RC.CamsIn()
        ' iC 用來記錄目前 RoCAR 的位置，CamsIn() 用來讀取光線感測器的傳回值

        Do Until iC = 0
            Select Case iC
                Case 1      '傳回值 = 1，直走
                    RC.MoveC("F")
                Case 3      '傳回傳 = 3，應該左轉
                    RC.MoveC("L")
                Case 4      '傳回傳 = 4，直走
                    RC.MoveC("F")
                Case 5      '傳回值 = 5，直走
                    RC.MoveC("F")
                Case 6      '傳回值 = 6，應該右轉
                    RC.MoveC("R")
                Case Else   '傳回值 = 其他值，一律後退
                    RC.MoveC("B")
            End Select
            RC.WaitN(100)   '使目前的動作持續 0.1 秒，然後停止
            RC.MoveC("X")
            iOldC = iC
            iC = RC.CamsIn()
        Loop

        '當 RoCAR 移到黑黑黑位置，而且 iOldC = 7(表示未移動)，提醒使用者將車子放到軌道上
        If iC = 0 And iOldC = 7 Then
            MsgBox("請將 RoCAR 放在軌道上")
        Else
            MsgBox("終點到了")
        End If
    End Sub
End Class
