PWM to RS232 Camera Zoom

Not sure if this is any better?

' ***********************************************************************************
' * * ' * Title : VK-S274 & VK-S274R SERIES Hitachi Block Camera Controller *
' * Version : 1.1 Beta * 
' * Last Updated : 02.12.2008 * 
' * Target Device : ATMEGA8535 * 
' * Author : Farmtech & Neptune769 * 
' * Support : Try Skype Name : farmtech_dk or in MCS Bascom Forum * 
' * Program code : BASCOM AVR * 
' * Hardware req. : VK-S274 or VK-S274R SERIES Hitachi Block Camera * 
' * Description : Control : Zoom - Focus & Iris Via Serialport on Camera * 
' * Baud for camera : 4800.8.E.1 : Even Parity default for camera * 
' * : 9600.8.N.1 but need setup in Camera (:WE05EF0) * 
' * * 
' * More Commands : See Datasheet lots of other stuff you can do. * 
' * * 
' * Datasheet : http://www.data-sheet.net/Vk-s454+ebook+pdf.html * 
' * : http://ip.gliebetronics.com:800/hitachi/S274R_Comm_E11.pdf * 
' * * 
' * Copyright : None use it as you like * 
' * * 
' *********************************************************************************** 
$regfile = "m8535.dat" $crystal = 8000000 $hwstack = 40 $swstack = 30 $framesize = 60 Open "comd.2:4800,8,e,1" For Output As #1 ' 
4800 bps , even Parity ; default for camera '
Config Ports for the 6 buttons n PortC we use buttons to GND Config Portc.0 = Input : Set Portc.0 '
Zoom NEAR Button Config Portc.1 = Input : Set Portc.1 '
Zoom FAR Button Config Portc.2 = Input : Set Portc.2 '
Focus NEAR Button IN Config Portc.3 = Input : Set Portc.3 '
Focus FAR Button OUT Config Portc.4 = Input : Set Portc.4 '
Iris offset (average) + Button Config Portc.5 = Input : Set Portc.5 '
Iris offset (average) - Button Config Portc.6 = Input : Set Portc.6 '
Freeze ON / OFF Toggle Button '
Config Portc.7 = Input : Set Portc.7 '
not used yet Button Dim I As Byte , Irish As Byte , Irisl As Byte , Irisavg As Byte , Freezebit As Byte '
Print Version() '
setup Camera Print #1 , ":WFCCB01" ' 
Switch the continuous digital zoom ON Waitms 10 Print #1 , ":WFDFC10" ' 
Zoom speed normal Waitms 10 Print #1 , ":WFCCB40" ' 
Switch the instant digital zoom ON Waitms 10 Print #1 , ":WFCBBA8" ' 
Auto focus ON allways (not on manual Focus) '
setup Iris offset (average) level Irisavg = &H40 ' 
Iris is normal hex 40 i hope Irish = 0 ' 
Bit set for Iris to HIGH Irisl = 0 ' 
Bit set for Iris to LOW Freezebit = 0 ' 
togglebit for freez images Wait 1 '
The main program Do If Pinc.0 = 0 Then Print #1 , ":WFCBB99" ' 
zoom near Bitwait Pinc.0 , Set ' 
wait for button relace Print #1 , ":WFCBBFE" ' 
stop Print #1 , ":WFCBBA8" ' 
Auto focus on Waitms 300 ' 
wait for auto zoom to get focus End If If Pinc.1 = 0 Then Print #1 , ":WFCBB9B" ' 
zoom far Bitwait Pinc.1 , Set ' 
wait for button relace Print #1 , ":WFCBBFE" ' 
stop Print #1 , ":WFCBBA8" ' 
Auto focus on Waitms 300 ' 
wait for auto zoom to get focus End If If Pinc.2 = 0 Then Print #1 , ":WFCBBFE" ' 
Auto focus off Print #1 , ":WFCBBAA" ' 
focus near Bitwait Pinc.2 , Set ' 
wait for button relace Print #1 , ":WFCBBFE" ' 
stop End If If Pinc.3 = 0 Then Print #1 , ":WFCBBFE" ' 
Auto focus off Print #1 , ":WFCBBA9" ' 
focus far Bitwait Pinc.3 , Set ' 
wait for button relace Print #1 , ":WFCBBFE" ' 
stop End If 'Iris offset (average) level + Open If Pinc.4 = 0 And Irish = 0 Then '
if pinc.4 and irisbit high is 0 else skip Irisl = 0 ' 
reset to low bit I = Irisavg + 1 ' 
to start from known pos For I = Irisavg To 255 ' 
Irisavg to High 00 - FF Print #1 , ":WFD9E" ; Hex(i) '
Iris offset (average) send Irisavg = I ' 
put I count into Irisavg If I = 255 Then ' 
If MAX Iris then exit for Irish = 1 ' 
Set Iris bit TO high so it dont do the OPEN until you have closed it a bit Exit For End If If Pinc.4 = 1 Then Exit For ' 
check for button relace Next ' 
next i End If '
Iris offset (average) level - Close If Pinc.5 = 0 And Irisl = 0 Then ' 
if pinc.5 and irisbit low is 0 else skip Irish = 0 ' 
reset to high bit I = Irisavg ' 
to start from known pos For I = Irisavg To 0 Step -1 ' 
from Irisavg to MIN FF - 00 Print #1 , ":WFD9E" ; Hex(i) ' 
Iris offset (average) send Irisavg = I ' 
put I count into Irisavg If I = 0 Then ' 
If MIN Iris then exit for Irisl = 1 ' 
Set Iris bit TO low so it dont do the CLOSE until you have open it a bit Exit For End If If Pinc.5 = 1 Then Exit For ' 
check for button relace Next ' 
next i End If If Pinc.6 = 0 And Freezebit = 0 Then Print #1 , ":WFF3301" '
Freeze Field ON Freezebit = 1 '
set freezebit to freeze OFF Bitwait Pinc.6 , Set ' 
check for button relace End If If Pinc.6 = 0 And Freezebit = 1 Then Print #1 , ":WFF3300" '
Freeze OFF Freezebit = 0 '
set freezebit to freeze ON Bitwait Pinc.6 , Set ' 
check for button relace End If '
If Pinc.7 = 0 Then ' 
Print #1 , ":" '
not used yet ' 
Bitwait Pinc.7 , Set '
End If Loop Close #1 End