How can I clean up my code?


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    425

    Default How can I clean up my code?

    I have a program where I'm sending three numbers out of the UART port. An example would be:

    in1 - 10
    in2 - 20
    in3 - 5

    On the transmitting side, the code is short and works fine. One the receiving side it's very inefficient. It seems like the case/select case would work better but I can't figure out how to implement it correctly. When I receive the numbers, it goes like this:

    Code:
    if b1=0 and b2=1 and b3=11    THEN GOTO ON1
       if b1=0 and b2=1 and b3=12    THEN GOTO OFF1
       if b1=0 and b2=3 and b3=11    THEN GOTO ON2
       if b1=0 and b2=3 and b3=12    THEN GOTO OFF2
       if b1=0 and b2=6 and b3=11    THEN GOTO ON3
       if b1=0 and b2=6 and b3=12    THEN GOTO OFF3
       if b1=0 and b2=5 and b3=11    THEN GOTO ON4
       if b1=0 and b2=5 and b3=12    THEN GOTO OFF4
    How can I get the PIC to just look at the three numbers and execute automatically, if that's possible? I have to sort through almost 100 different number combinations and the code is taking up way too much space. Is there an easier way?

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: How can I clean up my code?

    Use Select Case, check the value in B0 and go as you need to to check b1 and again direct until you check variable b2 OR
    use 3 lookup tables to direct your code
    Last edited by Archangel; - 18th June 2012 at 23:45.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts