Putting robotics at your service™

Free shipping on orders over $200

Just Basic programming

Print view Share :
Previous topicNext topic

Page 1 of 1 [ 7 posts ]

Guru ( offline )
Posts: 2251
Posted: 2008-03-20 16:48 
 Post subject: Just Basic programming
This topic may be of interest for those that want to try some very simple windows GUI programming. Just Basic is free from the below site and has a well supported user group. In particular serial communication with the SSC-32 might be of interest. Bottom is a simple GUI that sends bytes to the com port, then reads the return data after a short delay. I use it for simple serial testing. For other testing I tend to skip the gui and just use the console mode to read the return data.

http://www.justbasic.com/index.html
http://justbasic.conforums.com/

Code:
'Test Tx/Rx serial port program for com1.
'Connect the Rx (pin 2) and Tx (pin 3) together
'on a female DB9 connector and connect to the
'serial port jack.

nomainwin

WindowWidth = 400
WindowHeight = 300
texteditor #window.te, 0, 0, 391, 254
open "Device Response" for window as #window
print #window, "trapclose [quit]"
print #window.te, "!autoresize";
print #window, "font courier_new 9";
print #window.te, "The response from the device should appear below:"

open "Com1:9600,n,8,1,ds0,cs0,rs" for random as #comm

[repeat]

Prompt "Enter command to send to device "; s$

if s$ = "" then goto [quit]

Print #comm, s$;

EndTime = time$("ms") + 50
while time$("ms") < EndTime
wend

