Shop OBEX P1 Docs P2 Docs Learn Events
Newbie need help on What's a Microcontroller Chapter #3 — Parallax Forums

Newbie need help on What's a Microcontroller Chapter #3

azmax100azmax100 Posts: 173
edited 2006-11-06 05:29 in Learn with BlocklyProp
Hi all,

I am learning What's a Microcontroller Chapter #3: Digital Input-Pushbutton.

I am connecting the LED on·Pin 9 and the input (Pushbutton) on·Pin 14.

In this exercise Pin 9 will go high after the push button is press.

How to make the LED·High or doing other routine only after the Pushbutton is press 3 times?
Let say after· 3 times the button is press·Led on Pin 10 is high.

Please Help.

TQ.

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-11-02 15:52
    Hello,
    ·
    ·· Essentially you will need to update a counter variable with each press of the button and have a conditional check for when it reaches 3 then make the LED pin HIGH.· Since you’re only at Chapter #3, I would recommend completing the remaining chapters which may give you more insight on how best to accomplish this.· Take care.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • edited 2006-11-02 17:02
    Actually, at this point, I think you've covered enough in the book to solve that problem.· It will help if you break it down into steps:

    1) Declare a variable for counting.

    2) Write a command inside·a DO...LOOP that adds 1 to the counting variable each time through.

    3) Add a gate keeper that monitors the pushbutton, and only allows the program to move on when the button is pressed.· (Propbably a nested DO...LOOP, check the last activity in Chapter 3.)

    4) Add a statement similar to this:

    IF IN14 = 1 AND counter =·3 THEN

    · '· Do something

    ENDIF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.

    Post Edited (Andy Lindsay (Parallax)) : 11/2/2006 5:09:47 PM GMT
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-11-03 02:33
    Don't forget to reset the counter to zero at some point.
  • azmax100azmax100 Posts: 173
    edited 2006-11-03 04:28
    Hi all,



    Thanks to all who have replied.

    Finally I got the answer.

    Here I attached the code.





    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    counter VAR Byte

    Main:
    counter = 0
    DO
    · DO
    · DEBUG ? counter
    · IF IN14 =1 THEN
    · counter = counter + 1
    · HIGH 9
    · PAUSE 500
    · LOW 9
    · PAUSE 500
    · ENDIF
    LOOP UNTIL counter = 3
    · IF counter = 3 THEN
    · HIGH 2
    · PAUSE 1500
    · LOW 2
    · PAUSE 500
    · ENDIF
    · GOTO Main
    LOOP UNTIL (IN14 =1)


    Is there any other method?

    Thanks again to all of you.
    Respect you all.
  • BenoitBenoit Posts: 47
    edited 2006-11-03 13:37
    You've got some extra statements that won't be doing anything. You're program will be
    doing the same this as this:


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    counter VAR Byte

    Main:
    counter = 0
    DO
    DEBUG ? counter
    IF IN14 =1 THEN
    counter = counter + 1
    HIGH 9
    PAUSE 500
    LOW 9
    PAUSE 500
    ENDIF
    LOOP UNTIL counter = 3

    'At this point, counter will be 3
    'So the IF counter=3 will ALWAYS be true
    HIGH 2
    PAUSE 1500
    LOW 2
    PAUSE 500

    GOTO Main

    'because the previous GOTO will Always be executed, the next loop will never be reached
    'LOOP UNTIL (IN14 =1 )
  • AImanAIman Posts: 531
    edited 2006-11-03 19:18
    One thing I learned from game programing is to remove fragments of decimals that randomly occur.

    In other words instead of·
    Loop until counter = 3 say Loop until counter >= 3

    The reason for this is in case the computer does something like 3.01 your covered.
    Doesn't happen often but it does happen.

    So for what you want I would write it this way.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    counter VAR Byte

    Main:
    counter = 0
    DO
    · DO
    ···· DEBUG ? counter
    ···· IF IN14 =1 THEN
    ········ HIGH 9
    ········ PAUSE 500
    ········ LOW 9
    ········ PAUSE 500

    ····· counter = counter + 1
    ····· ENDIF
    ·· LOOP UNTIL counter >= 3

    This is where you need to figure out if something is going to happen only at 3 or not. If its going to happen only at 3 then write this.

    ·· HIGH 2
    ·· PAUSE 1500
    ·· LOW 2
    ·· PAUSE 500
    ·· if counter >= 3 then counter = 0
    LOOP UNTIL (IN14 =1)

    If it can happen anytime then you need to figure out when its going to happen. For example:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    counter VAR Byte

    Main:
    counter = 0
    DO
    · DO
    ···· DEBUG ? counter
    ···· IF IN14 =1 THEN
    ········ HIGH 9
    ········ PAUSE 500
    ········ LOW 9
    ········ PAUSE 500
    ····· ENDIF
    ·· HIGH 2
    ·· PAUSE 1500
    ·· LOW 2
    ·· PAUSE 500···

    ·· counter = counter + 1
    ···LOOP UNTIL counter >= 3
    ·· if counter >= 3 then counter = 0
    LOOP UNTIL (IN14 =1)
  • azmax100azmax100 Posts: 173
    edited 2006-11-04 01:33
    Thanks to Aiman and couch-pilot.

    You made me see the clear picture.

    Since I am a newbie I need people like you to guide me.

    You people are really helpfull.

    Thanks.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2006-11-06 05:29
    If you want the LED to light up only after the third button press, you would set it high on counter = 3, turn it off, then reset the counter to 0 in order to count to three again.

    Another way you could do it is to keep counting up with the the counter, and use the MODULUS operator to check to see if the value of counter is divisible by three. Something like this:

    IF (Counter // 3) = 0 THEN LightOn

    The reason you check for 0 is that modulus returns the remainder of the division operation, so 3//3 = 3/3, 1 remainder 0, 6//3 = 6/3, 2 remainder 0, 9//3 = 9/3, 3 remainder 0, etc.

    This means that your IF..THEN block should run on every third button press.

    Edit:

    Okay, since I recommended it, I thought I would whip something up as an example. Attached is a program using SERIN to detect keyboard presses, instead of a button. You should be able to modify it for a button based on Ch3 of the WaM book. Because it uses SERIN for the input, you may need to manually connect to the debug terminal.

    Post Edited (Kevin Wood) : 11/6/2006 7:10:12 AM GMT
Sign In or Register to comment.