If then gosub

lamxe

Senior Member
Dear all
write picaxe programming I don't use it often and don't understand much about writing programs . I tried using symbol < . > = written in many ways but it didn't work So please help me fix this code .
** If b0 = 0 to 50 then gosub label ** ; please help me to correct this code
**if b0 = 50 to 100 then gosub label ** ; please help me to correct this code

Many thank you and Happy new Year
 

lbenson

Senior Member
For example:
Code:
main:
  if b0 > 100 then ' do nothing
  elseif b0 > 50 then gosub label2 ' will be 51-100
  else gosub label1 ' will be 0-50
  endif
  goto main
label1: ' 0-50
  return
label2: ' 51-100
  return
As you stated it, you didn't make clear whether the value 50 should be in the label1 or the label2 category.

Try this in the simulator with different values of b0.
 

hippy

Ex-Staff (retired)
If b0 = 0 to 50 then gosub label ** ; please help me to correct this code
if b0 = 50 to 100 then gosub label ** ; please help me to correct this code
The simplest way to do "If b0 = X to Y" is to use and "AND"; so "If b0 >= X And b0 <= Y Then" -

If b0 >= 0 And b0 <= 50 Then ...
If b0 >= 50 And b0 <= 100 Then ...

But note that a value of 50 is valid for both cases.

An alternative approach is to use SELECT-CASE -
Code:
Select Case b0
  Case <=  50 : Gosub ... ;   0 to  50
  Case <= 100 : Gosub ... ;  51 to 100
  Case <= 150 : Gosub ... ; 101 to 150
  Else        : Gosub ... ; 151 and upwards
End Select
 

lamxe

Senior Member
For example:
Code:
main:
  if b0 > 100 then ' do nothing
  elseif b0 > 50 then gosub label2 ' will be 51-100
  else gosub label1 ' will be 0-50
  endif
  goto main
label1: ' 0-50
  return
label2: ' 51-100
  return
As you stated it, you didn't make clear whether the value 50 should be in the label1 or the label2 category.

Try this in the simulator with different values of b0.
The simplest way to do "If b0 = X to Y" is to use and "AND"; so "If b0 >= X And b0 <= Y Then" -

If b0 >= 0 And b0 <= 50 Then ...
If b0 >= 50 And b0 <= 100 Then ...

But note that a value of 50 is valid for both cases.

An alternative approach is to use SELECT-CASE -
Code:
Select Case b0
  Case <=  50 : Gosub ... ;   0 to  50
  Case <= 100 : Gosub ... ;  51 to 100
  Case <= 150 : Gosub ... ; 101 to 150
  Else        : Gosub ... ; 151 and upwards
End Select
Dear Sir Hippy and Sir Ibenson
Thank you for your attention and guidance, dedicated and through The details are very useful for learning
and HAPPY NEW YEAR
 
Last edited:

lamxe

Senior Member
For example:
Code:
main:
  if b0 > 100 then ' do nothing
  elseif b0 > 50 then gosub label2 ' will be 51-100
  else gosub label1 ' will be 0-50
  endif
  goto main
label1: ' 0-50
  return
label2: ' 51-100
  return
As you stated it, you didn't make clear whether the value 50 should be in the label1 or the label2 category.

Try this in the simulator with different values of b0.
Dear Sir Ibenson and Hippy
Thank you for your attention and guidance, dedicated and through The details are very useful for learning
and HAPPY NEW YEAR
 

lamxe

Senior Member
Dear all
After receiving your help, I think the difficulty will pass, but things are not as I thought. I have another problem in my new code'
I want my code to keep running (loop) until I want to stop, but my Program doesn't repeat, it runs only once time So please help me again by correcting my code to repeat cycle and if my code impossible repair please give me other your code so I can compare and learn more by myself. Many thank you and HAPPY NEW YEAR
Pseudo my code as I want :
This code to keep running (loop) until I want to stop
.............................................................
p1:
If b0=3 then high 1 ( repeat 10 time)
next goto p2
p2:
if b0=3 then high 2 ( repeat 10 time)
next goto p1
.......................................................
And continue : p1>p2>p1>p2>p1..>>>>>.............
.............................................................................
symbol pbswitch = pin3
symbol up = 1

Init:
B0=0 b3=0
SETINT %01000, %01000

