Shop OBEX P1 Docs P2 Docs Learn Events
If Key_pressed — Parallax Forums

If Key_pressed

PwnSaladPwnSalad Posts: 8
edited 2012-04-26 08:06 in BASIC Stamp
Hello, I need help with a command that I am not sure what the name is.

What I want it to do is when I press a key it will follow the statement/command that I have it do.

I am using pBasic 2.5 and Bs2.

Thanks

Comments

  • FranklinFranklin Posts: 4,747
    edited 2012-04-25 08:26
    Take a look at BUTTON
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-25 08:26
    Are you talking about the PC keyboard or some other keyboard that's somehow connected to your Stamp? If it's the PC keyboard, read the sections in the Stamp Manual on the DEBUGIN statement and the SERIN statement. If you're talking about an external keyboard, that depends on the keyboard and how you've connected it.
  • PwnSaladPwnSalad Posts: 8
    edited 2012-04-26 06:09
    It is a PC keyboard.

    Could I get a more specific answer?

    Also could I get some syntax?

    I took the code from the digital potentiometer in the Parallax book "Whats a micro controller?" on page 273-274.

    What I want it to do is increase when Q is pressed here is the example code that would be nice if i had the correct command and the syntax

    If Key_Pressed(This is the command I need) "Q"(The preferred key)

    THEN newtapsetting = oldtapsetting +1
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-26 06:46
    You'd use:
         DEBUGIN   temp
         IF temp = "Q" then newTapSetting = oldTapSetting + 1
    
    temp is declared as a byte or word variable. This will cause the Stamp to wait for a key to be pressed, then check the entered character against "Q". That's probably not what you want, but there is no way to test whether a character has already been typed since Stamp I/O isn't buffered. While the Stamp is executing other statements than DEBUGIN and SERIN, it's ignoring any serial characters sent to it.
  • PwnSaladPwnSalad Posts: 8
    edited 2012-04-26 07:04
    I really do appreciate the input.
    Lets try doing this with math or something simple before I try to use it in any complex way.

    Lets say I want any letter of the alphabet to add 1 to the current value every time I press that letter.

    It would look like something like this..


    X VAR BYTE
    X = 0

    DEBUGIN temp
    If temp = "Q" THEN X = X + 1

    ...Right?

    I'm not sure how to make it so the board knows when I am pressing a key.
    I would think that you could make the board read through hexidecimal values of keys or something.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-26 07:16
    You can't make it so the board "knows" when you're pressing a key. The PC sends the character when you press the key. Unless the Stamp is listening (with a DEBUGIN or SERIN), the character is never "seen". In order to listen, the Stamp can't do anything else. Sorry.

    You could attach a couple of pushbuttons to Stamp I/O pins or a 4 x 4 keypad and the Stamp could scan that. The reason that would work is that the Stamp program could probably check to see if one of the buttons is pressed several times a second and you would hold down the button typically longer than that. There are examples of this in the "What's a Microcontroller?" tutorial and some of the Nuts and Volts Columns, both downloadable from Parallax.

    You can easily read decimal or hexadecimal values from the PC, but the Stamp has to wait for the characters to be typed. Look at the documentation on the DEBUGIN statement for examples, but the following will wait for characters to be entered, ignore any leading non-digits, accept a decimal number, and stop when the first non-digit is typed after the number.

    DEBUGIN DEC temp

    You can use the DEBUG statement to prompt the user like this:

    DEBUG "Enter a value "
    DEBUGIN DEC temp
  • PwnSaladPwnSalad Posts: 8
    edited 2012-04-26 07:47
    I have looked into the Debugin command.
    Now my question is:
    Is there any way to read this?

    Example : If I press x is there any way to do this:

    If text = x then...?
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-26 07:54
    See my post #5. If that doesn't answer your question, please restate your question in more detail.
  • PwnSaladPwnSalad Posts: 8
    edited 2012-04-26 08:02
    When I run the code
    DEBUGIN DEC temp
    
    DEBUG "Enter a value "
    DEBUGIN DEC temp
    

    I get an error that says "expected a variable"
    temp being the highlighted error.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-04-26 08:06
    In post #5 I said "temp is declared as a byte or word variable". You need to either declare temp with a VAR statement or use some other variable name that's not being used at that point in your program.
Sign In or Register to comment.