Shop OBEX P1 Docs P2 Docs Learn Events
WAM Ch. 4 Exercise 1/2 — Parallax Forums

WAM Ch. 4 Exercise 1/2

kf4matkf4mat Posts: 21
edited 2005-10-24 23:59 in Learn with BlocklyProp
I'm having problems with this one and now I have to ask for help. For background I'll post the problem from the book in whole....
Ch 4 Controlling Motion pg 135 said...
·1. Write a code block that sweeps the value of Pulsout controlling a servo from a duration of 700 to 800, in increments of (a) 1, (b) 4.

2. Add a nested FOR...NEXT loop to your answer to exerxise 1b so that it delivers ten pulses before incrementing the pulsout duration argument by 4.
Here is the code block for 1b:
·······
······ FOR counter = 700 TO 800 STEP 4
···········PULSOUT 14,·counter
·· ······· PAUSE 20
·········· DEBUG DEC4 counter, CR, CRSRUP········'This line is my addition
······ NEXT
······ FOR counter = 700 TO 800 STEP 4
·········· FOR counter = 800 TO 7000
···········PULSOUT 14,·counter
·········· PAUSE 20
·········· DEBUG DEC4 counter, CR, CRSRUP········ 'This line is my addition
······ NEXT
··········
Now I have no problem with this and if I add a counter declaration it runs with no problems. However, moving on to question·two, I seem to be missing the solution. The answer provided in the manual will not work as written.·Here is the book code block for question #2:

········· FOR counter = 700 TO 800 STEP 4
··············· FOR·pulses = 1 TO 10
················· PULSOUT 14,·counter
················· PAUSE 20
················· DEBUG DEC4 counter, CR, CRSRUP········ ' mine
··············· NEXT
··········NEXT
········· FOR counter = 800 TO 700 STEP 4
················FOR pulses = 1 TO 10
················· PULSOUT 14,·pulses
········ ········ PAUSE 20
················· DEBUG DEC4 counter, CR, CRSRUP········· ' mine
················NEXT
·········· NEXT

No matter how I try to rewrite this it will not work properly. attached is my lastest try. Anyone want to give me a hint so that I can figure this one out.

Thanks, Tom

