Shop OBEX P1 Docs P2 Docs Learn Events
Is thre a better way to code this..... reading 5 pings and converting the resu — Parallax Forums

Is thre a better way to code this..... reading 5 pings and converting the resu

DgswanerDgswaner Posts: 795
edited 2008-08-26 14:45 in Propeller 1
This Code works but it seems a bit bulky and does take a bit of time to process. I'd appreciate any ideas on how to make it work better.
It reads 5 pings, converts the distance from to a letter A-I. which correlates to an Image on my PINK, visually indicating how close objects are.

[b]PUB[/b] PINGs  | pinkrange, val ,index
   
    range[noparse][[/noparse]0] :=  ping.Inches(PING_Pina)                  ' Get Range In Inches
    range :=  ping.Inches(PING_Pinb)                  ' Get Range In Inches
    range :=  ping.Inches(PING_Pinc)                  ' Get Range In Inches
    range :=  ping.Inches(PING_Pind)                  ' Get Range In Inches
    range :=  ping.Inches(PING_Pine)                  ' Get Range In Inches
      'waitcnt(clkfreq / 10 + cnt)                         ' Pause 1/10 Second 
    pointer:= 0    
    [b]repeat[/b] index [b]from[/b] 0 to 4                           
        LCD.gotoxy(Index * 4, 0)                          ' Position Cursor              
        LCD.decx(range[noparse][[/noparse]Index], 3)                         ' Print Inches
        [b]if[/b] Range[noparse][[/noparse]index] > range[noparse][[/noparse]pointer]
          Pointer := index
       [b]IF[/b] Range[noparse][[/noparse]index] > 80
           Val := "I"
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 32
           Val := "H"
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 28
           Val := "G"    
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 24
           Val := "F" 
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 20
           Val := "E"
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 16
           Val := "D"
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 12
           Val := "C"
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 8
           Val := "B"
       [b]ELSEIF[/b]  Range[noparse][[/noparse]index] > 4
           Val := "A"    
       XB.dec (Index + 1)
       XB.tx (Val) 



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster

DGSwaner

Comments

  • TimmooreTimmoore Posts: 1,031
    edited 2008-08-26 05:15
    how about something like

    dat
    len byte 4,8,12,16,20,24,28,32,80,65535

    repeat i from 0 to 9
    if range[noparse][[/noparse]index] < len
    quit
    val := "A" + i - 1
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-08-26 05:18
    Dgswaner,

    I haven't tested it, but I would think that you could simplify all of your elseif to something like this...

           IF Range[noparse][[/noparse]index] > 80
               Val := "I"
           ELSE
               Val := "A" + Range[noparse][[/noparse]index]>>2
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • DgswanerDgswaner Posts: 795
    edited 2008-08-26 14:45
    Ahh, I need to remember the old shift right trick. very nice. Every time I see myself using a lot of IF's or ELSEs, I know there must be a better way.

    Thanks I'll give that a try when I get home.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "A complex design is the sign of an inferior designer." - Jamie Hyneman, Myth Buster

    DGSwaner
Sign In or Register to comment.