Shop OBEX P1 Docs P2 Docs Learn Events
OEM BS2SX My program crashes and stops abruptly. — Parallax Forums

OEM BS2SX My program crashes and stops abruptly.

beazleybubbeazleybub Posts: 102
edited 2008-04-06 02:31 in BASIC Stamp
Hello everybody,

I am having trouble with a project I am working on using the BS2SX and hope someone will please check over my code and let me know if I am doing anything wrong. When I run the following code I get erratic results.

'{$STAMP BS2sx}
'{$PBASIC 2.5}
 
relay3 VAR Byte 'Varables
relay5 VAR Byte
relay10 VAR Byte

 
main:
 
IF (IN15 = 1) AND (IN7 = 1) THEN
 
FOR relay3 = 1 TO 3

HIGH 1                          
PAUSE 25
HIGH 2
PAUSE 25
LOW 1
PAUSE 25
LOW 2
PAUSE 25
HIGH 5
PAUSE 25
LOW 5
PAUSE 25
 
NEXT
 
ELSEIF (IN14 = 1) AND (IN7 = 1) THEN
 
FOR relay5 = 1 TO 5
 
HIGH 1
PAUSE 25
HIGH 2
PAUSE 25
LOW 1
PAUSE 25
LOW 2
PAUSE 25
HIGH 5
PAUSE 25
LOW 5
PAUSE 25
 
NEXT
 
ELSEIF (IN13 = 1) AND (IN7 = 1) THEN
 
FOR relay10 = 1 TO 10
 
HIGH 1
PAUSE 25
HIGH 2
PAUSE 25
LOW 1
PAUSE 25
LOW 2
PAUSE 25
HIGH 5
PAUSE 25
LOW 5
PAUSE 25
 
NEXT
 
ENDIF
 
GOTO main


What is happening is my program stops in progress and dosent complete or dosent work all together and sometimes it does seem to work. Have I used the wrong commands? Thank you!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
What if there was nothing? Nothing is something!

