' Visual Basic program for demonstrating serial communication to ' a Basic Stamp. Private Sub Form_Load() ' Initialize the Form when Loading Form1.Caption = "Simple Servo Controller" Form1.Label1 = "Servo #1" Form1.Label2 = "Servo #2" ' Initialize the Servo #1 Scroll ' Bar HScroll1.Min = 0 ' Set the Horizontal Scroll Bar ' minimum value to 0 HScroll1.Max = 254 ' Set the maximum value HScroll1.Value = 127 ' Set the initial Starting Value ' at the mid point HScroll2.Min = 0 ' Initialize Servo #2 scroll bar HScroll2.Max = 254 HScroll2.Value = 127 Form1.Label3 = HScroll1.Value ' Display the Servo #1 Position Form1.Label4 = HScroll2.Value ' Display the Servo #2 Position Form1.Label5 = "Com Port" Command1.Caption = "Open Port" ' Name the Open the Serial Port ' Button Command2.Caption = "Close" ' Name the exit application Button MSComm1.CommPort = 6 ' Use Com6 as the default Com port MSComm1.Settings = "9600,N,8,1" ' 9600 baud, no parity, 8 data, ' and 1 stop bit. Form1.Text1 = MSComm1.CommPort ' Show the default Com Port Form1.Label6 = "Options" Check1.Caption = "Lights" ' Name the Options Check Boxes, ' and initialize Check1.Value = 0 ' them to all un-checked. Check2.Caption = "Sound" Check1.Value = 0 Check3.Caption = "Fog" Check3.Value = 0 Check4.Caption = "Relay" Check4.Value = 0 Timer1.Interval = 15 ' Output servo positions every ' 15ms End Sub Private Sub HScroll1_Change() ' Servo #1 Position Setting Scroll ' Bar Form1.Label3 = HScroll1.Value End Sub Private Sub HScroll2_Change() ' Servo #2 Position Setting Scroll ' Bar Form1.Label4 = HScroll2.Value End Sub Private Sub Command1_Click() ' Open and Close the Com Port On Error GoTo Port_Open_Error ' Error Trapping If MSComm1.PortOpen = False Then ' Open Com Port MSComm1.CommPort = Val(Form1.Text1) MSComm1.PortOpen = True Command1.Caption = "Close Port" Timer1.Enabled = True ' Start Servo Update Timer Else ' Close Com Port MSComm1.PortOpen = False Command1.Caption = "Open Port" Timer1.Enabled = False ' Stop Servo Update Timer End If Exit Sub Port_Open_Error: ' Check Error Type If Err.Number = 8002 Then MsgBox "Com Port Not Available" End If If Err.Number = 8005 Then MsgBox "Com Port Already in Use" End If Err.Clear ' Clear Error Message End Sub Private Sub Command2_Click() ' Close Application If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If End End Sub Private Sub Timer1_Timer() ' Send Servo Position and Options ' out as Serial Data If MSComm1.PortOpen = True Then Accessory = 0 If Check1.Value = 1 Then Accessory = Accessory + 1 If Check2.Value = 1 Then Accessory = Accessory + 2 If Check3.Value = 1 Then Accessory = Accessory + 4 If Check4.Value = 1 Then Accessory = Accessory + 8 MSComm1.Output = Chr$(255) MSComm1.Output = Chr$(HScroll1.Value) MSComm1.Output = Chr$(HScroll2.Value) MSComm1.Output = Chr$(Accessory) End If End Sub ' Demo program for receiving serial data from a PC for controlling ' 2 servos and 4 digital outputs. This program is based on using ' a BS2p Stamp. ' {$STAMP BS2p} ' {$PBASIC 2.5} Servo_Pos VAR Word(3) ' Servo Positon Array Options VAR Byte ' Accessory Options variable i VAR Byte ' Counter variable Baud VAR Word ' Baud Rate variable Baud = 16624 ' Set Baud Rate: Inverted, 9600 Baud, ' 8 Data Bits, 1 Stop Bit Main: SERIN 15,Baud,[WAIT(255), Servo_Pause(1), Servo_Pos(2), Options)] FOR i = 1 TO 2 ' Set Servo Positions PULSOUT i, Servo_Pos(i)*52/10+1333 ' For a BS2p use Servo_cmd*52/10+1333 ' For a BS2 use Servo_cmd*2+500 ' For a BS2sx use Servo_cmd*49/10+1250 NEXT IF Options.BIT0 = 1 THEN HIGH 11 ' Lights Check Box IF Options.BIT0 = 0 THEN LOW 11 IF Options.BIT1 = 1 THEN HIGH 12 ' Sound Check Box IF Options.BIT1 = 0 THEN LOW 12 IF Options.BIT2 = 1 THEN HIGH 13 ' Fog Check Box IF Options.BIT2 = 0 THEN LOW 13 IF options.BIT3 = 1 THEN HIGH 14 ' Relay Check Box IF Options.BIT3 = 0 THEN LOW 14 GOTO main