dataRead$ = input$(#comm, lof(#comm))

print #window.te, dataRead$

goto [repeat]

[quit]
Close #comm
Close #window
End


Guru ( offline )
Posts: 2251
Posted: 2008-03-21 00:32 
The below links might be of interest. the first has some joystick and gamepad code, and the the other is for a parallel port setup that has check boxes, radio buttons, and a regular button in a gui. The bottom code (no gui) is what I'm using to test using a timed servo command and then checking for input values such as switch closure or servo position. Some of the commented stuff may be old.

http://libertybasic.conforums.com/index ... 1152087182

http://justbasic.conforums.com/index.cg ... 1145483474

Code:
open "Com5:9600,n,8,1,ds0,cs0,rs" for random as #comm

Print #comm, "#0P2200" '; 'initial position

y=0 'outside loop counter

[loop]

timer 2000, [delay2] 'iniial delay before timed move
wait
[delay2]
timer 0

x=0   'inside loop counter

'start 3 second move
Print #comm, "#0P1100T3000" 's$
'Print #comm, "#0P1100S367"

[loop1] 'inside loop

x=x+1

y$ = "VC"

Print #comm, y$ '; chr$(13);

timer 20, [delay] 'read return delay
wait
[delay]
timer 0

dataRead$ = input$(#comm, lof(#comm))

'print dataRead$

'print "   ";ASC(dataRead$)
num = ASC(dataRead$)
print num

if num > 100 then Print #comm, "STOP 0"

if x < 100 then [loop1] 'set for 10 samples

'Print #comm, "#0"; chr$(27) 'stop servo move

Print #comm, "QP0" 'get analog servo pot value

timer 50, [delay3]
wait
[delay3]
timer 0

dataRead$ = input$(#comm, lof(#comm))

print dataRead$ 'print out servo position
print ASC(dataRead$)

Print #comm, "#0 P2200" 'send servo to start position

print ""

y=y+1

if y < 10 then [loop] 'set for 10 loops

print "done"

Close #comm


Guru ( offline )
Posts: 2251
Posted: 2008-03-22 13:09 
Some time in the past someone expressed interest in sliders. Below is some demo slider code. Just basic has some graphics issues (at least in my mind) but one probably could make sliders for servo control and such.

Code:
 nomainwin

    WindowWidth = 568
    WindowHeight = 380

    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)

    button #main.btnStart, "Start", [btnStartClick], UL, 422, 21, 122, 25
    button #main.btnStop, "Stop", [btnStopClick], UL, 422, 61, 122, 25
    button #main.btnRnd, "Randomize", [btnRndClick], UL, 422, 101, 122, 25

    button #main.btnQuit, "Quit", [btnQuitClick], UL, 414, 306, 122, 25
    graphicbox #main.graph1, 14, 16, 240, 100
    graphicbox #main.graph2, 14, 126, 240, 100
    graphicbox #main.graph3, 14, 236, 240, 100
    graphicbox #main.dial1, 294, 16, 25, 100
    graphicbox #main.dial2, 270, 161, 100, 25
    graphicbox #main.dial3, 270, 236, 100, 100
    statictext #main.lbl1, "100", 326, 56, 30, 20
    statictext #main.lbl2, "100", 310, 141, 30, 20
    statictext #main.lbl3, "100", 310, 216, 30, 20
    open "slider type controls" for dialog as #main
    print #main.graph1,"down; fill white; flush"
    print #main.graph2,"down; fill white; flush"
    print #main.graph3,"down; fill white; flush"
    print #main.dial1,"down; fill white; flush"
    print #main.dial2,"down; fill white; flush"
    print #main.dial3,"down; fill white; flush"
    print #main, "trapclose [quit.main]"
    print #main.dial1,"when leftButtonUp [setDial1]"
    print #main.dial2,"when leftButtonUp [setDial2]"
    print #main.dial3,"when leftButtonUp [setDial3]"

    print #main, "font ms_sans_serif 10"

    dim a(3)
    for i = 1 to 3
        handle$= "#main.graph";i
        #handle$, "down"
        a(i)=int(100*rnd(1))
        y = 100 - a(i)
        #handle$, "color ";word$("red green blue",i)
        #handle$, "line 0 ";y;" 240 ";y
    next

    #main.dial3, "fill white; color blue; place 50 50; circle 35"
    da = 45 'degrees
    rr = 43
    for i = 0 to 10
        ai = i*10
        f1 = ai/100*(180+2*da)-da
        f2 = f1 *3.1415926/180
        x1 = 50 + rr*cos(f2)
        y1 = 50 - rr*sin(f2)
        #main.dial3, "size ";int(i*.7)+2;";set ";x1;" ";y1
    next
    rr = 35
    #main.dial3, "size 1; getbmp dial3 0 0 100 100"
    gosub [drawDials]
    wait

[setDial1]
    a(1) = 100 - MouseY
    if timerEnabled = 0 then gosub [drawDials]
    wait

[setDial2]
    a(2) = MouseX
    if timerEnabled = 0 then gosub [drawDials]
    wait

[setDial3]
    'to get angle from MouseX, MouseY - ???
    dx = MouseX - 50
    dy = 50 - MouseY
    a = atan2(dx, dy)   '0..360
    if a>270 then a = a-360 '-90..270
    '-45..225 -> 0..100
    select case
    case a<-45
        a2 = 0
    case a>225
        a2 = 100
    case else
        a2 = (a+45)/270*100
    end select
    a(3) = a2
    if timerEnabled = 0 then gosub [drawDials]
    wait

[quit.main]
    timer 0
    Close #main
    END

[btnRndClick]
    for i = 1 to 3
        a(i)=int(100*rnd(1))
    next
    if timerEnabled = 0 then gosub [drawDials]
    wait

[drawDials]
    for i = 1 to 3
        handle$= "#main.lbl";i
        #handle$, a(i)
        y = 100 - a(i)
        select case i
        case 1
            #main.dial1, "fill white; backcolor red; color red; place 0 100; boxfilled 25 ";y
'            print "0 ";y;" 25 100"
        case 2
            #main.dial2, "fill white; color green; line 0 20 100 0"
            x = 100-y   'reverce one more time
            i0 = 20*(1-x/100)
            for i1 = i0 to 20
                xx = (20-i1)/20*100
                #main.dial2, "line ";xx;" ";i1;" ";x;" ";i1
            next
'            print "0 ";y;" 25 100"
        case 3
            f1 = a(i)/100*(180+2*da)-da
            f2 = f1 *3.1415926/180
            x1 = 50 + rr*cos(f2)
            y1 = 50 - rr*sin(f2)
            #main.dial3, "discard; drawbmp dial3 0 0"
            #main.dial3, "line 50 50 ";x1;" ";y1
        end select
    next i
return

[nextTimer]
    timer 0
    for i = 1 to 3
        a(i)=a(i)+int(5*rnd(1))-2
        if a(i)>100 then a(i)=100
        if a(i)<0 then a(i)=0

        handle$= "#main.graph";i
        #handle$, "getbmp tmpBmp 0 0 239 100"
        #handle$, "drawbmp  tmpBmp 1 0"
        #handle$, "color white; line 0 0 0 100; discard"
        y = 100 - a(i)
        #handle$, "color ";word$("red green blue",i)
        #handle$, "set 0 ";y
    next
    gosub [drawDials]
    timer 100, [nextTimer]
    wait

[btnStartClick]    'Perform action for the button named 'btnStart'
    'Insert your own code here
    timer 0
    timerEnabled = 1
    timer 100, [nextTimer]
    wait


[btnStopClick]    'Perform action for the button named 'btnStop'
    'Insert your own code here
    timer 0
    timerEnabled = 0
    wait


[btnQuitClick]    'Perform action for the button named 'btnQuit'
    goto [quit.main]
    wait

'*****************************************
function atan2(x,y) 'returns 0..360 degrees
if abs(x)<0.001 then atan2 = 90+(y<0)*180 : exit function
    pi = 3.1415925
    tan = y/x
    atn = atn(tan)
select case
    case x>0 and y>0
        atan2 = atn/pi*180
    case x<=0
        atn = atn + pi
        atan2 = atn/pi*180
    case else
        atn = atn + 2*pi
        atan2 = atn/pi*180
end select
end function



Code:
nomainwin
    'on error goto [exitclicked]
    WindowWidth = 1000
    WindowHeight = 600

    dim SliderLocation(8,3) 'X co-ordinate of top left of slider background
    dim SliderColor$(8,3)   'drawing colour of each slider track
    dim LampLocation(8)   'drawing position for each lamp
    dim GroupBoxLocation(8) ' drawing position top left og grey box aruond lamp and 3 sliders
    dim ColorVal(8,3)      '0-255 value of each slider
    dim Color$(3)          '"Red" "Green" "Blue"
    dim SliderPosY(8,3)    'Y co-ordinate of centre of slider button

    SliderWidth=30
    LampRadius=40

    Color$(1) ="red"
    Color$(2) = "green"
    Color$(3) = "blue"

    for x=1 to 8
        LampLocation(x) = ((x-1)*120)+60
        GroupBoxLocation(x) = LampLocation(x) - 52
        SliderLocation(x,1) = LampLocation(x) - 45
        SliderLocation(x,2) = LampLocation(x) - 10
        SliderLocation(x,3) = LampLocation(x) + 25
        SliderColor$(x,1) = "red"
        SliderColor$(x,2) = "green"
        SliderColor$(x,3) = "blue"
    next x


    button #main.exit, "Exit", [exitClicked], UL, 350, 510,200,50


    for x =1 to 8
        for y = 1 to 3
        ColorVal(x,y) = 127
        SliderPosY(x,y) = (255 - ColorVal(x,y))+ 210
        next y
    next x

    graphicbox #main.graph, 10, 10, 970, 490

    'Com=16000   'set inputr and output comms buffer

    'open "com1:9600,n,8,1,ds0" for random as #comport


    open "Ryger FOOTLIGHT DMX PC Interface" for window as #main
    ' Add a trapclose statement
    Print #main, "Trapclose [exitClicked]"
    print #main.graph, "down ; fill green"
    print #main, "font ms_sans_serif 0 16"
    for x=1 to 8
        print #main.graph,"place ";GroupBoxLocation(x);" 18"
        print #main.graph,"backcolor lightgray"
        print #main.graph,"boxfilled ";GroupBoxLocation(x)+104;" 482"
        print #main.graph,"place ";GroupBoxLocation(x)+25;" 40"
        print #main.graph, "\ParCan ";x
        print #main.graph, "place ";LampLocation(x);" 100"
        print #main.graph, "backcolor ";ColorVal(x,1);" ";ColorVal(x,2);" ";ColorVal(x,3)
        print #main.graph, "circlefilled ";LampRadius

        for y = 1 to 3
            'draw slider backgound
            print #main.graph, "place ";SliderLocation(x,y);" 210"
            print #main.graph, "backcolor darkgray"
            print #main.graph, "boxfilled ";SliderLocation(x,y)+20;" 475"
            'drawcoloured lines on sliders
            print #main.graph, "place ";SliderLocation(x,y)+8;" 210"
            print #main.graph, "backcolor ";Color$(y)
            print #main.graph, "boxfilled ";SliderLocation(x,y)+12;" 475"
            'print slider values above sliders
            print #main.graph, "backcolor white"
            print #main.graph, "place ";SliderLocation(x,y)-2;" 190"
            print #main.graph, "\";ColorVal(x,y)
            'draw slider buttons
            print #main.graph, "place ";SliderLocation(x,y);" ";SliderPosY(x,y)-5
            print #main.graph, "backcolor ";Color$(y)
            print #main.graph, "boxfilled ";SliderLocation(x,y)+20;" ";SliderPosY(x,y)+5

        next y
    next x


    ' now flush the entire background picture once only.
    ' This picture will persist but will have the sliders centered
    Print #main.graph, "flush"

    'now start the mouse event
    print #main.graph, "when leftButtonMove [moveslider]"
    print #main.graph, "when leftButtonUp [moveslider]"

    'now start the repeating timed drawing loop
    'the loop has been changed to draw all sliders
    'every 1000 ms

    timer 1000, [drawSliders]
    wait



[moveslider]
select case
    case (MouseX > 15 ) and (MouseX < 35)
        SliderArrayMain=1
        SliderArraySub=1

    case (MouseX > 50 ) and (MouseX < 70)
        SliderArrayMain=1
        SliderArraySub=2

    case (MouseX > 85 ) and (MouseX < 105)
        SliderArrayMain=1
        SliderArraySub=3

    case (MouseX > 135 ) and (MouseX < 155)
        SliderArrayMain=2
        SliderArraySub=1

    case (MouseX > 170 ) and (MouseX < 190)
        SliderArrayMain=2
        SliderArraySub=2

    case (MouseX > 205 ) and (MouseX < 225)
        SliderArrayMain=2
        SliderArraySub=3

    case (MouseX > 255 ) and (MouseX < 275)
        SliderArrayMain=3
        SliderArraySub=1

    case (MouseX > 290 ) and (MouseX < 310)
        SliderArrayMain=3
        SliderArraySub=2

    case (MouseX > 325 ) and (MouseX < 345)
        SliderArrayMain=3
        SliderArraySub=3

    case (MouseX > 375 ) and (MouseX < 395)
        SliderArrayMain=4
        SliderArraySub=1

    case (MouseX > 410 ) and (MouseX < 430)
        SliderArrayMain=4
        SliderArraySub=2

    case (MouseX > 445 ) and (MouseX < 465)
        SliderArrayMain=4
        SliderArraySub=3

    case (MouseX > 495 ) and (MouseX < 505)
        SliderArrayMain=5
        SliderArraySub=1

    case (MouseX > 530 ) and (MouseX < 550)
        SliderArrayMain=5
        SliderArraySub=2

    case (MouseX > 565 ) and (MouseX < 585)
        SliderArrayMain=5
        SliderArraySub=3

    case (MouseX > 615 ) and (MouseX < 635)
        SliderArrayMain=6
        SliderArraySub=1

    case (MouseX > 650 ) and (MouseX < 670)
        SliderArrayMain=6
        SliderArraySub=2

    case (MouseX > 685 ) and (MouseX < 705)
        SliderArrayMain=6
        SliderArraySub=3

    case (MouseX > 735 ) and (MouseX < 755)
        SliderArrayMain=7
        SliderArraySub=1

    case (MouseX > 770 ) and (MouseX < 790)
        SliderArrayMain=7
        SliderArraySub=2

    case (MouseX > 805 ) and (MouseX < 825)
        SliderArrayMain=7
        SliderArraySub=3

    case (MouseX > 855 ) and (MouseX < 875)
        SliderArrayMain=8
        SliderArraySub=1

    case (MouseX > 890 ) and (MouseX < 910)
        SliderArrayMain=8
        SliderArraySub=2

    case (MouseX > 925 ) and (MouseX < 945)
        SliderArrayMain=8
        SliderArraySub=3

end select

     PosY=MouseY
     if PosY>470 Then PosY=470
     if PosY<215 Then PosY=215
     ColorVal(SliderArrayMain,SliderArraySub)= 470-PosY
     SliderPosY(SliderArrayMain,SliderArraySub)=PosY

     [drawSliders]
     'discard all previous slider drawing
     print #main.graph, "discard"
     for Ma=1 to 8
     for Su=1 to 3
            'draw slider backgound
            print #main.graph, "place ";SliderLocation(Ma,Su);" 210"
            print #main.graph, "backcolor darkgray"
            print #main.graph, "boxfilled ";SliderLocation(Ma,Su)+20;" 475"
            'drawcoloured lines on sliders
            print #main.graph, "place ";SliderLocation(Ma,Su)+8;" 210"
            print #main.graph, "backcolor ";Color$(Su)
            print #main.graph, "boxfilled ";SliderLocation(Ma,Su)+12;" 475"
            'print slider values above sliders
            print #main.graph, "backcolor white"
            print #main.graph, "color white"
            print #main.graph, "place ";SliderLocation(Ma,Su)-2;" 190"
            print #main.graph, "\";ColorVal(Ma,Su)

            print #main.graph, "color black"
            print #main.graph, "place ";SliderLocation(Ma,Su)-2;" 190"
            print #main.graph, "\";ColorVal(Ma,Su)
            'draw slider buttons
            print #main.graph, "place ";SliderLocation(Ma,Su);" ";SliderPosY(Ma,Su)-5
            print #main.graph, "backcolor ";Color$(Su)
            print #main.graph, "boxfilled ";SliderLocation(Ma,Su)+20;" ";SliderPosY(Ma,Su)+5
        'draw circles
        print #main.graph, "place ";LampLocation(Ma);" 100"
        print #main.graph, "backcolor ";ColorVal(Ma,1);" ";ColorVal(Ma,2);" ";ColorVal(Ma,3)
        print #main.graph, "circlefilled ";LampRadius
        next Su
        next Ma
        wait

    [exitClicked]
    timer 0
    close #main
    end



Guru ( offline )
Posts: 2251
Posted: 2008-03-22 14:39 
Some button pseudo code made to control motors using a mouse. Depressing the left mouse button in the gaphic box causes one action. Releasing the mouse buttion causes another action. This is quicker than clicking a button to start a motor and then having to quickly reclick a button to stop the motor.

Code:
 open "Move something!" for graphics as #w
  print #w, "when leftButtonDown [Move]"

  wait
[Move]
  print #w, "\Moving Left"
  print #w, "when leftButtonUp [Stop]"
  wait
[Stop]
  print #w, "\Now Stopped"
  wait


Code:
 'Stylebits #demo.btn1, 0, _WS_BORDER, 0, 0
    Graphicbox #demo.btn1, 100, 100, 80, 80
    Open "Pseudo Buttons" for Window as #demo
    #demo, "Trapclose XbyTrap"
    #demo.btn1, "Down; Fill Buttonface"
    #demo.btn1, "Color Black; Backcolor 0 255 0"
    #demo.btn1, "Place 0 0; Boxfilled 80 80; Flush"
    #demo.btn1, "When leftButtonDown MotorOn"
    #demo.btn1, "When leftButtonUp MotorOff"

Wait

Sub XbyTrap handle$
    Close #demo
    End
End Sub

Sub MotorOn handle$, xVar, yVar
    #demo.btn1, "Backcolor 255 0 0"
    #demo.btn1, "Place 0 0; Boxfilled 80 80; Discard"
    'Playwave "annoyingsound.wav", Loop
    print "MotorOn"
End Sub

Sub MotorOff handle$, xVar, yVar
    #demo.btn1, "Backcolor 0 255 0"
    #demo.btn1, "Place 0 0; Boxfilled 80 80; Discard"
    'Playwave ""
    print "MotorOff"
End Sub



Guru ( offline )
Posts: 2251
Posted: 2008-07-14 19:46 
Some test code I'm trying that controls a servo with a gamepad. If I can get a USB joystick I'll modify the code for use with that.

Code:
'comport commented out for testing
'nomainwin 'remove ' for no main display
open "joystick demo" for window as #joy
'OPEN "com4:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
#joy "trapclose [quit]"

x1 = 1500 ' set an initial position for servos
y1 = 1500

[refresh]
print "================================="
'timer 2000, [readStick] 'every 2 sec read the gamepad for testing
timer 200, [readStick] 'every 200 ms read the gamepad
wait
[readStick]
timer 0

scan
readjoystick 1 'either 1 or 2

'print "1x = ";Joy1x 'joystick 1 x value
'print "1y = ";Joy1y 'joystick 1 y value

'print " button 1 = "; Joy1button1 'button position, either 0 or 1
'print " button 2 = "; Joy1button2 'button position, either 0 or 2

if Joy1x > 34000 then xh = 10 ' change signs +/- to reverse
if (Joy1x < 34000) and (Joy1x > 30000) then xh = 0 ' servo rotation direction
if Joy1x < 30000 then xh = -10
if Joy1button1 = 1 then xh = xh * 10 'turbo 10x mode
'print "xh = "; xh

if Joy1y > 34000 then yh = 10 ' change signs +/- to reverse
if (Joy1y < 34000) and (Joy1y > 30000) then yh = 0 ' servo rotation direction
if Joy1y < 30000 then yh = -10
if Joy1button1 = 1 then yh = yh * 10 'turbo 10x mode
'print "yh = "; yh

x1 = (x1 + xh)
y1 = (y1 + yh)

'print "x1 = "; x1
'print "y1 = "; y1

'print ""

if x1 > 2500 then x1 = 2500
if x1 < 500 then x1 = 500
if y1 > 2500 then y1 = 2500
if y1 < 500 then y1 = 500

x1$ = str$(x1)
y1$ = str$(y1)

if x1$ = a1$ then xx$ = "" else xx$ = "0P" + x1$
if y1$ = b1$ then yy$ = "" else yy$ = "1P" + y1$

a1$ = x1$
b1$ = y1$

'print xx$
'print yy$

zz$ = xx$ + yy$

if zz$ = "" then print "no change"
if zz$ = "" then goto [refresh]

print "#" + xx$ + yy$
'print #2, "#" + xx$ + yy$
print ""

goto [refresh]

[quit]
'CLOSE #2
close #joy
end


Guru ( offline )
Posts: 2251
Posted: 2008-12-23 13:33 
Below is some code for tinkering with contact sensors, foot switches and such, for stopping a servo when contact is made and then getting the last position it was sent to. To try this, connect a jumper to the "C" input and then touch the jumper to ground while the servo is moving (simulating a switch closure).

Code:
'ssc-32 (SSC32-V2.01XE) servo control using Just Basic.
'3 second timed move, stopped when "C" digital input is
'grounded and reads "0" (foot switch, etc.).

open "Com10:9600,n,8,1,ds0,cs0,rs" for random as #comm

Print #comm, "#0P2500" 'put servo to initial position

y=0 'outside loop counter set to 0

[loop]

timer 2000, [delay2] 'iniial delay before timed move
wait
[delay2]
timer 0

x=0   'inside loop counter set to 0


Print #comm, "#0P500T3000" 'start 3 second servo 0 move to 500

[loop1] 'inside loop

x=x+1

'y$ = "VC" 'used for analog input
y$ = "C" 'digital input

Print #comm, y$ 'request digital input value from ssc-32

timer 50, [delay] 'read return delay
wait
[delay]
timer 0

dataRead$ = input$(#comm, lof(#comm))

print dataRead$ 'either 0 or 1

'num = ASC(dataRead$)'used for analog data
'print "num is "; num

if dataRead$ = "0" then Print #comm, "STOP 0" 'stops servo movement
if dataRead$ = "0" then Print "STOP 0" 'prints data value (0 or 1)

if x < 60 then [loop1] 'set for number of samples during move

Print #comm, "QP0" 'get last position sent to servo

timer 50, [delay3] 'delay to allow data to be returned
wait
[delay3]
timer 0

dataRead$ = input$(#comm, lof(#comm)) 'get returned data

print "dataRead$ is "; dataRead$ 'print out raw servo position
print "servo position is "; ASC(dataRead$)*10 'convert position into ms

Print #comm, "#0 P2500" 'send servo to start position

print ""

y=y+1

if y < 2 then [loop] 'set for number of loops

print "done"

Close #comm


Another version with a more useable printout:

Code:
'ssc-32 (SSC32-V2.01XE) servo control using Just Basic.
'3 second timed move, stopped when "C" digital input is
'grounded and reads "0" (foot switch, etc.).

oncomerror [handleIt]
open "Com10:9600,n,8,1,ds0,cs0,rs" for random as #comm
Print #comm, "#0P2500" 'put servo to initial position
y=1 'outside loop counter set to 0
[loop]
print "step ";y
print "starting 2 second start delay"
timer 2000, [delay2] 'iniial delay before timed move
wait
[delay2]
timer 0
x=0   'inside loop counter set to 0
Print #comm, "#0P500T6000" 'start 3 second servo 0 move to 500
[loop1] 'inside loop
x=x+1
'y$ = "VC" 'used for analog input
y$ = "C" 'digital input
Print #comm, y$ 'request digital input value from ssc-32
timer 50, [delay] 'read return delay
wait
[delay]
timer 0
dataRead$ = input$(#comm, lof(#comm))
'print dataRead$ 'either 0 or 1
'num = ASC(dataRead$)'used for analog data
'print "num is "; num
if dataRead$ = "0" then Print #comm, "STOP 0" 'stops servo movement
'if dataRead$ = "0" then Print "STOP 0" 'prints data value (0 or 1)
if x < 60 then [loop1] 'set for number of samples during move
Print #comm, "QP0" 'get last position sent to servo
timer 50, [delay3] 'delay to allow data to be returned
wait
[delay3]
timer 0
dataRead$ = input$(#comm, lof(#comm)) 'get returned data
print "position byte is "; dataRead$ 'print out raw servo position
print "servo position is "; ASC(dataRead$)*10 'convert position into ms
Print #comm, "#0 P2500" 'send servo to start position
print ""
y=y+1
if y < 11 then [loop] 'set for number of loops
print "done"
Close #comm
end
[handleIt]
print "Comport error! Check settings and status."


The simple contact sensor setup: servo, popcicle stick attached with two thumb tacks, paperclip with jumper to "C" input on ssc-32, and aluminum foil contact pad with jumper connected to ssc-32 ground.


Inexpensive USB hub powering ssc-32/servo, with USB to serial adapter connected ssc-32:


Last edited by zoomkat on 2009-10-28 04:16, edited 1 time in total.

Guru ( offline )
Posts: 2251
Posted: 2008-12-26 15:56 
Another code variation using analog voltage input to the ssc-32 to stop the servo movement and then get the servo position. Might be useful in testing sensors that have analog outputs and are moved by servos until an output threshold is reached. Bottom is the output when the paperclip on my popcicle stick servo arm touches the aluminum foil and the servo is stopped.

Code:
'ssc-32 (SSC32-V2.01XE) servo control using Just Basic.
'6 second timed move, stopped when "VC" analog input is
'grounded and reads less than "228" (foot switch, etc.).
'22k resistor connected between +5v and "C" input
'to keep "C" high at +5v (~"255" value) until grounded.

oncomerror [handleIt]
'set comport value to the one you use
open "Com1:9600,n,8,1,ds0,cs0,rs" for random as #comm
Print #comm, "#0P2200" 'put servo to initial position
y=1 'outside loop counter set to 0
[loop]
print "step ";y
print "starting 2 second start delay"
timer 2000, [delay2] 'iniial delay before timed move
wait
[delay2]
timer 0
x=0   'inside loop counter set to 0
Print #comm, "#0P700T6000" 'start timed servo 0 move to 700
[loop1] 'inside loop
x=x+1
y$ = "VC" 'used for analog input
Print #comm, y$ 'request digital input value from ssc-32
timer 20, [delay] 'read return delay
wait
[delay]
timer 0
dataRead$ = input$(#comm, lof(#comm))
num = ASC(dataRead$)'used for analog data
if num < 228 then Print #comm, "STOP 0"
if num < 228 then [jump]
if x < 160 then [loop1] 'set for number of samples during move
[jump]
Print #comm, "QP0" 'get last position sent to servo
timer 50, [delay3] 'delay to allow data to be returned
wait
[delay3]
timer 0
dataRead$ = input$(#comm, lof(#comm)) 'get returned data
print "voltage byte is "; num 'print out raw servo position
print "position byte is "; dataRead$ 'print out raw servo position
print "servo position is "; ASC(dataRead$)*10 'convert position into ms
timer 500, [delay4] 'delay to allow data to be returned
wait
[delay4]
timer 0
Print #comm, "#0 P2200" 'send servo to start position
print ""
y=y+1
if y < 11 then [loop] 'set for number of loops
print "done"
Close #comm
end
[handleIt]
print "Comport error! Check settings and status."



step 1
starting 2 second start delay
voltage byte is 19
position byte is Â


 

All times are UTC - 5 hours [ DST ]. It is currently 2014-07-24 04:24
Feedback Form
Feedback Form