Shop OBEX P1 Docs P2 Docs Learn Events
Can this code be more compact / more elegant? — Parallax Forums

Can this code be more compact / more elegant?

John KauffmanJohn Kauffman Posts: 653
edited 2006-11-27 16:58 in General Discussion
Can this code be more compact / more elegant?

Below is some SX/B code that is very easy to understand; it flashes an LED if both switches are closed.
Now I want to get it into as few lines (and as elegant) as possible. Any suggestions?
Is there some way to do this with a variable sized bit instead of byte?
Can I avoid the variable completely?
·
' code to flash MyLed when Switch1 AND Switch2 are closed
' Switch· pins use internal pull-up so open = 1
·
' IO Pins
MyLed······ ····· PIN·· RC.0
Switch1·········· pin·· RB.1
Switch2·········· pin·· RB.2
·
' Variables
SwitchTotal ····· var·· byte
·
Main:
SwitchTotal = 0
SwitchTotal = SwitchTotal + Switch1
SwitchTotal = SwitchTotal + Switch2
IF SwitchTotal = 2 THEN
····· HIGH MyLed
····· Pause 500
····· HIGH MyLed
····· Pause 500
ENDIF
GOTO Main

Comments

  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-11-27 06:04
    You could use the AND (&) operator on the switch values. See the SX/B help file for an example.

    Although you are looking for the least amount of code, you might want to consider putting your LED code in a seperate subroutine. This increases size, but adds clarity and reusability.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2006-11-27 06:50
    AND sounds like the right track, but doesn't AND only work for bit - sized ?
    I've fooled around with AND but can't figure out how to differentiate oe switc closed and two switches closed
    To get from 0 up to 2 wouldn't I need byte sizes?
    I see I can do both additions on one line, but I still need byte size variable, no?

    Could point on the LED code, thanks.
  • John KauffmanJohn Kauffman Posts: 653
    edited 2006-11-27 06:53
    ALSO... What if switch normal is reversed form my original question. Because if using pull-ups switch open would be 1
    So how to most efficiently code for following (uses subtraction)

    Main:
    SwitchTotal = 2
    SwitchTotal= SwitchTotal- swith1
    SwitchTotalvar = SwitchTotal - switch2
    IF SwitchTotal = 0 THEN (LED flash)

    THanks guys.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2006-11-27 07:33
    It shouldn't matter if it is reversed or not, as long as you check the proper condition. You don't need to initialize SwitchTotal, you can do something like this...

    For your original question....
    SwitchTotal = switch1 + switch2
    IF SwitchTotal = 2 THEN (LED flash)
    
    



    Or for your newest question....
    SwitchTotal = switch1 + switch2
    IF SwitchTotal = 0 THEN (LED flash)
    
    

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

    IC Layout Engineer
    Parallax, Inc.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-11-27 07:52
    Assuming that Switch1 and Switch2 can have a value of either 0 or 1:

    AND
    0 & 0 = 0
    0 & 1 = 0
    1 & 0 = 0
    1 & 1 = 1

    So, the answer can only be 0 or 1, based on the inputs. You need both inputs to be 1 to get a 1 on the output.


    If closing the switches produces a zero, you could use NOR (Not OR):

    NOR
    0 | 0 = 1
    0 | 1 = 0
    1 | 0 = 0
    1 | 1 = 0

    Here, both inputs need to be 0 for a 1 on the output. NOR isn't a keyword operator, so you would need to use OR + NOT:

    OR + NOT
    0 | 0 = ~0
    0 | 1 = ~1
    1 | 0 = ~1
    1 | 1 = ~1

    Generally speaking, in Boolean logic, 1 = On/Open/Yes/True/etc., and 0 = Off/Closed/No/False/etc. NOT inverts the value, so NOT 0 = 1, NOT 1 = 0. Boolean logic will always reference a value of 0 or 1.

    The counter that you're using is doing essentially the same thing as the Boolean logic: keeping track of the states of two inputs to determine an output value. Boolean logic also scales very well. If you had 100 switches ANDed, they would only produce a 1 output if ALL the inputs = 1.

    Here is a website that shows some basic gates: hyperphysics.phy-astr.gsu.edu/hbase/electronic/gate.html#c1
  • BeanBean Posts: 8,129
    edited 2006-11-27 12:20
    John,

    · I normally do "IF...AND" this way:

    ' code to flash MyLed when Switch1 AND Switch2 are closed
    ' Switch  pins use internal pull-up so open = 1
     
    ' IO Pins
    MyLed             PIN   RC.0 OUTPUT
    Switch1           PIN   RB.1 INPUT PULLUP
    Switch2           PIN   RB.2 INPUT PULLUP
     
    ' Constants
    CLOSED            CON 0 ' Value of pin when switch is closed
     
    ' Variables
     
    Main:
      IF Switch1 = CLOSED THEN
        IF Switch2 = CLOSED THEN
          HIGH MyLed
          PAUSE 500
          LOW MyLed ' I assume you meant to use LOW here ???
          PAUSE 500
        ENDIF
      ENDIF
    GOTO Main
    
    

    Bean.




    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "People who are willing to trade their freedom for·security deserve neither and will lose both." Benjamin Franklin


    Post Edited (Bean (Hitt Consulting)) : 11/27/2006 12:25:04 PM GMT
  • PJMontyPJMonty Posts: 983
    edited 2006-11-27 16:40
    John,

    As a general rule of thumb, "elegant" to me is code that I can easily read and understand years after I have written it without working hard. Programmers often try to squeeze code into tiny spaces (or make it super fast) without needing to.

    While your question sounds like a simple hypothetical exercise, I would like to suggest that unless code needs space or speed optimization, then don't bother. If the code works and is easy to read, then you have written excellent code.

    BTW, Bean's suggestion is a perfect example of a coding style that makes the code easier to comprehend. His use of the defined constant "CLOSED" means that the code reads even more like English. Remember, when reading source code, you become the CPU, since you have to parse the code and "run it in your head" to understand it. Bean's example is something you could read a hundred years from now and understand easily. It doesn't get much easier than "IF Switch1 = CLOSED THEN..." for human comprehension.
      Thanks, PeterM

    Post Edited (PJMonty) : 11/27/2006 7:36:47 PM GMT
  • John KauffmanJohn Kauffman Posts: 653
    edited 2006-11-27 16:58
    I completely agree with you, PJMonty; readability is king, especially for teaching, until some other factor encroaches.
    Bean is right on the money as well - I've made notes to change all of my code in the curret project to use self-explanatory constants.

    Elegent was probably the wrong word. Spending time visiting Microsoft is exciting, but it does leave one viewing the world from the perspective of squeezing speed out of billions of lines of code, at the cost of simplicity.

    In this particular case it seemed like there would be a simpler way to get the result. I wanted to be sure I didn't miss. But it sounds like the best bet is the nested IF or the "combine before IF" approaches.

    And Kevin, thanks for the review of logic states. I hadn't seen how NOR would solve the problem until you explained it.

    Thanks guys.
Sign In or Register to comment.