SX28 protoboard using SX/B Button/Switch issue

Hello-

For the life of me I can't figure this out. Been reading through the forums and trying things all day to no avail. It's probably super easy and I'm just missing it. In my project I have a micro switch that once it is pressed and held I would like the code to stop after it's done. (It keeps looping when I hold the switch) Not sure what to do to keep it from looping? Need the LED to stay HIGH when button is held after it goes through first part of code. Once button is released the LED goes LOW. Any advise is much appreciated. Thanks! Here is the code--


DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ 4_000_000

PButton1 PIN RA.0
Led PIN RC.7

duty VAR Byte


PROGRAM Start

Start:

IF PButton1 = 0 THEN

HIGH Led
PAUSE 100
LOW Led
PAUSE 100
HIGH Led
PAUSE 50
LOW Led
PAUSE 500
HIGH Led
PAUSE 100
LOW Led
PAUSE 100
HIGH Led
PAUSE 100
LOW Led
PAUSE 100
HIGH Led
PAUSE 2000
FOR duty = 255 TO 1 STEP -1
PWM Led, duty, 3
NEXT
PAUSE 100
FOR duty = 1 TO 255 STEP 1
PWM Led, duty, 3
NEXT
PAUSE 500
HIGH Led
PAUSE 100
LOW Led
PAUSE 100
HIGH Led
PAUSE 500
LOW Led
PAUSE 100
HIGH Led
PAUSE 500
LOW Led
PAUSE 50
HIGH Led
PAUSE 50

ELSEIF PButton1 = 1 THEN

LOW Led
PAUSE 50

ENDIF

GoTo Start

Comments

  • 4 Comments sorted by Date Added Votes
  • Your code looks like it should make the LED low when the PButton1 pin is high.
    If you want it to be HIGH, then you need to change the line after "ELSEIF PButton1=1 THEN" from "LOW Led" to "HIGH Led".

    You really only need ELSE and not the "ELSEIF PButton1=1 THEN" because the pin can only be 0 or 1 anyway.

    Other than the ELSEIF the code looks okay to me.

    Do you have a pull-up on the button pin ? How is it connected ?

    Bean
  • Thanks Bean for the help!

    I guess I should explain this a little better so we are on the same page. I am using the SX Professional Development Board for mock up and then will be using one of my stashed SX28 protoboards for final placement in project. I'm using one of the pushbuttons on the development board to test. In the project I'll be using a small micro switch and will wire accordingly. This button/switch will just be used to turn a LED on and off pretty much. I'm using board power just for the chip. I'm then using 12vdc through a N-Channel Mosfet for the LED's as they are large 20mm models. Grounds tied together of course. (Learned the hard way about that! Crazy things happen if you don't!) :-)

    The code above works for what I need. What I'm having troubles with is in the project the micro switch will be held on and I need the upper portion of code to just go through once and then stop with the LED's on or HIGH. The way it is now it just keeps looping the upper portion when the switch is held.

    Maybe I should explain what I'm doing.... The LED's are eyes in my project. When the switch is closed and held the eyes turn on and off and use a little pwm to act as a powering up type sequence. I only need that sequence once and then they stay on or HIGH as long as the switch is held. Once the switch is released or opened the LED's go off.

    I'm wondering if I need to use some type of timer so it knows that the button/switch is held and will cause the upper portion of code to stop after one run through.

    Man I hope all this made sense??

    Thanks!!
  • Replace "ELSEIF PButton1 = 1 THEN"
    with
    DO
    LOOP UNTIL PButton1 = 1
    

    That should do it...

    Bean
  • Bean-

    That was the trick. Works perfect. Thanks a bunch! I still need to figure out how this made it work though. But I will now move on to the next stage of my project. Thanks again!
Sign In or Register to comment.