My first biped 2.0

for loops

They allow a programmer to complete a block of instructions a finite number of times. If you wanted to print Hello World 5 times, you could:

print "Hello World"
print "Hello World"
print "Hello World"
print "Hello World"
print "Hello World"

OR you could:

for i = 1 to 5
   print "Hello World"
next i

i is being used as a counter variable in this case. i will start at 1 and print Hello World. next i will increment i by 1, in this case, and then the loop will print Hello World again. The for i = 1 to 5 part of the loop does the actual comparison and will check to see that i is less than or equal to 5. When next i is called and i gets incremented to 6 the loop will be finished and the program will continue with the instruction that follows the next i statement.

In Ro-Bot-X’s example:

Tilt_right_15:

for i = 145 to 130 step -5
       servo 7, i
       pause 20
next i

return

The for loop counts backwards from 145 to 130 in 5 step decrements. Everything between the for and next commands will be executed as many times as is allowed by the loop, in this case 4 times. Because, i will start at 145 and the loop will execute. next i will be called and i will then be decremented by 5 (step -5) and will be 140 everything inside the loop will occur. i - 5 = 135 and the loop will run again. next i, i - 5 = 130 and the loop happens once again. next i, i - 5 = 125 which is less than 130, the loop exits and the next command is return.

I hope this helps.

Whoa

Wow.  That is really clear.  I will be trying that when i get home from work today.  That will make my life so much easier.  Thanks guys.  You should write a programming for Noobs walkthrough.

So very very close

Ok i used what you put here and i have it almost the way i have envisioned it.  The only problem i am having is the shifting of the weight from on foot to the other.  I cant figure out how to loop so both feet move at the same time for a smoother planting and thus less sliding.  Here is what i am using.

 

Tilt_right1:
for b0= 150 to 170 step 2
servo lfoot, b0
pause 30
next b0
for b1= 130 to 155 step 2
servo rfoot, b1
next b1
return

Tilt_left2:
for b0= 170 to 150 step -2
servo lfoot, b0
pause 30
next b0
for b1= 155 to 125 step -2
servo rfoot, b1
next b1

 

(Note:  I treied to use “for i=”  but it kept saying i is an invalid syntax when I would run the syntax check in picaxe.  So i used b0 and b1 instead and hoped it wouldnt make a diffrence.) 

Observations/questions

In my “overboard” response you may notice that I used leftFootCenter and rightFootCenter in the loops.

Tilt_right:
   for i = tilt to 0 step -5
   servo 7, leftFootCenter - i
   servo 6, rightFootCenter - i
   pause 20
   next i
return

If you set the variable tilt before you call Tilt_right, you will be able to make a common routine. Your Tilt_Right routine uses very hard coded numbers, aka magic numbers (bad word :stuck_out_tongue: ).

Tilt_right1:
   for b0= 150 to 170 step 2
   servo lfoot, b0
   pause 30
   next b0
   for b1= 130 to 155 step 2
   servo rfoot, b1
   next b1
return

If you were to use my routine and just call it Tilt, you could pass it positive or negative numbers depending on whether you tilt left or right. You would also need to declare a variable that was LEFT = 1 and one that was RIGHT = -1. Now you set tilt to a whole number * LEFT or RIGHT before you call the Tilt subroutine.

SYMBOL LEFT = 1
SYMBOL RIGHT = -1
SYMBOL direction = b10 'Holds the value of the direction of tilt/swing 1 or -1
SYMBOL stepResolution = b9 'Controls how far the servo will move for each run through the loop

main:
   stepResolution = 2
   direction = LEFT
   tilt = 20 * direction * -1 ’ We need the inverse to get the tilt in the correct direction
   gosub Tilt
end 

Tilt:
   for b6 = tilt to 0 step stepResolution * direction 
   servo 7, leftFootCenter - i 'This line and the next may need the + and - swapped
   servo 6, rightFootCenter + i
   pause 20
   next b6
return

My explanation does not, however, make the adjustment that you show in your code for the right foot. In your tilt right routine you have 130 to 155 and in your tilt left you have 155 to 125.

Another comment, in your Tilt code you use b0 for the left foot and b1 for the right foot. Your loops are not nested in your code.
Example:

'These loops are not nested. They do not reside one inside the other.
for b0 = 1 to 5
   print "hi"
next b0
’This is a waste of a variable.
for b1 = 5 to 1
   print "bye"
next b1

OUTPUT:
hi
hi
hi
hi
hi
bye
bye
bye
bye
bye 

'These loops are nested. They will require the use of two individual variables.
for b0 = 1 to 5
   for b1 = 5 to 1
      print “buy low”
   next b1
   print "sell high"
next b0

OUTPUT:
buy low
buy low 
buy low 
buy low 
buy low 
sell high 
buy low
buy low 
buy low 
buy low 
buy low 
sell high
… 3 more times

Clear as mud? :slight_smile:

I would also suggest that you try to avoid using the first byte variable, b0, as it is the only one available for use as bit variables. Hence the reason I changed my Tilt routine to use b6 in the for loop.

Hmm, now it’s time for me to

Hmm, now it’s time for me to take some notes… You gave me the idea how to shrink my code and eliminate all those similar functions I have, by using a few more variables. I’ll look over my code and see how to implement this method. Changing the resolution will change the speed of the movement, which is good and it doesn’t require the heavy floating point math that the interpolating method does. Thanks!

Re: notes

I am just shooting from the hip and making it up as I go along.