Shop OBEX P1 Docs P2 Docs Learn Events
Brute Force Vs Elegant Programming... How do I make the elegant version work? — Parallax Forums

Brute Force Vs Elegant Programming... How do I make the elegant version work?

latigerlillylatigerlilly Posts: 114
edited 2007-02-13 21:20 in BASIC Stamp
Hi guys,

I wrote a program for the BS2P40 on the PDB to display "dog" on its 7 segment LED display as a default. When button 1 is pressed, it will display god for 1 second. When button 2 is pressed, it will display odd for 1 second. This first program works, but it is brute force computer programming. When you read it, you'll know what I mean.

The second post here in this thread has a more elegant computer program, but it doesn't work. The default only displays just the letter "d". When button 1 is pressed, it only displays "8" permenantly. When button 2 is pressed, it experiences no change.

How do I make the more elegant computer program work? What did I do wrong?

Also, I thought that a more elegant computer program will use less memory, but I checked and the more elegant version used 24% of the memory while the brute force computer program only used 19% of available memory. It doesn't make sense. I mean, wouldn't typing GOSUB X a million times take more memory than using a simple set of instructions set in EEPROM ?

Thanks a million,
Lilly. tongue.gif

Here is the brute force program that works:

' Learning on the PDB - experimental.bs2p

' Spells "dog" on the 7 segment LED display on the PDB as the default.
' Spells "god" on the 7 segment LED display on the PDB when button 1 connected to RB0 is pressed.
' Spells "odd" on the 7 segment LED display on the PDB when button 2 connected to RB1 is pressed.
' 7 segment LED display has digits 4, 3, and 2 used on X19.
' 7 segment LED display has segments A through G used on X18. Segment DP is not used.
' Digits 4, 3, and 2 are connected to P5, P4, and P3, respectively.
' Segments A, B, C, D, E, and F are connected to P6, P7, P8, P9, P10, and P11, respectively.
' Segment G is connected to P14.

' {$STAMP BS2p}
' {$PBASIC 2.5}

DEBUG "Program Running!"

counter VAR Word

GOSUB l4

DO
MAINIO
IF (IN8 = 0) THEN

' If button 1 is pressed then display god

FOR counter = 1 TO 33
GOSUB hg
PAUSE 5
GOSUB h4
GOSUB l3
GOSUB lg

GOSUB ho
PAUSE 5
GOSUB h3
GOSUB l2
GOSUB lo

GOSUB hd
PAUSE 5
GOSUB h2
GOSUB l4
GOSUB ld
NEXT

ELSEIF (IN9 = 0) THEN

' If button 2 is pressed then display odd

FOR counter = 1 TO 33
GOSUB ho
PAUSE 5
GOSUB h4
GOSUB l3
GOSUB lo

GOSUB hd
PAUSE 5
GOSUB h3
GOSUB l2
GOSUB ld

GOSUB hd
PAUSE 5
GOSUB h2
GOSUB l4
GOSUB ld
NEXT

ELSE

'If no button is pressed then display dog.

GOSUB hd
PAUSE 5
GOSUB h4
GOSUB l3
GOSUB ld

GOSUB ho
PAUSE 5
GOSUB h3
GOSUB l2
GOSUB lo

GOSUB hg
PAUSE 5
GOSUB h2
GOSUB l4
GOSUB lg

ENDIF
LOOP

' Sets digit 4 of display on vss.
l4:
MAINIO
LOW 5
RETURN

' Sets digit 3 of display on vss.
l3:
MAINIO
LOW 4
RETURN

' Sets digit 2 of display on vss.
l2:
MAINIO
LOW 3
RETURN

' Sets digit 4 of display on vdd.
h4:
MAINIO
HIGH 5
RETURN

' Sets digit 3 of display on vdd.
h3:
MAINIO
HIGH 4
RETURN

'Sets digit 2 of display on vdd.
h2:
MAINIO
HIGH 3
RETURN

' Sets segments of display for the letter d on vdd.
hd:
MAINIO
HIGH 7
AUXIO
HIGH 14
HIGH 10
HIGH 8
HIGH 9
RETURN

' Sets segments of display for the letter d on vss.
ld:
MAINIO
LOW 7
AUXIO
LOW 14
LOW 10
LOW 8
LOW 9
RETURN

' Sets segments of display for the letter o on vdd.
ho:
AUXIO
HIGH 14
HIGH 10
HIGH 8
HIGH 9
RETURN

' Sets segments of display for the letter o on vss.
lo:
AUXIO
LOW 14
LOW 10
LOW 8
LOW 9
RETURN

' Sets segments of display for the letter g on vdd.
hg:
MAINIO
HIGH 6
HIGH 7
AUXIO
HIGH 14
HIGH 11
HIGH 8
HIGH 9
RETURN

' Sets segments of diplay for the letter g on vss.
lg:
MAINIO
LOW 6
LOW 7
AUXIO
LOW 14
LOW 11
LOW 8
LOW 9
RETURN

Pics;
Image013.jpg
Image014-1.jpg
Image005.jpg
Image020.jpg

Post Edited (latigerlilly) : 2/5/2007 4:13:19 PM GMT

