Shop OBEX P1 Docs P2 Docs Learn Events
Inverting binary with basic stamp and solid state relay — Parallax Forums

Inverting binary with basic stamp and solid state relay

bigfreshbigfresh Posts: 16
edited 2008-03-04 03:13 in BASIC Stamp
I'm trying to program my basic stamp (board of education basic stamp 2) to control 5 record players, using binary, relayed by the COMFILE SSR8 cubloc.com/product/01_03ssr8.php. I dont have the code in front of me but i believe the lines to turn all the turntables on for 5 seconds and then off for 5 seconds is:

DIRH= %11111

OUTH = %00000 <--on
PAUSE 5000
OUTH = %11111 <-- off
PAUSE 5000

So as you can see the way I have to program the board is inverted. 0 means on. 1 means off. Someone told me that the SSR8 reads the code inverted. Or perhaps the Basic Stamp is sending it inverted. Either way it would be a lot easier for my brain to send commands as 0 meaning off and 1 meaning on. Any clues? I've read about the SEROUT command but cant get anything to work.

Another question. Is it possible to declare PAUSE within a variable?
e.g.: A = PAUSE 5000
That throws me an error.

Thanks for any help!

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2008-02-27 02:53
    No, but you can say:

    A = 5000
    Pause A
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-02-27 03:39
    Something you may find less confusing is to assign on and off a constant value.

    on CON %00000
    off CON %11111

    OUTH=on
    OUTH=off

    this is probably not going to be the eventual answer because you might need to control the on off switching of each player independently but it serves as an example thats easier on the mind and the kind of thing that you can do.

    Jeff T.
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 03:49
    thanks for the help...

    yes i will definitely want to control the players independently so i'm going to need to find something else...or just reorient my thinking to inverted binary, but that seems really dumb.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-02-27 04:00
    ·~
    That's the symbol for bitwise inversion:
    · A = %00100···'write this as 1=on, 0=off
    · B = ~A······ 'Therefore, B = %11011
    ·
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 04:13
    Thanks PJ Allen. While that could be useful for something, my real goal here is to basically write a composition. I'd like to have a long series of unrelated binary instructions, so storing them as variables and then inverting each one with another variable isnt going to be feasable. something like...

    OUTH = %00100
    PAUSE 5000
    OUTH = %10011
    PAUSE 5000
    OUTH = %10001
    PAUSE 5000
    OUTH = %10111
    PAUSE 5000

    and so on and so on....

    so im trying to invert the entire output
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-02-27 04:44
    bigfresh -

    The only way I can see that you can do what you appear to want to do, is to use five hardware inverters on the output pins of each pin port.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Genius is one percent inspiration and ninety-nine percent perspiration."

    Thomas Alva Edison
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 06:49
    is this what you're talking about? this says its a hex inverter, i couldnt find any for strictly binary...
    www.solarbotics.com/products/74ac14n/
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2008-02-27 07:39
    bigfresh -

    What you need is a 7404 (74xx04) hex inverter. They should be available most anywhere, but I noticed that Solarbotics doesn't offer them - sorry.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "Genius is one percent inspiration and ninety-nine percent perspiration."

    Thomas Alva Edison
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-02-27 14:01
    bigfresh,

    Nevermind.
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 14:17
    thanks bruce i'll track one down
  • terry_bearterry_bear Posts: 84
    edited 2008-02-27 17:53
    Guys,

    Rather than using additional hardware, such as a hex inverter (which should work), couldn't one simply use the inverted form of baudmode ? That is, 16468 instead of 84, for a BS2?

    Just asking <grin>

    Terry
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 17:58
    That is exactly the answer I was looking for...but if you have any help on how to change the baudmode, that would be great....something with
    SEROUT 0, 16468 ................?
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-02-27 18:15
    Hi bigfresh, looking at the SSR8 it takes a logic 1 to switch on and a logic 0 to switch the relay off so I don't know where the inversion is occuring, maybe I looked at it wrong.

    Setting that aside and dealing with the task of allowing you to enter a logic 1··equals on and transparently inverting it for·an output that equals 0 then PJ Allen's suggestion of the tilde operator is the ideal solution. The bitwise invert (~) does exactly the same thing to the value as adding a hex inverter to your circuit, so unless you have current limitations you are wasting your time

    You don't say where or how you are setting the values of the binary output but your example of OUTH followed by a PAUSE repeatedly is not an economical·or versatile way to switch the outputs. Where you have code that repeats often it is better placed inside a sub routine, for example

    Output_values:

    OUTH = ~switch_value······· 'entering a value of %00001 would output a value of %11110 switching on player 1

    PAUSE Pause_value··········· 'you can vary the pause time here

    RETURN

    If you need assistance on how you would pass a value to·a sub routine post more details of where and how you want to control the record players.

    Jeff T.
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 18:53
    ok, basically the first line of the code is this

    DIRH = %11111  'this sets pins 0-5 to output on the stamp
    
    



    and now as you say i should declare a subroutine

    Output_values:
    
    OUTH = ~switch_value        'entering a value of %00001 would output a value of %11110 switching on player 1
    
    PAUSE Pause_value            'you can vary the pause time here
    
    RETURN
    
    



    Now every digit of the binary corresponds with a record player (5 record players). I want to turn the record players on and off indiscrimiately, and irregularly so they cant be included in a loop i can repeat.

    So if i were to call that subroutine every time i want a different record player state wouldnt i have to do this:

    switch_value = %01001
    pause_value = 5000
    GOSUB Output_values
    
    switch_value = %01101
    pause_value = 2000
    GOSUB Output_values
    
    switch_value = %00011
    pause_value = 10000
    GOSUB Output_values
    
    



    ...and so on and so on....

    That is actually less economical than the repetitive code than i had originally! But it is more versatile. Got any improvements?

    Thanks,
    Brian

    EDIT: P.S: What i would really love is to just declare binary values one after another with only a call to the subroutine at the end, and only change the pause value when i have to. So i could kind of have a visual map of what is going on....

    eg: 
    pause_value = 5000
    switch_value = %10000
    switch_value = %01000
    switch_value = %00100
    switch_value = %00111
    switch_value = %00100
    switch_value = %01000
    switch_value = %10000
    pause_value = 2000
    switch_value = %11111
    switch_value = %11110
    
    

    Post Edited (bigfresh) : 2/27/2008 7:04:22 PM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2008-02-27 19:47
    Hi bigfresh, there are several ways you can use the sub routine. The 5 outputs have 32 possible combinations, you could place those 32 different possibilities in a lookup table and reference them with a random or preset number for the index or you could just generate a random number between·1 and 32, so if the random value (switch_value) was equal to 17 the the outputs would be %10001 etc. (if the baud rate was indeed the inversion problem you can omit the tilde). You can do a similar thing·to vary the·pause if need be.

    RANDOM switch_value

    switch_value=switch_value//32 +1

    GOSUB Output_values

    Jeff T.
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-02-27 19:53
    Like this, perhaps?



    ' {$STAMP BS2}
    pause_value VAR Word
    switch_value VAR Word

    MAIN:
    · pause_value = 5000
    · switch_value = %10000 : GOSUB OutputValue
    · switch_value = %01000 : GOSUB OutputValue
    · switch_value = %00100 : GOSUB OutputValue
    · switch_value = %00111 : GOSUB OutputValue
    · switch_value = %00100 : GOSUB OutputValue
    · switch_value = %01000 : GOSUB OutputValue
    · switch_value = %10000 : GOSUB OutputValue
    · pause_value = 2000
    · switch_value = %11111 : GOSUB OutputValue
    · switch_value = %11110 : GOSUB OutputValue
    · GOTO MAIN

    OutputValue:
    · OUTH = ~switch_value······· 'entering a value of %00001 would output a value of %11110 switching on player 1

    · PAUSE Pause_value··········· 'you can vary the pause time here
    · RETURN
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 20:12
    allanlane5, I wont be able to test your solution until tonight or tomorrow, but it is entirely what I am looking for. Unsoundcode, while your idea is a bit cleaner it's a bit less user friendly for my intents and purposes.

    many many thanks,
    Brian
  • PatMPatM Posts: 72
    edited 2008-02-27 20:16
    Something isn't right. The SSR8 turns on when current flows through it (logical 1 on the pin). However, that assumes you are going from the pin through a resistor·to the SSR8 and then from the SSR8 to ground. If you are hooking the + side of the SSR8 to power through a resistor and then from the SSR8 to the stamp pin that would operate in the manner you describe (pin needing to sink current instead of supply it).

    Explain how you've connected the SSR8 because there's more to worry about than just the logical on state. Driving the 8 optocouplers (LEDS) may be too much current for the stamp. My memory is vague· but I believe the stamp is quite limited in the total current it can handle on a port. The SSR8 was designed for use with Cublocs (Atmel mega 128s) which has a much higher current handling capability.

    You need to make sure 8 SSRs turned on at once don't exceed the stamp's capability. If there is a danger of over-current then you can use transistors (3904 or 3906, or ULN2803 since there are 8 inputs).

    ·
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-02-27 20:54
    I believe the BS2 can source or sink 25 mA per pin, BUT only a total of 50 mA over all pins. So yes, driving 8 LED's might be a problem, unless each one takes 6 mA or less.

    And having reviewed the CuBloc site, I think you'll definitely need the Darlington chip -- but only one.· It looks like the CuBloc boards incorporate optically isolated digital outputs already, so the Relay board control signals will require higher currents than the BS2 can put out by itself.· ulm2803 should do it though.

    Post Edited (allanlane5) : 2/27/2008 9:06:54 PM GMT
  • bigfreshbigfresh Posts: 16
    edited 2008-02-27 21:02
    at most i'll be using 6 of the relays. the stamp will always be set to output, not input. my teacher seems to think the SSR8 has built-in resistors, and said that i can connect directly to the ssr from the stamp without additional resistors. but this is the same teacher who said that binary inversion is impossible and that i should get used to writing inverted binary, which i would not accept.

    anyhow, as of a few days ago i have everything setup and running and no burnouts..........
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-02-27 21:06
    Good, just measure the currents to make sure -- stick a cheap multi-meter set on Amps between one of the BS2 I/O pins and the SSR8. It'd be a shame to 'cook' your BS2 dead slowly over time. Forwarned is for-armed.
  • terry_bearterry_bear Posts: 84
    edited 2008-02-28 00:31
    bigfresh,

    In regard to the Baudmode approach; 84 works on the DEBUG port, so I'm assuming (yeah, I know) that 16468 will work on the other pins. I'm not in a position to test...

    The command to use the pin would be something like SEROUT "pin-number", baudmode, [noparse][[/noparse]serialdata in hex format]

    The brackets are necessary. There are options for flow control and time-out if permission is never received from the other end.
  • bigfreshbigfresh Posts: 16
    edited 2008-03-04 03:13
    allanlane: just to update...your code worked beautifully. thanks to everyone really, you saved me hours of frustration.
Sign In or Register to comment.