Shop OBEX P1 Docs P2 Docs Learn Events
WAM Chapter 4, project #2 — Parallax Forums

WAM Chapter 4, project #2

ArchiverArchiver Posts: 46,084
edited 2004-02-27 20:33 in General Discussion
I am currently taking a class on microcontrollers and we are using
the What's a Microcontroller text and BOE. I am stuck on project 2
of chapter 4. This project calls for us to design a program that
will run a pushbutton windshield wiper motor with six speeds. We
should be able to speed it up with one button and slow it down with
the other. I've gotten as far as getting the stepper motor to move
back and forth at the given speeds, but I just can't seem to get my
pushbuttons to do anything other than start the first speed. I am
currently trying to use a counter for each pushbutton and adding a
goto command for it to go to the speed that corresponds to the number
in the counters, but this has brought me nothing but a headache from
going over the whole thing over and over. Does anyone have any
advice or info on how I can get myself out of this chapter for good?
Any help would be highly appreciative. Thanks.

Wayne

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-02-26 22:34
    Wayne,

    Are you perhaps using the wrong variable to dispatch in your GOTO?
    Or, are you reinitializing that varible inside yur main loop?

    Posting the code, particularly the loop in question, would help.

    Russ

    --- In basicstamps@yahoogroups.com, "keithfamily01" <keith01@m...> wrote:
    > I am currently taking a class on microcontrollers and we are using
    > the What's a Microcontroller text and BOE. I am stuck on project 2
    > of chapter 4. This project calls for us to design a program that
    > will run a pushbutton windshield wiper motor with six speeds. We
    > should be able to speed it up with one button and slow it down with
    > the other. I've gotten as far as getting the stepper motor to move
    > back and forth at the given speeds, but I just can't seem to get my
    > pushbuttons to do anything other than start the first speed. I am
    > currently trying to use a counter for each pushbutton and adding a
    > goto command for it to go to the speed that corresponds to the number
    > in the counters, but this has brought me nothing but a headache from
    > going over the whole thing over and over. Does anyone have any
    > advice or info on how I can get myself out of this chapter for good?
    > Any help would be highly appreciative. Thanks.
    >
    > Wayne
  • ArchiverArchiver Posts: 46,084
    edited 2004-02-27 16:26
    This is what I have so far. Though all it does is activate the motor
    when I hit PB1. It will not change speeds in either direction.

    Thanks, Wayne.

    counter VAR Word
    pb1counter VAR Byte
    pb2counter VAR Byte

    Start:
    DO
    pb1counter = 0
    pb2counter = 5
    LOOP UNTIL IN3 = 1

    DO
    IF IN3 = 1 AND IN4 = 0 THEN
    pb1counter = pb1counter + 1
    pb2counter = pb2counter -1
    PAUSE 100
    ENDIF
    IF IN3 = 0 AND IN4 = 1 THEN
    pb2counter = pb2counter + 1
    pb1counter = pb1counter - 1
    PAUSE 100
    ENDIF

    Speedcontrol:
    IF pb1counter = 1 AND pb2counter = 4 THEN
    DEBUG "Speed 1", CR, CR
    'GOTO Start:
    ENDIF
    LOOP WHILE pb1counter = 1 AND pb2counter = 4
    IF pb1counter = 2 AND pb2counter = 3 THEN
    DEBUG "Speed 2", CR, CR
    'GOTO Speed1:
    ENDIF

    IF pb1counter = 3 AND pb2counter = 2 THEN
    DEBUG "Speed 3", CR, CR
    'GOTO Speed2:
    ENDIF



    'Speed1:
    'DO
    'IF pb1counter = 1 AND pb2counter = 4 THEN
    ' FOR counter = 500 TO 1000 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT
    ' PAUSE 500
    ' FOR counter = 1000 TO 500 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT
    'ELSE
    ' GOTO Speedcontrol:
    'ENDIF
    'LOOP

    'Speed2:
    'DO
    'IF pb1counter = 2 AND pb2counter = 3 then
    ' FOR counter = 500 TO 1000 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT
    ' PAUSE 3000
    ' FOR counter = 1000 TO 500 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT
    'else
    ' GOTO Speedcontrol:
    'ENdif
    'Loop
  • ArchiverArchiver Posts: 46,084
    edited 2004-02-27 20:33
    Here is your code, with comments:

    counter VAR Word
    pb1counter VAR Byte
    pb2counter VAR Byte

    Start:
    DO
    pb1counter = 0
    pb2counter = 5
    LOOP UNTIL IN3 = 1

    DO
    IF IN3 = 1 AND IN4 = 0 THEN
    pb1counter = pb1counter + 1
    pb2counter = pb2counter -1
    PAUSE 100
    ENDIF

    IF IN3 = 0 AND IN4 = 1 THEN
    pb2counter = pb2counter + 1
    pb1counter = pb1counter - 1
    PAUSE 100
    ENDIF

    Speedcontrol:
    IF pb1counter = 1 AND pb2counter = 4 THEN
    DEBUG "Speed 1", CR, CR
    'GOTO Start:
    ENDIF

    LOOP WHILE pb1counter = 1 AND pb2counter = 4

    [noparse][[/noparse]Bug: Once you push button 1, the WHILE conditon is always
    TRUE, which means you won't get past this poing.]

    IF pb1counter = 2 AND pb2counter = 3 THEN
    DEBUG "Speed 2", CR, CR
    'GOTO Speed1:
    ENDIF

    IF pb1counter = 3 AND pb2counter = 2 THEN
    DEBUG "Speed 3", CR, CR
    'GOTO Speed2:
    ENDIF



    'Speed1:
    'DO
    'IF pb1counter = 1 AND pb2counter = 4 THEN
    ' FOR counter = 500 TO 1000 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT

    ' PAUSE 500

    ' FOR counter = 1000 TO 500 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT

    'ELSE
    ' GOTO Speedcontrol:
    'ENDIF

    'LOOP

    [noparse][[/noparse]Bug: After button 1 is pushed, pb2counter will become 1
    and pb2counter will become 4. This means that the
    first IF test after the DO will always be satisfied,
    whicl will trap the program in the DO ... LOOP
    forever.]


    'Speed2:
    'DO
    'IF pb1counter = 2 AND pb2counter = 3 then
    ' FOR counter = 500 TO 1000 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT

    ' PAUSE 3000

    ' FOR counter = 1000 TO 500 STEP 10
    ' PULSOUT 14, counter
    ' PAUSE 7
    ' NEXT

    'else
    ' GOTO Speedcontrol:

    'ENdif

    'Loop


    [noparse][[/noparse]Same bug as the last one, except with different values
    being tested. But the program will never get here!]

    I think you only need one counter. PB1 increments the counter, PB2
    decrements it. Once it reaches 5, don't increment. Once it reaches
    0, don't decrement it.

    At first I thought testing bothe PB1 and PB2 was not necessary, but
    then I realized that both could be pressed at the same time!

    Learn to indent your code! It makes understanding it far easier!
Sign In or Register to comment.