Private Sub Form_Load() Dim i As Long Dim command As String Dim description As String Dim category As String Dim Flags As Long Dim action As String For i = 1 To DirectSS1.CountEngines If DirectSS1.ModeName(i) = "RoboSoft Three" Then GoTo OkayToCont Next i OkayToCont: DirectSS1.Select i ' Buffer to hold input string Dim Instring As String ' Use COM1. MSComm1.CommPort = 1 ' 9600 baud, no parity, 8 data, and 1 stop bit. MSComm1.Settings = "9600,N,8,1" ' Tell the control to read entire buffer when Input is used. MSComm1.InputLen = 0 ' Open the port. MSComm1.PortOpen = True ' Send the attention command to the modem. Say ("Hello World!") End Sub ---------------------------------------------------------------- Public Sub Say(Arg As String) Form1.DirectSS1.TextData CHARSET_TEXT, 1, Arg End Sub ---------------------------------------------------------------- Public Sub Wait(duration) StartTime = Timer Do While Timer - StartTime > duration DoEvents Loop End Sub ---------------------------------------------------------------- Private Function Direction() Select Case Compass() Case 13 Direction = "North" Case 5 Direction = "NorthEast" Case 7 Direction = "East" Case 3 Direction = "SouthEast" Case 11 Direction = "South" Case 10 Direction = "SouthWest" Case 14 Direction = "West" Case 12 Direction = "NorthWest" Case 0 Direction = " in an unknown direction." End Select End Function ---------------------------------------------------------------- Private Sub LeftMotor(Arg As Integer) If Arg < 0 Then If Arg > -11 Then Arg = -11 If Arg < -99 Then Arg = -99 MSComm1.Output = "!MLB" & Abs(Arg) End If If Arg > 0 Then If Arg < 11 Then Arg = 11 If Arg > 99 Then Arg = 99 MSComm1.Output = "!MLF" & Arg End If If Arg = 0 Then MSComm1.Output = "!MLF10" 'HC11 stops motors End Sub ---------------------------------------------------------------- Private Sub RightMotor(Arg As Integer) If Arg < 0 Then If Arg > -11 Then Arg = -11 If Arg < -99 Then Arg = -99 MSComm1.Output = "!MRB" & Abs(Arg) End If If Arg > 0 Then If Arg < 11 Then Arg = 11 If Arg > 99 Then Arg = 99 MSComm1.Output = "!MRF" & Arg End If If Arg = 0 Then MSComm1.Output = "!MRF10" 'HC11 stops motors End Sub ---------------------------------------------------------------- Private Function Touch() MSComm1.Output = "!ST" Do DoEvents Loop Until MSComm1.InBufferCount >= 2 Touch = Val(MSComm1.Input) End Function ---------------------------------------------------------------- Private Function Photo() MSComm1.Output = "!SP" Do DoEvents Loop Until MSComm1.InBufferCount >= 1 Photo = Val(MSComm1.Input) End Function ---------------------------------------------------------------- Private Function Compass() MSComm1.Output = "!SC" Do DoEvents Loop Until MSComm1.InBufferCount >= 1 Compass = Val(MSComm1.Input) End Function ---------------------------------------------------------------- Private Function LeftEncoder() MSComm1.Output = "!S<" Do DoEvents Loop Until MSComm1.InBufferCount >= 1 ' Read the "OK" response data in the serial port. LeftEncoder = Val(MSComm1.Input) End Function ---------------------------------------------------------------- Private Function RightEncoder() MSComm1.Output = "!S>" Do DoEvents Loop Until MSComm1.InBufferCount >= 1 RightEncoder = Val(MSComm1.Input) End Function ---------------------------------------------------------------- Private Function Sonar(Arg As Integer) MSComm1.Output = "!S" & Arg Do DoEvents Loop Until MSComm1.InBufferCount >= 1 Sonar = Val(MSComm1.Input) End Function ---------------------------------------------------------------- Private Sub Command10_Click() Label10.Caption = LeftEncoder() End Sub ---------------------------------------------------------------- Private Sub Command11_Click() Label11.Caption = RightEncoder() End Sub ---------------------------------------------------------------- Private Sub Command12_Click() LeftMotor (50) RightMotor (-50) End Sub ---------------------------------------------------------------- Private Sub Command2_Click() Label2.Caption = Sonar(1) End Sub ---------------------------------------------------------------- Private Sub Command3_Click() Label3.Caption = Sonar(2) End Sub ---------------------------------------------------------------- Private Sub Command4_Click() Label4.Caption = Sonar(3) End Sub ---------------------------------------------------------------- Private Sub Command5_Click() Label5.Caption = Sonar(4) End Sub ---------------------------------------------------------------- Private Sub Command6_Click() Label6.Caption = Sonar(5) End Sub ---------------------------------------------------------------- Private Sub Command7_Click() Label7.Caption = Photo() End Sub ---------------------------------------------------------------- Private Sub Command8_Click() Label8.Caption = Compass() End Sub ---------------------------------------------------------------- Private Sub Command9_Click() Label9.Caption = Touch() End Sub ---------------------------------------------------------------- Private Sub Explore_Click() Dim Sonar1Var As Long Dim Sonar2Var As Long Dim LeftClicks As Long Dim RightClicks As Long Say ("I'm exploring!") While (1) Sonar1Var = 0 Sonar2Var = 0 For x = 1 To 10 Sonar1Var = Sonar1Var + Sonar(1) Wait (100) Sonar2Var = Sonar2Var + Sonar(2) Wait (100) Next x Sonar1Var = Sonar1Var / 10 'Get average Sonar2Var = Sonar2Var / 10 'Get average If Sonar1Var < 7000 And Sonar2Var < 7000 Then LeftClicks = LeftEncoder() LeftMotor (-75) RightMotor (-75) While LeftEncoder() - LeftClicks < 25 DoEvents Wend LeftMotor (-50) RightMotor (50) LeftClicks = LeftEncoder() While LeftEncoder() - LeftClicks < 10 DoEvents Wend End If If Sonar1Var + Sonar2Var < 20000 Then If Sonar1Var > Sonar2Var Then RightMotor (-25) LeftMotor (Sonar1Var / 300) Wait (500) Else LeftMotor (-25) RightMotor (Sonar2Var / 300) Wait (500) End If Else LeftMotor (99) RightMotor (99) End If If Touch() = 1 Then Say ("Oops. I bumped something on my right side") LeftClicks = LeftEncoder() LeftMotor (-75) RightMotor (-75) While LeftEncoder() - LeftClicks < 25 DoEvents Wend LeftClicks = LeftEncoder() LeftMotor (-50) RightMotor (50) While LeftEncoder() - LeftClicks < 10 DoEvents Wend End If If Touch() = 2 Then Say ("Oops. Hit something on the left") LeftClicks = LeftEncoder() LeftMotor (-75) RightMotor (-75) While LeftEncoder() - LeftClicks < 25 DoEvents Wend LeftMotor (50) RightMotor (-50) LeftClicks = LeftEncoder() While LeftEncoder() - LeftClicks < 10 DoEvents Wend End If If Touch() = 3 Then Say ("Rats. Hit it head on.") LeftClicks = LeftEncoder() LeftMotor (-75) RightMotor (-75) While LeftEncoder() - LeftClicks < 25 DoEvents Wend LeftMotor (-50) RightMotor (50) LeftClicks = LeftEncoder() While LeftEncoder() - LeftClicks < 10 DoEvents Wend End If Wend End Sub ---------------------------------------------------------------- Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) MSComm1.PortOpen = False End Sub ---------------------------------------------------------------- Private Sub Turn_Light_Click() Dim StartDir As Integer Dim BrightestDir As Integer Dim Highest As Integer Say ("I am looking for light.") StartDir = Compass() 'What direction are we starting in? BrightestDir = StartDir 'Initially StartDir is brightest direction Highest = Photo() 'Light reading for StartDir LeftMotor (50) 'Rotate clockwise at half speed RightMotor (-50) For x = 1 To 8 'Rotate 360 degrees and take light readings each time compass changes Do DoEvents Loop Until Compass() <> StartDir StartDir = Compass() If Photo() > Highest Then Highest = Photo() 'Stores brightest value BrightestDir = Compass() 'Stores compass heading of highest End If Next x Do DoEvents Loop Until Compass() = BrightestDir LeftMotor (0) 'Should stop motors Say (Direction() & " is the brightest direction.") End Sub