Error : If without Endif

lamxe

Senior Member
Dear all
I would like to used pin 1 and 3 for control 3 difference pulsout but no work. Please help me to rewrite my wrong code : Many thank you.

Code:
# picaxe 08m
do
If pin1=0 and pin3=0 then 
gosub sau

If pin1=1 and pin3=0 then
 gosub bon

If pin1=1 and pin3=1 then
 gosub hai

endif
 loop
 ;///////////////////////////////////
sau:
pulsout 2,180
pause 15
return

'=============================
bon:	

	pulsout 2,180
	pause 23

return

	

'=============================
hai:	

	pulsout 2,500

pause 48
return
                  '///////////////////////////////////
 

oracacle

Senior Member
every if statement need to be terminated. you have 3 if statements and only 1 end if. this should do what you need

Code:
# picaxe 08m
do
If pin1=0 and pin3=0 then 
gosub sau
end if

If pin1=1 and pin3=0 then
 gosub bon
end if

If pin1=1 and pin3=1 then
 gosub hai
endif
 loop
 ;///////////////////////////////////
sau:
pulsout 2,180
pause 15
return

'=============================
bon:	

	pulsout 2,180
	pause 23

return

	

'=============================
hai:	

	pulsout 2,500

pause 48
return
                  '///////////////////////////////////
 

Goeytex

Senior Member
Alternatively, there is no need to use "endif" when the conditional statement is on one line and calls a subroutine.

This should also work.

Code:
#Picaxe 08m

'===========  Main Loop =============
Do
   If pin1 = 0 and pin3 = 0 then  gosub sau
   If pin1 = 1 and pin3 = 0 then  gosub bon
   If pin1 = 1 and pin3 = 1 then  gosub hai
Loop
'===========  End Main Loop ==========


'=================================
sau:
	pulsout 2,180
	pause 15
return
'=================================

'=================================
bon:	
	pulsout 2,180
	pause 23
return
'================================

'================================
hai:	
	pulsout 2,500
	pause 48
return
'===============================
 

westaust55

Moderator
Or without if then tests, try something like this assuming byte variable b0 is free or can be changed for another variable
(and a few bytes less program space at 52 bytes)
Code:
Main:
DO
	Bit0 = pin1
	Bit1 = pin3
	b0 = b0 AND $03

	ON b0 GOSUB sau,bon,skip,hai ; labels to branch to when b0 is 0,1,2 or3
;
LOOP

Skip:
Return

 ;///////////////////////////////////
sau:
	pulsout 2,180
	pause 15
	return

'=============================
bon:	

	pulsout 2,180
	pause 23

	return

	

'=============================
hai:	

	pulsout 2,500

	pause 48
	return
'///////////////////////////////////
or slightly smaller at 50 bytes and more easily using any variable:

Code:
#Picaxe 08m

    Main:
    DO
    	b10 = pin3 *2 + pin1


    	ON b10 GOSUB sau,bon,skip,hai ; labels to branch to when b0 is 0,1,2 or3
    ;
    LOOP

    Skip:
    Return

     ;///////////////////////////////////
    sau:
    	pulsout 2,180
    	pause 15
    	return

    '=============================
    bon:	

    	pulsout 2,180
    	pause 23

    	return

    	

    '=============================
    hai:	

    	pulsout 2,500

    	pause 48
    	return
    '///////////////////////////////////
 
Last edited:

lamxe

Senior Member
Dear Sir Westaust55
Thank you so much for you give me a high performance
and professional code.
 
Top