Post Edited (beazleybub) : 4/6/2008 2:34:34 AM GMT

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2008-04-04 17:03
    Those "Relay" names, and the 25 mSec on, 25 mSec off, make me think you're driving relays.

    First of all, if IN7 isn't held high, nothing happens.

    Second of all, driving three relays takes quite a bit of current (depending on the relays) which tends to lead to 'brown-out' resets if your battery/supply isn't up to it.

    Oh, and you DO have "protection diodes" installed across your relays, right?· Because when you 'release' a relay, the collapsing magnetic field generates a current pulse.· And that current pulse needs somewhere·to go (like through a protection diode) so it doesn't go through your I/O pins.
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-04 17:19
    allanlane5
    Thank you for your quick reply!

    Yes I am driving "two" relays but I am using NPN trasnsistors to do the heavywork and I do have protection diodes in place. I fully understand that a spike will eventually kill any stamp. [noparse]:)[/noparse]

    I do not think a brownout condition exists because if I use this code the relays run perfectly.

    '{$STAMP BS2sx}
    '{$PBASIC 2.5}
     
    DO
    
    
    HIGH 1
    PAUSE 25
    HIGH 2
    PAUSE 25
    LOW 1
    PAUSE 25
    LOW 2
    PAUSE 25
    HIGH 5
    PAUSE 25
    LOW 5
    PAUSE 25
     
    LOOP
    

    When you say "First of all, if IN7 isn't held high, nothing happens." Do you mean that it has to be held down?
    IN7 is a momentary pushbutton and the rest of the inputs are connected to a 8 position dip switch. I am using 220 Ohm and 4.7K Ohm resistors for the pullup pulldown. IN7 also uses them.

    From what I gather your saying my code is right? (Though I should use a DO LOOP)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-04 17:40
    If the problem is with the momentary pushbutton how can I change the code so that it executes properly when the·button·is pressed and then released.

    Thank you for your time. [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-04-04 18:24
    Hi, a way that might be a good way to deal with this is to look at P13 P14 P15 as one using the INS instruction (look under memory and variables in the help file). If you can reassign the pushbutton to P12 that would be even better. Using a do loop you can monitor the value of IND, when the button is pressed you come out of the do loop with a value that is dependent on the pushbutton value and the position of the dip switch

    DO
    value=IND
    LOOP UNTIL IN12>0··· 'IN12 assigned to PB

    Now you only need one sub routine for the relays passing it a counter value. Using SELECT-CASE you can look at the variable "value" , if value=3 then you have dip switch set to P13 and the pushbutton has been pressed so set the counter to 10 counts, if value=5 then you have dip switch set to P14 and the pushbutton has been pressed so set the counter to·5 counts and finally if value=9 then you have dip switch set to P15 and the pushbutton has been pressed so set the counter to·2 counts.

    the sub routine would be something like .......FOR relay_count=1·TO counter

    Jeff T.
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-04 18:33
    Jeff,

    That sounds great but I have already manufactured my PCBs and there is no way for me to relocate P7
    Is there anyway to do this still using P7?

    P8 through P15 Are dedicated to the 8 position dip switch and P7 is dedicated to the push button.

    Can you explain to me why my code does not work? Is it because P7 is not held down?

    I'm in a bit of a pickle here arent I ?!freaked.gif

    Thanks!tongue.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-04-04 18:43
    Hi, to do the same using P7 as the PB make the do loop as follows

    DO
    value=IND
    value=value & 14
    LOOP UNTIL IN7>0

    the bitwise and (&) will mask out the lowest bit reading only the three higher bits which will give values of 2=dip P13, 4=dip P14 and 8=dip P15.

    when the PB is pressed the do loop will exit with one of those three values (of course if the dip switch is not set the value will be zero)

    BTW do you have pull down resistors on your switches.

    Jeff T
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-04 19:05
    Jeff,

    Yes I do have pull down resistors on my switches.

    I used 220 and 4.7K Ohm resistors and have them set like the example in the "what's a microcontroller book on page 77"

    Thanks, Robert


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!

    Post Edited (beazleybub) : 4/6/2008 2:36:33 AM GMT
    240 x 187 - 17K
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-04-04 19:09
    You're probably ok hardware-wise -- but I do have one question.

    What IS the purpose of In7? Do you press the button once, then the program loops forever? Is the program supposed to loop once, then wait until somebody presses the button again?

    The point is, there's a coding solution for both situations, but I can't tell from your current code what behavior you want from pressing the button.
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-04 19:16
    When PB in7 is pressed and released it would start the program and·loops it only once per press.
    The dip switch configuration would determine which part of the code would be executed.

    Then the program would be ready to run again whenever PB in7 is pressed again.

    Thanks guys!

    I have to go to work now, I will check back with you later.

    Thank you VAR WORD

    FOR Thank you = 1 TO 1000roll.gif


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!

    Post Edited (beazleybub) : 4/4/2008 7:21:40 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-04-04 19:30
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ButtonState VAR Bit
    ButtonState = 0
    MAIN:

    ' The next piece waits for the IN7 button to be
    ' pressed -- forever if necessary...
    DO WHILE ButtonState = 0
    · ButtonState = IN7
    · PAUSE 100
    LOOP
    ' When here, we KNOW the In7 button has been pressed...

    IF ButtonState = 1 THEN
    '...
    ENDIF
    '...
    '...
    '...
    ' This is at the end -- where we want to make sure
    ' the button has been released.
    DO WHILE ButtonState = 1
    · ButtonState = In7
    · PAUSE 100
    LOOP
    GOTO MAIN
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-04 19:37
    I want to thank both of you for your kind efforts to help me.

    I will take what both of you have shown me and learn from it.

    Possibly one day I too will lend a helping hand to those who need it.

    Robert

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-05 14:59
    allanlane5,

    Thank you again for your guideance.

    Here is what I did with your example..

    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}
    
    ButtonState VAR Bit
    ButtonState = 0
    RelayCount VAR Byte
    MAIN:
    ' The next piece waits for the IN7 button to be
    ' pressed -- forever if necessary...
    
    DO WHILE ButtonState = 0
      ButtonState = IN7
      PAUSE 100
    LOOP
    
    ' When here, we KNOW the In7 button has been pressed...
    IF IN15 AND ButtonState = 1 THEN
    FOR RelayCount = 1 TO 10
    HIGH 1
    PAUSE 250
    LOW 1
    PAUSE 250
    HIGH 2
    PAUSE 250
    LOW 2
    PAUSE 250
    HIGH 5
    PAUSE 250
    LOW 5
    PAUSE 250
    NEXT
    ENDIF
    '...
    '...
    '...
    ' This is at the end -- where we want to make sure
    ' the button has been released.
    
    DO WHILE ButtonState = 1
      ButtonState = IN7
      PAUSE 100
    
    LOOP
    GOTO MAIN
    
     
    

    It works except when I add this for a status LED..
    HIGH 5
    Pause 250
    LOW 5
    Pause 250
    The relays slow down. Is it because of overhead?
    Second I am kinda unsure how to approach adding ELSIF statements to this so I can add other dip switch configurations. Ultimately I am looking to have this program count 3, 5, 10, 25, 50 relay counts depending on what the dip switch is set to.
    Thanks again!

    Robert


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!

    Post Edited (beazleybub) : 4/5/2008 3:33:13 PM GMT
  • jmalaysiajmalaysia Posts: 97
    edited 2008-04-05 17:10
    It slows down because you are adding 2 ea 250ms delays to flash the LED.

    HIGH 1
    PAUSE 250
    LOW 1
    PAUSE 250
    HIGH 2
    PAUSE 250
    LOW 2
    PAUSE 250
    HIGH 5
    PAUSE 250 (here)
    LOW 5
    PAUSE 250 (and here)
    NEXT
    ENDIF


    Try this...

    HIGH 1
    HIGH 5
    PAUSE 250
    LOW 1
    LOW 5
    PAUSE 250
    HIGH 2
    HIGH 5
    PAUSE 250
    LOW 2
    LOW 5
    PAUSE 250
    NEXT
    ENDIF


    To match the number of counts to the dip switch I would try a variable in the for-next loop.· In the last code you posted,· for example,· use

    LastRelay· var byte

    If IN15=1 then LastRelay=10

    For RelayCount=1 to LastRelay
    · (insert counting routine)
    Next
  • beazleybubbeazleybub Posts: 102
    edited 2008-04-06 02:31
    I just wanted to let everybody know that I got the bugs out of my code and I picked up some great information from you all.

    My board runs great and exactly the way I envisioned it!

    Thank you so much for your time... Robert roll.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    What if there was nothing? Nothing is something!
Sign In or Register to comment.