Main:
IF b0=2 and b3>=1 and b3<=10 then gosub p1 ; for repeat 10 time
IF b0=2 and b3>=10 and b3<=20 then gosub p2 ; for repeat 10 time
if pin4=1 then gosub stopp

GOTO Main

p1:
HIGH 1
wait 1
b0=0

RETURN
p2:
HIGH 2
wait 1
b0=0
RETURN
stopp:
LOW 1 LOW 2

RETURn
interrupt:
inc B0 inc b3
stillup:
IF pbswitch = up THEN stillup
SETINT %01000, %01000
RETURN
 

lbenson

Senior Member
Have you run your program in the simulator? In PE5, it will look like this (PE6 is different, but similar).

You can single-step through your code and see exactly what it is doing at each step, as you click on and release the buttons for pin3 and pin4.
25095
I've formatted the code more conventionally, changing nothing but the white space. Here it is:
Code:
symbol pbswitch = pin3
symbol up = 1

Init:
  B0=0 b3=0
  SETINT %01000, %01000

Main:
  IF b0=2 and b3>=1 and b3<=10 then gosub p1 ; for repeat 10 time
  IF b0=2 and b3>=10 and b3<=20 then gosub p2 ; for repeat 10 time
  if pin4=1 then gosub stopp

  GOTO Main

p1:
  HIGH 1
  wait 1
  b0=0
  RETURN

p2:
  HIGH 2
  wait 1
  b0=0
  RETURN

stopp:
  LOW 1 LOW 2
  RETURN

interrupt:
  inc B0
  inc b3
stillup:
  IF pbswitch = up THEN stillup
  SETINT %01000, %01000
  RETURN
 

Attachments

westaust55

Moderator
As lbenson has suggested, try stepping through your program in the Program Editor in Simulate mode.

It is unclear exactly what you are trying to achieve.

You must press the pushbutton 20 times to complete the 10 cycles through P1
The first pass in P1: sets pin 1 (pin B.1) high but it is never changed back to a low (zero) state.

Likewise the first pass in P2: sets pin 2 (pin B.2) high but again it is never changed back to a low (zero) state.

The Interrupt: subroutine keeps incrementing variable b3.
Then once it is greater than 20 the program will never again call subroutines P1: or P2:
it just keeps going around the Main: loop


So, when you say "doesn't repeat, it runs only once time "
How do you know this?

Note also that when variable B3 = 10, that both P1: and P2: will be called for that one pass.
 

lamxe

Senior Member
Hi Westaust55 Ibenson and Hippy
Dear All
First of all, I would like to thank you for taking the time to answer my unclear question. I use version 5.5.6 picaxe simulation. This code works fine but only runs 1 cycle (i.e. I press the push button 20 times to complete 10 cycles past P1 and 20 times to complete p2 . Total 40 times for complete program. But when I keep pressing the button for the 41st times the program in sleep mode ̣ (exit?)
So can you help me how to program:
After the end of the 40 cycle If I keep pressing the button for the 41st program will do new cycle ̣ ̣(LOOP ?)
Main:
1st ..... the program runs until the 40 st press button
40th ..... complete 40 times press button
41st time press button
goto main, for make a new cycle and continue forever

Hope you understand my question and help me to get modified my code or your code after the holidays and I wish you and family a Merry Christmas And a Happy New Year
 
Last edited:

lbenson

Senior Member
First, we're all colleagues here in picaxe land, so unless you know that someone has been knighted, there's no need to use "sir".

Congratulations on using the simulator. As westaust55 notes, once b3 "is greater than 20 the program will never again call subroutines P1: or P2:".

Why--because b3 will always be greater than 20, and your "IF" conditions will fail. So before "GOTO main", put "if b3 > 20 then b3=0". Does the program then do what you want?
 

lamxe

Senior Member
First, we're all colleagues here in picaxe land, so unless you know that someone has been knighted, there's no need to use "sir".

Congratulations on using the simulator. As westaust55 notes, once b3 "is greater than 20 the program will never again call subroutines P1: or P2:".

Why--because b3 will always be greater than 20, and your "IF" conditions will fail. So before "GOTO main", put "if b3 > 20 then b3=0". Does the program then do what you want?
[/QU25100OTE]
Hi Ibenson
(.....So before "GOTO main", put "if b3 > 20 then b3=0". Does the program then do what you want?)
Thank you for your help again According to me understanding, do you want me to put your code here) or if I misunderstood, please show again many thanks.
 
Top