IF and ELSEIF

HI Guys.

I’m a photographer and have been using servos to remotely control cameras for years, last year I was using a BS2, but just recently changed to a BasicATOM Pro 28 for a new project.

I’m using 8.0.1.7 for programming

The IF ELSEIF dosen’t seem to work the way it should ?? in the following battery monitoring code all works like it should except the ELSEIF. the compiler error is “Unexpected token after ELSEIF command”

I have commented the multiple iterations of IF ENDIF out (They work fine and left in the ELSEIF 's in the following code

may seem trivial but I’m using a 10 segment LED array to show battery condition (Only 3 segments shown here) and How do you get the ELSEIF to work ?

SCARAMOUCHEII

So the following code


[code]
battery var byte
index0 var byte

adin 19, battery

’ start up LED sequence to let the user know the unit powered up

high 15
pause 50
high 14
pause 50
low 15
pause 50
high 13
pause 50
low 14
pause 50
high 12
pause 50
low 13


’ leave LED 12 on as the POWER indicator LED

start

'
'now lets set the 3 LED array to reflect the state of the battery
'

lookdown battery, < , [20, 80,  180, 200], index0

	if index0 = 0 then 
			low 15
			low 14
			low 13
		'endif
		'if index0 = 1 then	
		elseif index0 = 1 then
			high 15
			low 14
			low 13
		'endif
		'if index0 = 2 then	
		elseif index0 = 2 then 
			high 15
			high 14
			low 13
		'endif
		'if index0 = 3 then	
		elseif index0 = 2 then 
			high 15
			high 14
			high 13
		
	endif

goto start[/code]

as you know “if” should be returned with “endif” after each exception,

the problem you are having is that you have missed these out.
so it should look something like this.
im not a programmer but its something iv pick up on so please dont hole me responsible for servo or board activity.

[code]start


'now lets set the 3 LED array to reflect the state of the battery

lookdown battery, < , [20, 80, 180, 200], index0

  if index0 = 0 then
        low 15
        low 14
        low 13
        
     elseif index0 = 1 then
        high 15
        low 14
        low 13
   
     elseif index0 = 2 then
        high 15
        high 14
        low 13
  
     elseif index0 = 2 then
        high 15
        high 14
        high 13
     
  endif

endif
endif
endif
goto start[/code]

Hmmm …

Well I seemed to remember that ELSE or ELSEIF are generally considered part of the original IF ???

Anyway, I tried what you suggested and it didn’t work …

As I stated in my original post, the compiler fails immediately after the first ELSEIF with the error: “Unexpected token after ELSEIF command”

Whats the point of an ELSEIF command if you have to add a whole bunch of ENDIF’s ?

Scaramoucheii

try this:

[code]start


'now lets set the 3 LED array to reflect the state of the battery

lookdown battery, < , [20, 80, 180, 200], index0

  if index0 = 0 then 
        low 15 
        low 14 
        low 13 
      
     else
      if index0 = 1 then 
        high 15 
        low 14 
        low 13 
        
     else
      if index0 = 2 then 
        high 15 
        high 14 
        low 13 
  
     else
      if index0 = 2 then 
        high 15 
        high 14 
        high 13 
      
  endif 
  endif
  endif
  endif

goto start[/code]

Actually the real problem is that the else if does not have a then…

Try:

[code]start


'now lets set the 3 LED array to reflect the state of the battery

lookdown battery, < , [20, 80, 180, 200], index0

  if index0 = 0 then 
        low 15 
        low 14 
        low 13 
     'endif 
     'if index0 = 1 then    
     elseif index0 = 1
        high 15 
        low 14 
        low 13 
     'endif 
     'if index0 = 2 then    
     elseif index0 = 2 
        high 15 
        high 14 
        low 13 
     'endif 
     'if index0 = 3 then    
     elseif index0 = 2 
        high 15 
        high 14 
        high 13 
      
  endif 

goto start[/code]

Kurt

Hi,

Remove the “then” after ELSEIF, only use “then” after IF.

Thanks Zenta …

The THEN has been removed after the ELSEIF statement and all compiles …

I’m using a 3 cell 2500 mah LiPo for this new camera and the cell monitoring is important.

Thanks to all for the discussion.

Hi,

Good to hear it all works fine. But Kurt was one minute before my post. :laughing:

So it goes like this…

IF THEN ELSEIF ELSEIF ELSEIF ELSEIF ENDIF
For someone like me that’s good to know. These are the kind of bugs that seem to find their way in my programs as well. I still struggle with nested IFs beyond two loops :laughing: