Shop OBEX P1 Docs P2 Docs Learn Events
using pin as an input and an output. — Parallax Forums

using pin as an input and an output.

tedbeautedbeau Posts: 48
edited 2008-04-08 12:48 in BASIC Stamp
Ok, I searched the forum post and the support area and cannot find an example. I know the documentation says it is possible to use the same stamp pin as both a input and an output.

My question is, in the Button command, what is the difference between downstate and targetstate. Which one decides when to perform an action. In the description below, I want to change which input causes the same action to occur. I also need to know if the output state of a pin effects the input.
····················· VDD
························ l
························ l
·······················LED
························ l········switch··
························ l_______/ _____/\/\/\/\_________PO
················································ 200 ohm
················································



Step 1.
Here is what I want, at startup of the program pin 0 is using a pull down resister to light an LED. There is also a slide switch in the circuit that is closed, between VDD and the LED. P0 is an output and I switch it from high, (LED off) Pause, switch it to low, (LED on) Pause, repeat. This is so I know the program is running and it gives me a delay to set the robot into position and back away.

Step 2
Main program runs.

When the program finishes I want to be able to rerun it by changing the switch position. Since the switch is closed, the input of P0 should be in a high state so I want to use the button command, with downstate(or is it targetstate) of 0.·I realize if I use a momentary button I could just watch to see PO go low when it is pressed, but I do not want to have to hold the button down while step 1 above is running to see if the LED flashes. So I slide the switch to open position, the downstate/targetstate of·pin P0 now reads low. When I see this I·change the button downstate/targetstate to High. (this will be explained in step 3). Then I go·back to the main run portion of the program in step 2 above.

Step 3.
Now when the program finishes the switch is still open and if I had left my button downstate/targetstate at low I and would automatically loop back to step 2. Since the d/tstate was changed I wait for the switch to be moved again. If I want to run the program again I slide the switch to closed, matching the d/tstate of high, reset d/tstate to low, return to main. and on and on and on......
·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-04 19:01
    The BUTTON statement is intended to service a pushbutton where there is significant contact bounce and where you may want a repeat action where the button appears to be pressed multiple times as it actually is held down. In your case, you have a slide switch which behaves differently. I don't see where a BUTTON statement fits or even works properly for you. I'm also confused about your diagram and why you've connected the I/O pin as you have. Do you want to control the LED with the switch or with the Stamp I/O pin, but only if the switch is closed or what? When the switch is open and the I/O pin is supposed to function as an input, it's floating and will easily pick up noise and falsely trigger. Before you try to figure out how to program things, you need to figure out the hardware: What happens when the switch is open or closed? What happens when the I/O pin is high, low, or input? How is the LED supposed to function in each condition?
  • tedbeautedbeau Posts: 48
    edited 2008-04-07 12:30
    The LED is only important as a startup indicator. I saw a suggestion on a robot build page that suggested an led cycle loop at startup in case there was a wiring problem. i.e. to much motor draw that causes a chip reset. With the led flash loop, you can at least know the program has started and is running at least thru that point. I was trying to reduce the number of pins used, buy reusing pin 0 for a switch controlled function, once the startup loop is finished. I want to change P0 from output used in startup loop, to an input pin, and use it to see if the slide switch is open, or closed. I guess what I am confused about is, in the parameters for the button command there is a downstate, and a targetstate. Aren't they the same thing, I mean, assuming the setup I have shown above, if P0 is an input, when the switch is closed the downstate would be High, and I guess the target state would also be high. dpepending on which run mode I want to use, the switch would be open, or closed. My code would check to see if the switch was closed. Would I check Targetstate, or downstate, or I suppose I could check PO directly! That, I guess is my question, how do you use the downstate and targetstate parameters in a button command.

    If I have to, I can use a seperate Stamp pin to check the switch, but since the manual suggests that it's possible to use the pin as an input and an output I thought I would save a pin this way.

    Ted
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-07 13:43
    Please read the chapter on the BUTTON statement in the Stamp Basic Manual. It has a description of all the parameters and some examples of its use. The Downstate parameter indicates whether the I/O pin is low (0) or high (1) when the button is pushed. The Targetstate parameter indicates whether the BUTTON statement should GOTO the Address label when the button is pushed (1) or released (0). As the description clearly indicates, you have to use the BUTTON statement in a loop where it will be continually executed from time to time.

    The way you are using the slide switch doesn't fit the way the BUTTON statement is intended to be used. When the switch is open, you have a floating input on the Stamp and it will react to electrical noise in the vicinity. You need a pulldown resistor, perhaps 100K to ground from the I/O pin.

    In your program, you'd just test for the closed state of the switch with "INPUT 0" followed by "IF IN0 = 1 THEN GOTO closed". If the switch is closed, you can turn on the LED with "LOW 0" or turn it off with "HIGH 0" or "INPUT 0".
  • tedbeautedbeau Posts: 48
    edited 2008-04-07 15:46
    Thanks Mike, I will add the pulldown resister. I originally had thought I might need one. It looks then like I can just read IN0 to determine if the switch is open or not.

    Thanks
    Ted
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-07 16:08
    You can also add a 0.1uF capacitor from I/O pin 0 to ground to slow down the switch closure signal. A slide switch should have less contact bounce than a pushbutton anyway, but this will help as well. The pulldown will discharge this capacitor which is what you want.
  • tedbeautedbeau Posts: 48
    edited 2008-04-08 12:48
    I had an epiphany the other day, since what I wa wanting to do originally was rerun the program a second time with the same data that was entered the first time without having to save it to the eeprom it dawned on me "Why not but a loop to check for a button press at the end of the run?" I was trying to check at the begining of the program to see if the slide switch was in a new data or reuse old data position. I am sure it would just be simpler to run the program and then go into a loop and wait for a rerun button press.
    Sometimes I just need to think about this stuff in the back of my head for a few days.

    Thanks for all the help though. The forums like this one and the NSbasic for Palm forum are wonderful places where people give freely of their time and experience to help each other. I always try to add any suggestions to challenges that I have run across, of course I'm a newbie as far as Stamps go so it may be a while before I am to contribute here.

    Ted
Sign In or Register to comment.