Comments

  • kf4matkf4mat Posts: 21
    edited 2005-10-23 22:35
    Well, after much nashing of teeth I seem to have devised a way to make the exercise work. see attached if interested.

    Tom
  • edited 2005-10-24 00:58
    Hi Tom,

    Working straight from the questions as they are posted, this is what I think the answers should be:

    [color=green]' Solutions - I think.bs2[/color]
     
    [color=green]' {$STAMP BS2}[/color]
    [color=green]' {$PBASIC 2.5}[/color]
     
    [color=#000000]counter VAR Word[/color]
    [color=#000000]pulses  VAR Nib[/color]
     
    [color=green]' Chapter 4, Exercise 1-a[/color]
     
    [color=#0000ff]FOR[/color][color=#000000] counter = 700 [/color][color=#0000ff]TO[/color][color=#000000] 800[/color]
      [color=#0000ff]PULSOUT[/color][color=#000000] 14, counter[/color]
      [color=#0000ff]PAUSE[/color][color=#000000] 20[/color]
    [color=blue]NEXT[/color]
     
    [color=green]' Chapter 4, Exercise 1-b[/color]
     
    [color=#0000ff]FOR[/color][color=#000000] counter = 700 [/color][color=#0000ff]TO[/color][color=#000000] 800 [/color][color=#0000ff]STEP[/color][color=#000000] 4[/color]
      [color=#0000ff]PULSOUT[/color][color=#000000] 14, counter[/color]
      [color=#0000ff]PAUSE[/color][color=#000000] 20[/color]
    [color=blue]NEXT[/color]
     
    [color=green]' Chapter 4, Exercise 2[/color]
     
    [color=#0000ff]FOR[/color][color=#000000] counter = 700 [/color][color=#0000ff]TO[/color][color=#000000] 800 [/color][color=#0000ff]STEP[/color][color=#000000] 4[/color]
      [color=#0000ff]FOR[/color][color=#000000] pulses = 1 [/color][color=#0000ff]TO[/color][color=#000000] 10[/color]
        [color=#0000ff]PULSOUT[/color][color=#000000] 14, counter[/color]
        [color=#0000ff]PAUSE[/color][color=#000000] 20[/color]
    [color=blue]  NEXT[/color]
    [color=blue]NEXT[/color]
    
    



    They are not tested.· Please try them out and let me know if they work.

    Regards, Andy

    Post Edited (Andy Lindsay (Parallax)) : 10/24/2005 1:03:40 AM GMT
  • kf4matkf4mat Posts: 21
    edited 2005-10-24 20:33
    Andy Lindsay said...
    ' Chapter 4, Exercise 2
    ·
    FOR counter = 700 TO 800 STEP 4
    · FOR pulses = 1 TO 10
    ··· PULSOUT 14, counter
    ··· PAUSE 20
    · NEXT
    NEXT
    Hi Andy, the above code block does not work with regards to question 2. In order to see if it worked I added another line after the pause 20 to watch the counter:

    ······· DEBUG DEC3 counter, CR, CRSRUP

    When you run it the debug terminal counts in steps of 4 starting from 700. So then I changed it to read the value of pulses, when you run it then the debug terminal counts from 1 to 10 over and over until the value of counter gets to 800.

    The only way I could get it to work the way the question is worded was to add a nested IF...THEN statement· See the attachment on my second post. That way the value of counter as displayed on the debug terminal counts from 700 to 710 by 1's and then counts the rest of the way by 4's. It works but I don't know if nesting a IF...THEN inside a FOR...NEXT is considered good pratice.

    Tom
  • edited 2005-10-24 22:13
    Tom,

    I think you will see it better if you use

    · DEBUG ? counter

    instead.··Here, try this one:

    ' Chapter 4, Exercise 2
    ·
    FOR counter = 700 TO 800 STEP 4
    · FOR pulses = 1 TO 10
    ··· PULSOUT 14, counter
    ··· ' PAUSE 20
    ··· DEBUG ? counter
    · NEXT
    NEXT

    The result in the Debug Terminal should display counter = 700 ten times, followed by counter = 704 ten times, and so on up to counter = 800 ten times.· Keep in mind that the counter variable is the PULSOUT command's duration argument in the solution.

    These nested loops are important.· The net effect is that it causes the servo to hold the 700 position for about 1/10 second, followed by the 710 <edit>, no, it's actually 704 </edit> position for about 1/10 of a second, and so on.·

    The results will probably be more visible on the servo with this kind of subroutine (without the DEBUG command):

    ' Another example - 9 positions, 3/4 seconds each
    ·
    FOR counter =·350 TO·1150 STEP 100
    · FOR pulses = 1 TO 32
    ··· PULSOUT 14, counter
    ··· PAUSE 20
    · NEXT
    NEXT

    Lots of machines are programmed with that kind of loop.· The other day on the news, I saw one of those biomedical machines that puts a drop of solution on hundreds of different test slides.· While the motors and the microcontroller's programming language are likely different, it·almost certainly still uses nested loops to advance the plate of slides to one position, stay there for a while the eye dropper does its thing, then advance to the next position, etc.

    Regards, Andy

    Post Edited (Andy Lindsay (Parallax)) : 10/24/2005 10:16:30 PM GMT
  • kf4matkf4mat Posts: 21
    edited 2005-10-24 23:59
    blush.gif·Well now, things are kinda making sense. This whole time I've been misinterperting the question.
    Andy Lindsay said...
    These nested loops are important.· The net effect is that it causes the servo to hold the 700 position for about 1/10 second, followed by the 710 <edit>, no, it's actually 704 </edit> position for about 1/10 of a second, and so on.·
    I've been reading it as you want the servo to advance by steps of·1, from 700 to 710 and then starting at 711 start advancing by steps of 4. Not sure I would have understood that without your help.

    Thanks alot,

    Tom
Sign In or Register to comment.