Shop OBEX P1 Docs P2 Docs Learn Events
Can someone help me shorten this code up pls? — Parallax Forums

Can someone help me shorten this code up pls?

cabojoecabojoe Posts: 72
edited 2004-07-29 03:01 in BASIC Stamp
· This code works fine, but I know it could be shorter using select case or something (which I can't make my braincell fit around). Could someone run over this and explain to me what I can do to make it shorter and sweeter please?
· The program uses an LTC 1298 adc to read x/y axis of joystick, then sends control bytes out a Surelink to the Bot.

·Thanks cool.gif

'{$STAMP BS2}
'{$PBASIC 2.5}
' Program: JoeControlBoard.BS2 (LTC1298 analog-to-digital converter,Surelink)
' Transmits via Surelink 2 direction control bytes to bot derived from adc hooked up to joystick.
' The transmitter side should be running JoeControlBoard.BS2.
' Receiver/Tranmitter should be running Transceiver1.BS2.
' Receiver should be running SerialReceiver1.BS2

' ==========================================================
' Set up Program Variables and Constants
' ==========================================================

config······· VAR··· Nib················ ·' Configuration bits for ADC.
startB······ ·VAR··· config.BIT0····· ·' Start bit for comm with ADC.
sglDif······· ·VAR··· config.BIT1······ ' Single-ended or differential mode.
oddSign····· VAR··· config.BIT2······ ' Channel selection.
msbf········ ·VAR··· config.BIT3····· ·' Output 0s after data xfer complete.
Value········ VAR··· Word·············· ' Variable to hold 12-bit AD result.
ValueBit···· ·VAR··· Value.BIT0
Idx············ VAR··· Byte
baud··········CON··· $4054········· ·· ' lcd baud
sbaud······· ·CON··· $4054········· ·· ' Baudrate for Surelink
Dout········· ·CON··· 0················· ·' Data out to Surelink
Din·········· · CON··· 1················· ·' Data in from Surelink
Link·········· ·CON··· 2················· ·' Surelink link indicator
Tx············ ·CON··· 8
lcd··········· ·CON··· 9
CS············ CON··· 13··········· ·· · ' Chip select; 0 = active
CLK········· · CON··· 14·············· · ' Clock to ADC; out on rising, in on falling edge.
DIO_n······· ·CON··· 15··············· ·' Data I/O pin _number_.

' ==========================================================
' Initialize
' ==========================================================

SEROUT lcd,baud,[noparse][[/noparse]12,14,"JoeControlBoard"]
PAUSE 2000
SEROUT lcd,baud,[noparse][[/noparse]12,"Ch0:",13,"Ch1:"]
HIGH Dout
GOSUB Chklink

' ==========================================================
' Main
' ==========================================================

HIGH CS·································· ·· ' Deactivate ADC to begin.
HIGH DIO_n································· ' Set data pin for first start bit.
main:
··· oddSign = 0··························· ·· ' channel 0
··· GOSUB convert························· ' Get data from ADC.
··· GOSUB Assign0························· ' Make into a byte
··· GOSUB Transmit························ ' Send
··· SEROUT lcd,baud,[noparse][[/noparse]16,69,18,5,DEC4 Value]

··· oddsign = 1····························· ·' channel 1
··· GOSUB convert························· ' Get data from ADC
··· GOSUB Assign1························· ' Make into a byte
··· GOSUB Transmit1······················· ' Send
··· SEROUT lcd,baud,[noparse][[/noparse]16,85,18,5,DEC4 Value]
GOTO main·································· ·' Endless loop.

' ==========================================================
'···················· ADC Subroutine
' ==========================================================

convert:
· config = config | %1011························· ' Set all bits except oddSign.
· LOW CS············································· ··' Activate the ADC.
· SHIFTOUT DIO_n,CLK,LSBFIRST,[noparse][[/noparse]config\4]· ' Send config bits.
· SHIFTIN DIO_n,CLK,MSBPOST,[noparse][[/noparse]Value\12]··· ' Get data bits.
· HIGH CS············································· ·' Deactivate the ADC.
·RETURN··············································· ·' Return to program.

' ==========================================================
' Transmit Routine for Surelink
' ==========================================================

Transmit:
·SEROUT Dout,sbaud,[noparse][[/noparse]"x",value]··········· ' Out to Surelink
RETURN·········································· ·' Send "x" first to sync with receiver, then data
····················································
Transmit1:
·SEROUT Dout,sbaud,[noparse][[/noparse]"y",value]··········· ' Out to Surelink
RETURN··········································· ' Send "y" to sync with receiver, then data··

' ==========================================================
' Assign simple values to ADC results
' ==========================================================
Assign0:························································· ·· 'Channel 0
·· IF (value >= 2000) AND (value <= 2500) THEN····· · 'Neutral
·· GOSUB neutral0
·· ELSEIF (value > 2500) AND (value <= 3000) THEN··· 'Slow forward
·· GOSUB Forward1
·· ELSEIF (value > 3000) AND (value <= 3500) THEN··· 'Med slow forward
·· GOSUB Forward2
·· ELSEIF (value > 3500) AND (value <= 4000) THEN··· 'Med fast forward
·· GOSUB Forward3
·· ELSEIF (value > 4000) THEN······························ · 'Fast forward
·· GOSUB Forward4
·· ELSEIF (value < 2000) AND (value >= 1500) THEN··· 'Slow Back
·· GOSUB Back1
·· ELSEIF (value < 1500) AND (value >= 1000) THEN··· 'Med slow back
·· GOSUB Back2
·· ELSEIF (value < 1000) AND (value >= 500) THEN···· ·'Med fast back
·· GOSUB Back3
·· ELSEIF (value < 500) THEN································ · 'Fast back
·· GOSUB back4
·· ENDIF
·RETURN

Assign1:···························································· · 'Channel 1
·· IF (value >= 2000) AND (value <= 2500) THEN······· ··'Neutral
·· GOSUB neutral0
·· ELSEIF (value > 2500) AND (value <= 3000) THEN··· ·'Slow left
·· GOSUB Left1
·· ELSEIF (value > 3000) AND (value <= 3500) THEN·· · 'Med slow left
·· GOSUB Left2
·· ELSEIF (value > 3500) AND (value <= 4000) THEN···· 'Med fast left
·· GOSUB Left3
·· ELSEIF (value > 4000) THEN································ ·'Fast left
·· GOSUB Left4
·· ELSEIF (value < 2000) AND (value >= 1500) THEN·· · 'Slow right
·· GOSUB Right1
·· ELSEIF (value < 1500) AND (value >= 1000) THEN··· ·'Med slow right
·· GOSUB Right2
·· ELSEIF (value < 1000) AND (value >= 500) THEN···· ·'Med fast right
·· GOSUB Right3
·· ELSEIF (value < 500) THEN································· ·'Fast right
·· GOSUB Right4
·· ENDIF
·RETURN

' ==========================================================
' Assign Subroutines
' ==========================================================

'Channel 1
Neutral0:········ ·'Neutral
· value = 1
RETURN

Forward1:······· 'Slow forward
· value = 2
RETURN

Forward2:······· 'Med slow forward
· value = 3
RETURN

Forward3:······· 'Med fast forward
· value = 4
RETURN

Forward4:······· 'Fast forward
· value = 5
RETURN

Back1:··········· ·'Slow back
· value = 6
RETURN

Back2:··········· ·'Med slow back
· value = 7
RETURN

Back3:·········· · 'Med fast back
· value = 8
RETURN

Back4:··········· ·'Fast back
· value = 9
RETURN

'Channel 2
Neutral1:······· ·'Neutral
· value = 1
RETURN

Left1:··········· ·'Slow left
· value = 2
RETURN

Left2:·········· · 'Med slow left
· value = 3
RETURN

Left3:·········· · 'Med fast left
· value = 4
RETURN

Left4:··········· ·'Fast left
· value = 5
RETURN

Right1:·········· ·'Slow right
· value = 6
RETURN

Right2:········· · 'Med slow right
· value = 7
RETURN

Right3:·········· ·'Med fast right
· value = 8
RETURN

Right4:·········· ·'Fast right
· value = 9
RETURN

ChkLink:
·SEROUT lcd,baud,[noparse][[/noparse]"?"]···················· ' Print a .if the units are not linked.
·IF Link = 0 THEN ChkLink················· ' Once the units link, then
·SEROUT lcd,baud,[noparse][[/noparse]12,"*****LINKED*****"]
·PAUSE 500·································· ·' pause to read
RETURN

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-07-28 14:35
    LOOKUP is your friend. Try something like this:

    Assign0:
    · LOOKUP (value / 500), [noparse][[/noparse]9, 8, 7, 6, 5, 4, 3, 2, 1], value
    · RETURN

    Be sure to read the help file section on LOOKUP so all of this make sense -- but as you can see, one line code using LOOKUP can replace a bunch of IF-THEN statements and GOSUBs.

    Not bad, huh? PBASIC rules!· yeah.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 7/28/2004 2:38:26 PM GMT
  • Tracy AllenTracy Allen Posts: 6,663
    edited 2004-07-28 14:44
    Well, here is one thing. The Assign subroutines depend on multiples of 500. So you might have something like this:
    assign1:
    x = value/500 max 8
    ON x GOSUB right4,right3,right2,right1,neutral0,left1,left2, left3,left4
    RETURN

    Each one of those subroutines just assigns a new value, so that could be simplified further to eliminate the subroutines altogether:

    assign1:
    x = value/500 max 8
    LOOKUP x,[noparse][[/noparse]9,8,7,6,1,2,3,4,5],value
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • cabojoecabojoe Posts: 72
    edited 2004-07-29 03:01
    Well thank you both very much...I'll get to studying. BTW both of you are my heros. I read as much of both of you as I can find.smile.gif I'll let you know when I got something better to show you.
    Thanks mucho
Sign In or Register to comment.