Comments

  • latigerlillylatigerlilly Posts: 114
    edited 2007-02-05 08:01
    This is the elegant program that doesn't work:

    ' Learning on the PDB - god.bs2p

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}

    DEBUG "Program Running!"

    counter VAR Word
    tracker VAR Word
    track VAR Word
    instruction VAR Byte
    counter2 VAR Word
    counter3 VAR Word

    DATA "Gp83gOp92oDp64dOp83oDp92dDp64dDp83dOp92oGp64g"

    GOSUB l4

    DO
    MAINIO
    IF (IN8 = 0) THEN

    ' If button 1 is pressed then display god

    tracker = 0

    ELSEIF (IN9 = 0) THEN

    ' If button 2 is pressed then display odd

    tracker = 15

    ELSE

    ' If no button is pressed then display dog

    tracker = 30

    ENDIF

    track = tracker + 14

    IF (tracker < 30) THEN
    counter2 = 33
    ELSE
    counter2 = 1
    ENDIF

    FOR counter = tracker TO track
    FOR counter3 = 1 TO counter2
    READ tracker, instruction
    SELECT instruction
    CASE "9": GOSUB h3
    CASE "8": GOSUB h4
    CASE "6": GOSUB h2
    CASE "3": GOSUB l3
    CASE "4": GOSUB l4
    CASE "2": GOSUB l2
    CASE "G": GOSUB hg
    CASE "O": GOSUB ho
    CASE "D": GOSUB hd
    CASE "g": GOSUB lg
    CASE "o": GOSUB lo
    CASE "d": GOSUB ld
    CASE "p": GOSUB p
    ENDSELECT
    NEXT

    NEXT

    LOOP

    END

    l4:
    MAINIO
    LOW 5
    RETURN

    l3:
    MAINIO
    LOW 4
    RETURN

    l2:
    MAINIO
    LOW 3
    RETURN

    h4:
    MAINIO
    HIGH 5
    RETURN

    h3:
    MAINIO
    HIGH 4
    RETURN

    h2:
    MAINIO
    HIGH 3
    RETURN

    hd:
    MAINIO
    HIGH 7
    AUXIO
    HIGH 14
    HIGH 10
    HIGH 8
    HIGH 9
    RETURN

    ld:
    MAINIO
    LOW 7
    AUXIO
    LOW 14
    LOW 10
    LOW 8
    LOW 9
    RETURN

    ho:
    AUXIO
    HIGH 14
    HIGH 10
    HIGH 8
    HIGH 9
    RETURN

    lo:
    AUXIO
    LOW 14
    LOW 10
    LOW 8
    LOW 9
    RETURN

    hg:
    MAINIO
    HIGH 6
    HIGH 7
    AUXIO
    HIGH 14
    HIGH 11
    HIGH 8
    HIGH 9
    RETURN

    lg:
    MAINIO
    LOW 6
    LOW 7
    AUXIO
    LOW 14
    LOW 11
    LOW 8
    LOW 9
    RETURN

    p:
    PAUSE 5

    Post Edited (latigerlilly) : 2/5/2007 8:07:35 AM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-02-05 15:28
    "Elegance" is often in the eye of the beholder. Personally, "Simple and works" is usually enough for me.

    What specifically were you trying to achieve with your elegance? One usual goal is "more parameterized" -- where you give a subroutine a "g" and a letter position, and the subroutine displays the letter where you want it -- perhaps by calling more subroutines in the process.

    Right now, I can't figure out how your 'brute force' code works, much less your "elegant" version.
  • TheBestTheBest Posts: 9
    edited 2007-02-05 15:45
    thanks for the answer on topic
    http://forums.parallax.com/forums/default.aspx?f=5&m=169907
    i see i have some homework to do
    its not so easy if i though it was
    but you didnt do a dot matrix
    so now i try to use your code to make my one code

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I am from HOLLAND!!!!!

    sorry for my bad english [noparse];)[/noparse]
  • latigerlillylatigerlilly Posts: 114
    edited 2007-02-05 16:14
    TheBest,

    A dot matrix (what you did) is the same as a LED matrix (what I did). The LEDs are just bigger "dots".

    Allanlane5,

    I am practicing writing programs that use less memory and are easier to write. That's why I am using subroutines, so that I don't have to write the same set of codes over and over again. I have added better documentation in my code. Please look over it for me.

    Thanks guys,
    Lilly. wink.gif

    Post Edited (latigerlilly) : 2/5/2007 4:18:28 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-02-05 20:27
    There is always a threshold before an ‘elegant’ program will show any real savings. For example, take a program that does, DEBUG “HELLOW WORLD!” If you write the code to make it more elegant and put the text into a DATA statement and read it out character by character with the DEBUG in a subroutine you would find that for something this simple the first method was much smaller.

    Now take the example where you have dozens of DEBUG lines spitting out TEXT messages. Now the same program will be much smaller because each DEBUG statement uses memory, but using the same one over and over saves space.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2007-02-05 23:41
    If you want a very good introductory college-level computer science course, here it is:

    http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-001Spring-2005/CourseHome/index.htm

    Everything you need to do it is freely available online - the development IDE (use DrScheme from PLT), the lecture notes, the book, and if you Google the title, you'll also find a set of video-taped lectures. You won't get academic credit for doing the work, but you will learn a lot.
  • aquasapienaquasapien Posts: 25
    edited 2007-02-13 21:20
    If brute force is not working....you aint using enough force!!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The singularity is close at hand, Genetics, Nanotechnology, Robotics...and I get to be alive to see it.
Sign In or Register to comment.