Shop OBEX P1 Docs P2 Docs Learn Events
Addressing an individual pin in a loop — Parallax Forums

Addressing an individual pin in a loop

ShmuelShmuel Posts: 14
edited 2005-11-16 01:22 in BASIC Stamp
Toward the begining of my code I have

BUTTON1 VAR IN1
BUTTON0 VAR IN0
 
Button1Pressed VAR Bit
Button0Pressed VAR Bit





Now I'm trying to write a subroutine to poll for activity on each switch. Because of the possibility of someone pressing and holding the button, as opposed to pressing and releasing in one cycle, I've decided to write my own routine. (Unless you have something better to recommend.)

Instead of writing a subroutine for each button, I want to have one subroutine poll every button. In pseudo-pseudocode, I'd think it would look like this.

FOR count = 0 TO 1 

[indent]IF (BUTTON.count = 1) THEN    'I also tried IN.count  
...
Button|"count"|Pressed = 1
...
ENDIF
[/indent]

NEXT



The crux of the problem is using the loop counter in a statement that refers to a specific pin. I'd rather avoid a SELECT statement.

I'm new to Pbasic so it's possible I'm overlooking an instruction.

I'd appreciate your help

Thanks,
Shmuel

Comments

  • Ryan ClarkeRyan Clarke Posts: 738
    edited 2005-11-15 23:21
    Are you only using two buttons? You may try reading in the value of the INH or INL and bit-masking off the pins that aren't possible (i.e. not button inputs)-

    Really depends on the larger scope of the program...

    Ryan

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ryan Clarke
    Parallax Tech Support

    RClarke@Parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-15 23:25
    Using PBASIC keywords as variable names is not a good idea -- it will quickly lead to programming errors.

    I frequently scan multiple inputs with a subroutine.· The code usually looks like this:

    Get_Btns:
    · btns = %0011
    · FOR idx = 1 TO 5
    ··· btn1 = btn1 & IN0
    ··· bnt2 = btn2·& IN1
    ··· PAUSE 5
    · NEXT
    · RETURN

    The variables btn1 and btn2 are aliases for btns.BIT0 and btns.BIT1.· The subroutine scans the buttons and forces them to be pressed for 25 millisceonds before being registered as valid.· If the button is released in the middle of the loop the ANDing of the input will clear that variable.· This technique is discussed in StampWorks.· You can download it from the following link:

    http://www.parallax.com/detail.asp?product_id=27220

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • DannyDanny Posts: 56
    edited 2005-11-15 23:41
    Jon:
    Just tried to download that manual, the link will only let you save as a link not the PDF.
    I'm planning buy it, but wanted to download it and get a head start!!
    Danny

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Never create anything you can't control"
    "The amount of intelligence on the planet is fixed... the population is growing"
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-15 23:51
    We noticed that too -- sorry.· Our web folks are working on it.· I've attached the experiment from the book that deals with multiple input debouncing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • ShmuelShmuel Posts: 14
    edited 2005-11-16 00:29
    Thanks. What would be the difference between using this code and the BUTTON command?
  • ShmuelShmuel Posts: 14
    edited 2005-11-16 00:41
    Should have included this in my last post.

    You mentioned keeping an eye toward the larger project.

    The project is a digital clock/watch. I'm using a DS17887 RTC chip. I've gotten the STAMP to interface with the RTC and can set the time from CON declarations in the file. I'm now trying to up the ante and set the time from buttons as inputs.

    For now we'll say there are two buttons. One will scroll through positions: Hrs->Mins->Month->Date. The other will scroll/roll through values in each position.

    Let's say that it makes a difference whether the button is pressed or held. For instance to make the values scroll one at a time or quickly.

    I was thinking of using BUTTON that I came across before reading your post. Now I'm not sure.

    Shmuel
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-16 01:22
    Don't use BUTTON -- it's the one keyword that I tend to avoid; not that it's bad, but it's less helpful that one would think it is, especially if you have to scan multiple buttons.· In StampWorks, there is a clock project that may help you (attached).· The main loop includes a 100 ms delay so that a pressed button will not advance the clock too quickly.

    The link to the StampWorks PDF should be working now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.