question about "For Next" loops

rob53

New Member
Hi folks, I'm still a rookie at this programming business. I have no formal training and I'm trying to teach myself, but I need a little help from you experts from time to time.

I'm writing a program to simulate random behavior for 10 blinking LEDs. I'm using for-next loops to have an LED blink a certain number of times, then pause, then blink at a different rate for a number of times, then pause....etc etc.

It says in the picaxe manual "For...next loops can be nested 8 deep", does that mean I can only use the for..next command 8 times in my entire program, or only 8 times under one label? What do they mean by "nested 8 deep"?

I'm probably gonna smack my forehead when I read the answer, so....sorry for the dumb question :eek:
 

Rick100

Senior Member
What do they mean by "nested 8 deep"?
I'm probably gonna smack my forehead when I read the answer, so....sorry for the dumb question :eek:
Hello rob53,
It's not a dumb question. Nested "for next" loops look like this. They are "for "next" loops inside of other "for next" loops. This example is 4 deep:

Code:
for b0 = 0 to 10
	for b1 = 0 to 10
		for b2 = 0 to 10
			for b3 = 0 to 10
				'do something	
			next b3
		next b2
	next b1
next b0
It's unlikely you will ever exceed the limit.

Good luck,
Rick
 

rob53

New Member
Hi Rick, thank you for explaining that for me, below is a abbreviated example of the code I'm writing. I need to do those for...next loops a bunch of times within a single "start label" to get the random blinking effects I'm looking for. This would not be considered "nesting", and I can do it as many times as I want?

Code:
start0:
       for b0 = 1 to 10
       high B.1
       pause 200
       low B.1
       next b0
       pause 1000
           for b1 = 1 to 6
           high B.1
           pause 500
           low B.1
           next b1
           pause 1000
             for b2 = 1 to 4
              high B.1
              pause 200
              low B.1
              next b2
              pause 500
                 for b3 = 1 to 4
                 high B.1
                 pause 200
                 low B.1
                 next b3
               goto start0                    
 start1:
        for b4 = 1 to 10
         high B.2
         pause 200
         low B.2
         next b4
         pause 500
           for b5 = 1 to 4
           high B.2
           pause 300
           low B.2
           next b5 
           pause 1000
       goto start1
 

Rick100

Senior Member
This would not be considered "nesting", and I can do it as many times as I want?
That's not nesting. I believe the only restrictions are running out of memory or variables. You haven't said which chip your using.

Good luck,
Rick
 

rob53

New Member
I was thinking about using the 14M2. I have all of the M2 chips, the 08, 14, 18 and 20. I'm thinking I only need to randomize 5 or 6 of the LEDs, the rest of them (10 LEDs total) can just blink at one constant rate. There are 28 byte variables available on the M2 chips, correct? If so, that should be enough. I think I'll be ok with memory as well.

I also noticed in the picaxe manual, under "Reserved Labels", the "start" label is only listed from 0 to 7, so does that mean I can only use it 8 times? This doesn't really matter with this random blink program that I'm going to write, I just wanted to know for future reference.
 

geoff07

Senior Member
I'm still a rookie at this programming business. I have no formal training and I'm trying to teach myself, but I need a little help from you experts from time to time.
You might consider replacing your
Code:
start1:
goto start1
and the like with a do /loop construct. That way you won't be tempted to use random gotos to get back to start. Gotos are one of the biggest causes of error and confusion in programming, as they are not a closed construct i.e. anything can be directed to a label from anywhere. Much better to use the constructs provided (do/loop; select/case/endselect, for/next etc.) to do all of the program flow management. That way, if the program starts to get large and you want to understand it in the future, you have more of a chance. It may mean you have to think the design through more thoroughly before you start coding, but that would be a good thing.
 

erco

Senior Member
@rob53: Per Rick100, your program's for/next loops are not nested since no loops are inside other loops. You could have reused variable b0 in each of your for/next loops instead of b0-b5.
 

rob53

New Member
Sorry it took so long to get back, had lots of errands to run today. I want to thank all of you for responding and for the great tips. I didn't realize I could reuse that b0 variable, but now that I think about it, it makes perfect sense. I'll read up on the do-loops, select case etc. I've been trying to learn by the examples given in the PDFs, I should probably be looking online in the forums for other good examples of how more experienced programmers do things. Anyway, thanks again folks. I know I can always rely on the good people in this forum for information and advice.
 

Morganl

Senior Member
It is a good idea to make a plan about how you are going to use the variables, and naming of symbols, constants, etc so you can yourself directly see what it is, even next year. I have not been long with picaxe yet, but here is what i try currently:
Code:
#rem ___Author´s rules
;___Temporary variables
Bits: bit0-bit7
Bytes: b0, b2-b9  (b1 reserved for specific bit flags)
Words: w5-w9
;___Naming
bit symbols: prefixed with b_
byte symbols: no prefix
word symbols: w_
pins: p_
constants: prefixed by "c_"
Macros and #defined code: CAPS
Task entry: T_
Subroutine entry prefix: Sub
Include files: try yo have entry point start with same letters as file. Try to have same letters in beginning of vars and const (after the prefix)
#endrem
 
Top