Shop OBEX P1 Docs P2 Docs Learn Events
Complex expression error — Parallax Forums

Complex expression error

MrRedHairMrRedHair Posts: 9
edited 2012-01-29 12:05 in BASIC Stamp
I was programming my boe-bot to be controlled by a normal IR remote control, one that normal goes to a a tv, this morning. After I finished typing the IF...THEN statements, I go to run the program and I get an error message that says "Expression is too complex". I've never seen this error before and would like some help on how to still upload my program to do what it should.

PS: It isn't set up to run the motors yet. Only post a sentence via DEBUG statements.
' {$STAMP BS2}
' {$PBASIC 2.5}
time      VAR     Word(5)
DEBUG "Program Initialization.", CR
FREQOUT 4, 800, 3000
DO
  DO
    RCTIME 9, 1, time(0)
  LOOP UNTIL time(0)> 1000
  PULSIN 9, 0, time(0) ' Measure/store data pulses.
  PULSIN 9, 0, time(1)
  PULSIN 9, 0, time(2)
  PULSIN 9, 0, time(3)
  PULSIN 9, 0, time(4)
  IF (time(4) < 400) THEN
    IF time(3) < 400) THEN
      IF (time(2) < 400) THEN
        IF (time(1) < 400) THEN
          IF (time(0) < 400) THEN
             DEBUG "The 1 button is being pressed.", CR
             PAUSE 750
          ELSEIF (time(0) > 600) THEN
             DEBUG "The 2 button is being pressed.", CR
             PAUSE 750
          ENDIF
        ELSEIF (time(1) > 600) THEN
          IF (time(0) < 400) THEN
             DEBUG "The 3 button is being pressed.", CR
             PAUSE 750
          ELSEIF (time(0) > 600) THEN
             DEBUG "The 4 button is being pressed.", CR
             PAUSE 750
          ENDIF
        ENDIF
      ELSEIF (time(2) > 600) THEN
        IF (time(1) < 400) THEN
          IF (time(0) < 400) THEN
             DEBUG "The 5 button is being pressed.", CR
             PAUSE 750
          ELSEIF (time(0) > 600) THEN
             DEBUG "The 6 button is being pressed.", CR
             PAUSE 750
          ENDIF
        ELSEIF (time(1) > 600) THEN
          IF (time(0) < 400) THEN
             DEBUG "The 7 button is being pressed.", CR
             PAUSE 750
          ELSEIF (time(0) > 600) THEN
             DEBUG "The 8 button is being pressed.", CR
             PAUSE 750
          ENDIF
        ENDIF
      ENDIF
    ELSEIF (time(3) > 600) THEN
      IF (time(2) < 400) THEN
        IF (time(1) < 400) THEN
          IF (time(0) < 400) THEN
             DEBUG "The 9 button is being pressed.", CR
             PAUSE 750
          ELSEIF (time(0) > 600) THEN
             DEBUG "The 0 button is being pressed.", CR
             PAUSE 750
          ENDIF
        ELSEIF (time(1) > 600) THEN
          IF (time(0) > 600) THEN
             DEBUG "The ENTER button is being pressed.", CR
             PAUSE 750
          ENDIF
        ENDIF
      ENDIF
    ENDIF
  ELSEIF (time(4) > 600) THEN
    IF (time(2) < 400) THEN
      IF (time(1) < 400) THEN
        IF (time(0) < 400) THEN
           DEBUG "The Ch+ button is being pressed.", CR
           PAUSE 750
        ELSEIF (time(0) > 600) THEN
           DEBUG "The Ch- button is being pressed.", CR
           PAUSE 750
        ENDIF
      ELSEIF (time(1) > 600) THEN
        IF (time(0) < 400) THEN
          DEBUG "The Vol+ button is being pressed.", CR
          PAUSE 750
        ELSEIF (time(0) > 600) THEN
          DEBUG "The Vol- button is being pressed.", CR
          PAUSE 750
        ENDIF
      ENDIF
    ENDIF
  ENDIF
LOOP

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-01-29 09:54
    Simplify your logic. I'm not sure why you need all the time(n) validation. Anyway, this is the same as the nested IFs.
      IF (time(4) < 400 AND time(3) < 400 AND time(2) < 400 AND time(1) < 400 AND time(3) < 400) THEN
        DEBUG "The 1 button is being pressed.", CR
        PAUSE 750
      ENDIF
    
  • MrRedHairMrRedHair Posts: 9
    edited 2012-01-29 10:01
    I figured it would be better for the controller to only check time(n) once, instead of checking it on every IF...THEN statement. With your example, very combination would be coded, around 25 checks of every time(n), whihc seemed unneeded to me.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-29 10:02
    The Stamp allows 16 levels of nested IF statements. While I don't see quite how that applies to your program listing, that must be what is going on. The Stamp internally has an expression stack that keeps track of the internal reference points and intermediate results of computations, and that message comes up when that stack overflows.

    It is an interesting puzzle, to restructure, given your time-weighted inputs. What happens if a button is pressed in the no-mans-land between 400 and 600? I would suggest forming a single integer with binary digits corresponding to the time values being on one side or the other of 500ms, and then work with that integer.
  • MrRedHairMrRedHair Posts: 9
    edited 2012-01-29 10:04
    The remote I am using sends either ~350 or ~650 on: 0-9, vol+/-, ch+/-, enter, power. I am sort of new to programming and don't know how to assignt a single integer binary digits corresponding to the values.. :/
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-29 10:10
    What I mean to say is something that ends up with an integer value, keys:
    keys=0
    PULSIN 9, 0, time ' Measure/store data pulses.
    IF time>500 THEN keys.bit0=1   
    PULSIN 9, 0, time
    IF time>500 THEN keys.bit1=1    ' etc.
    

    The time() values do not matter in the end.
  • MrRedHairMrRedHair Posts: 9
    edited 2012-01-29 10:14
    Hmm... I don't understand that..
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-29 10:20
    Suppose time(0) and time(4) are ~650, while the other times are ~350.

    Then the algorithm ends up with keys=%10001. If time(1) is also ~650, then the algorithm ends up with keys=%10011. The PBASIC syntax allows setting individual bits in a variable.
    keys.bit0=1.
  • MrRedHairMrRedHair Posts: 9
    edited 2012-01-29 10:25
    I guss it's my newness to programming, but wouldn't doing that still end up with all of the IF...THEN statements just as I have it now? It's just assigning the time(n) to binary instead. 1s and 0s instead of 600s and 400s.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-01-29 12:05
    Precisely, and those binary values are binary weighted.

    You end up with a numerical code from 0 to 31 for all the possiblities,

    DEBUG CR, "The key value is ", DEC keys

    So %00001 prints 1. Or %10011 prints 19. and so on. There may need to be an additional mapping step between the numerical value of keys and what gets printed. E.g. The keys=17 may map into a string of "channel" and a number, but from the keys value you can take advantage of LOOKUP and LOOKDOWN commands.
Sign In or Register to comment.