May 04, 2024, 04:58:30 AM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


What's the best approach to expanding my program?

Started by Jeff Haas, July 08, 2012, 12:25:02 AM

Previous topic - Next topic

Jeff Haas

I'm looking at upgrading a prop from last year and adding new features to it.  Specifically, I've got a Prop-2 watching for a button press, which then turns on a relay (activating a rotating light), creates sound effects through a speaker, and finally triggers an RC-4 at the end of the sequence.  This is part of a mad scientist lab and was in a sci-fi computer console.

The console had blinking lights handled by a 555-based board, but it wasn't that effective, because it just had a single pattern.  Also, when it was outside, the colder the temperature got, the slower the 555 board ran...to the point where it almost looked like it wasn't blinking.

There are about 16 lights that I'd like to control, which is more outputs than a Prop-2 will handle on its own, but I was thinking of adding two 74HC595 shift registers to control the lights instead, which would allow me to have an "idle" mode and an "active" mode after the button is pressed.

My first question is, will adding shift registers work?  Can a Prop-2 keep a pattern going on shift registers while doing other things? 

My alternate solution would be to have a second prop controller running the shift registers and have it watch for the Prop-2 to signal it, similar to pressing button 1 for pattern 1 and button 2 for pattern 2.

All advice welcome!

' =========================================================================
'
'   File...... Embryo Chamber Halloween 2011
'   Purpose... To creep people out
'   Author.... Jeff Haas
'   Started... Sept. 4, 2011
'   Updated... Oct. 2, 2011.  Added rotating light and klaxon.
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ I/O Definitions ]-------------------------------------------------

CLICK        PIN     8               'Pushbutton SETUP = DN; Momentary contact switch, pins W & R on Prop-2
REDLIGHT     PIN     14              'Relay connects to rotating red light on pin 14 and V+
Sio          PIN     15              'RC-4 communications - controls electric crackers on relay 1
' Note that the speaker for the SFX connects to V+ and OUT2.

' -----[ Constants ]-------------------------------------------------------

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T2400       CON     396
    T38K4       CON     6
  #CASE BS2SX, BS2P
    T2400       CON     1021
    T38K4       CON     45
  #CASE BS2PX
    T2400       CON     1646
    T38K4       CON     84
#ENDSELECT

SevenBit        CON     $2000
Inverted        CON     $4000
Open            CON     $8000
Baud            CON     Open + T38K4           'Baud jumper in

Addr            CON     %00                    'Address of RC-4, set by jumpers %00 - %11

IsOn            CON     1
IsOff           CON     0

Speaker         CON   2              'Speaker / amplifier pin

t               CON   544            'Constant for klaxon sound
y               CON   502            'Constant for klaxon sound

' -----[ Variables ]-------------------------------------------------------

id              VAR     Byte(3)        'RC-4 variable
relays          VAR     Nib            'RC-4 variable
status          VAR     Byte           'RC-4 variable
x               VAR     Word           'Loop counter
z               VAR     Word           'Second loop counter
note1           VAR     Word           'First tone for FREQOUT
note2           VAR     Word           'Second tone for FREQOUT

' -----[ Initialization ]--------------------------------------------------

Reset:
  DEBUG CLS                            'Init debug screen on PC
  LOW REDLIGHT                         'Reset relay and rotating light

' -----[ Program Code ]----------------------------------------------------

Main:

  IF (CLICK = IsOff) THEN Main         'Wait for pushbutton activity
  DEBUG CR, "Button pressed"

' -----[ Start rotating light, sound klaxon ]-----------------------

HIGH REDLIGHT
  DEBUG CR, "Warning light ON"

FOR x = 1 TO 2

  FREQOUT 2,100,50         'Klaxon - Short sound
  FOR z = 1 TO 140
    FREQOUT 2,9,y, t       'Klaxon - Long sound
  NEXT
  PAUSE 222

NEXT


' -----[ Sound effects, from BS2 docs ]-----------------------

  DEBUG CR, "ALERT!  EXPERIMENT ACTIVATED!"

Computer_Beeps:
  DEBUG CR, "50's Sci-Fi Computer"
  FOR x = 1 TO 25                               ' run about 5 seconds
    RANDOM note1                                ' create random note
    note1 = note1 // 2500                       ' don't let note go too high
    FREQOUT Speaker, 50, note1                  ' play it
    PAUSE 100                                   ' short pause between notes
  NEXT


Space_Transporter:
  DEBUG CR, "Space Transporter"
  FOR note1 = 5 TO 2000 STEP 5                  ' frequency sweep up
    FREQOUT Speaker, 10, note1, note1 */ 323    ' play note, note * 1.26
  NEXT


  DEBUG CR, "Zaaaap!"

' -----[ RC-4 relay control code ]-----------------------

  SEROUT Sio, Baud, ["!RC4", Addr, "V"]               'get version
  SERIN  Sio, Baud, 200, Main, [STR id\3]
  DEBUG CR, "RC-4 Version ", STR id\3, CR

    SEROUT Sio, Baud, ["!RC4", Addr, "R", 1, %0001]   'set outputs
    GOSUB Show_Status
    PAUSE 500
    SEROUT Sio, Baud, ["!RC4", Addr, "R", 1, 1]       'relay 1 on
    GOSUB Show_Status
    PAUSE 500
    SEROUT Sio, Baud, ["!RC4", Addr, "R", 1, 0]       'relay 1 off
    GOSUB Show_Status
    PAUSE 300
    SEROUT Sio, Baud, ["!RC4", Addr, "R", 1, 1]       'relay 1 on
    GOSUB Show_Status
    PAUSE 400

'Reset relays
  SEROUT Sio, Baud, ["!RC4", Addr, "X"]               'all off
  GOSUB Show_Status

' -----[ Stop rotating light ]-----------------------

LOW REDLIGHT
  DEBUG CR, "Warning light OFF"

GOTO          Main                                    'Back to Main

' -----[ Subroutines ]-----------------------------------------------------

Show_Status:
  SEROUT Sio, Baud, ["!RC4", Addr, "G"]               'Get status
  SERIN  Sio, Baud, 200, Show_Status, [status]
  DEBUG CR, "Status = ", IBIN4 status
  RETURN



bsnut

July 08, 2012, 08:11:26 AM #1 Last Edit: July 08, 2012, 08:22:04 AM by bsnut
QuoteMy first question is, will adding shift registers work?
Yes, you will need 3 I/O pins to be able to control the shift registers.
QuoteCan a Prop-2 keep a pattern going on shift registers while doing other things?
It can, with some limitations and it depends when you need these lights to be chasing. One you will have to make a subroutine that will be called every time you need to have the lights chasing. Remember that the Basic Stamp 2 the brains of the Prop-2 is a single minded controller and can only do one thing at a time.
QuoteMy alternate solution would be to have a second prop controller running the shift registers and have it watch for the Prop-2 to signal it, similar to pressing button 1 for pattern 1 and button 2 for pattern 2.
This wouldn't be a bad idea for you to do. Another thing is that you may want to use the HC-8+ to handle what your first Prop-2 is handling now and have it run the chasing lights. Or, you can use (hack) an HC-8+ to handle the whole show and have the shift registers connected to the option header and the rest of the show connected to the OUTS terminal strip like your current setup.

The great thing about hacking the HC-8+, it is powered by the Propeller which is able to multitask because it has 8 processors or what we call Cogs running at the same time.     
William Stefan
The Basic Stamp Nut


JonnyMac

July 08, 2012, 10:23:22 AM #3 Last Edit: July 08, 2012, 10:27:26 AM by JonnyMac
From a programming standpoint using the HC-8+ would in fact be easier because you could devote one of the processors (cogs) to running your shift-registers.  I have a 74x595 driver object that is ready to go (you can even to dimming if you connect LEDs to the outputs).  You would connect the shift-register board to the header pins.  This would leave you five high current outputs.  Remember, the HC-8+ has eight dedicated TTL inputs and a SERIAL pin that could be used to send commands to an device that will take a single-pin serial stream (can be used as a basic TTL IO pin, too).

If you want to try to use your Prop-2 then you need to learn to write "state machine" type programs. State machines allow single-threaded processors to appear to be doing multiple things at once.  I've written a few programs here that use this style, usually having to do with updating servos at regular intervals.  Having looked over your program, though, it may be tricky to keep timing in the loop very regular with all the serial things you're doing.

Here's a thought... you could use the HC-8+ as your main controller and your Prop-2 as your blinky light board.  You could even use the HC-8+ to send serial commands to the Prop-2 through one of the TTL pins.  You can do this with the Prop-2 because the SERIN function has a time-out feature.
Jon McPhalen
EFX-TEK Hollywood Office

Jeff Haas

Thanks for all the input!  That's what I love about this forum, the expertise available.

The HC-8+ looks great, but this is July and I'm not sure there's bandwidth to pick up a new programming language and get everything else done in time (I'm about to start a new job).

So let's think about one of my other options.  How would I connect two prop controllers?  Assume that one is running the shift registers and is waiting for one of two inputs.  Input A would trigger the idle pattern and input B would trigger the active pattern.  Then I could add a few lines to the current code which set an output high, and that won't tax the Basic Stamp 2 at all.

Do I set up connections from the TTL pins or the OUT pins?  Do I need a resistor to pull the line high or low so the other controller looks for a change?

JonnyMac

If you're using two of our controllers you can use the TTL pins directly. The only caution is that you must not allow the receiver side to drive the input pin(s) high.  The ULN circuitry acts like a pull-down so you don't need anything special if not using a pin that doesn't have a SETUP jumper.

If you only have two states then this can be handled with one pin.  When it's low the receiver will do one thing, when it's high the  receiver will do the other.  Here's an example Prop-1 program that looks at P6 (so I could test on Trainer). When the trigger is open the LEDs do a ping-pong pattern; when the trigger is pressed the LEDs do a random pattern.  Note that the code uses BRANCH to deal with the trigger input and at the end of each section jumps back to the top to re-test the trigger.

' =========================================================================
'
'   File......
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS1}
'   {$PBASIC 1.0}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

SYMBOL  Sio             = 7                     ' SETUP = UP; no ULN
SYMBOL  Trigger         = PIN6                  ' SETUP = DN


' -----[ Constants ]-------------------------------------------------------

SYMBOL  Yes             = 1
SYMBOL  No              = 0

SYMBOL  TrOn            = 1                     ' active-high trigger
SYMBOL  TrOff           = 0

SYMBOL  IsOn            = 1                     ' active-high I/O
SYMBOL  IsOff           = 0

SYMBOL  Baud            = OT2400


' -----[ Variables ]-------------------------------------------------------

SYMBOL  last            = B2                    ' for debounce loop
SYMBOL  idx             = B3

SYMBOL  lottery         = W5


' -----[ Initialization ]--------------------------------------------------

Power_Up:
  ' put code here that only happens at power-up/hard reset

Reset:
  PINS = %00000000                              ' all off
  DIRS = %00111111                              ' P5..P0 are outputs


' -----[ Program Code ]----------------------------------------------------

Main:
  BRANCH Trigger, (Ping_Pong, Scrambler)        ' jump based on trigger


Ping_Pong:
  IF last = 0 THEN PP_Next                      ' if still pp, skip
    idx = 0                                     ' reset to start of cycle

PP_Next:
  READ idx, PINS                                ' copy pattern to pins
  idx = idx + 1 // 10                           ' update, wrap around
  PAUSE 100                                     ' sequence timing delay
  last = 0                                      ' mark running pp sequence
  GOTO Main                                     ' re-test trigger


Scrambler:
  FOR idx = 1 TO 3                              ' big scramble
    RANDOM lottery
  NEXT
  PINS = lottery                                ' random value to pins
  PAUSE 100
  last = 1                                      ' mark running scrambler
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -----[ User Data ]-------------------------------------------------------

Ping_Leds:
  EEPROM (%00000001, %00000010, %00000100, %00001000, %00010000)

Pong_Leds:
  EEPROM (%00100000, %00010000, %00001000, %00000100, %00000010)
Jon McPhalen
EFX-TEK Hollywood Office

Jeff Haas

Jon, thanks!  I really appreciate your willingness to knock out a program on a Sunday.

FYI, I've spent some time reading up on the BRANCH command, and I think I found a typo in the Basic Stamp help.  If you look up BRANCH in the Basic Stamp Help file, the example at the bottom for the BS2 uses parenthesis:

BRANCH value, (Case_0, Case_1, Case_2)

Shouldn't it use brackets, such as:
BRANCH value, [Case_0, Case_1, Case_2]

Or am I misreading something?

Jeff Haas

One more question...

I set up a Prop-2 to signal a Prop-1 (using above code and a Trainer on the Prop-1) and connected them with a servo cable.  I noticed that the Prop-2 powers the Prop-1 via the servo cable.  If I only connect the W and R pins then the Prop-1 barely works, but if you turn its power on it's fully functional.

I didn't expect this, it it OK?  Is there a configuration option to keep it from happening?

Very simple code to trigger the Prop-1 from the Prop-2:

' =========================================================================
'
'   File...... BRANCH Test Prop-2
'   Purpose... Test BRANCH control of Prop-1
'   Author.... Jeff Haas
'   E-mail....
'   Started...
'   Updated... 7/9/2012
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
' This is running on a Prop-2.  Prop-1 has the Trainer on it and is running BRANCH LEDs.bs1.
' Controllers are connected with a servo cable from Prop-2 P15 to Prop-1 P6 (on Trainer).
'

' -----[ I/O Definitions ]-------------------------------------------------

CLICK        PIN     0     ' Button is connected to P0, W & R pins on Prop-2

' -----[ Constants ]-------------------------------------------------------

IsOn            CON     1
IsOff           CON     0

' -----[ Variables ]-------------------------------------------------------


' -----[ Initialization ]--------------------------------------------------


' -----[ Program Code ]----------------------------------------------------

Main:
  LOW 15

  IF (CLICK = IsOff) THEN Main         'Wait for pushbutton activity
  DEBUG CR, "Button pressed"

  HIGH 15
  DEBUG CR, "ACTIVATE Prop-1"          'Pin 15 goes HIGH, signals Prop-1
  PAUSE 5000
  DEBUG CR, "DONE"
  DEBUG CR, " "

  GOTO Main                                     ' start again

bsnut

Jeff,

If you read the Basic Stamp help file you will notice that they are two BRANCH command formatting one is for the Basic Stamp 1 and one the Basic Stamp 2 as shown below.

This line of code is for Basic Stamp 1 (ie; Prop-1) only.
BRANCH value, (Case_0, Case_1, Case_2)

Since the Basic Stamp 1 is the only processor that uses parenthesis in its code.

This line of code is for Basic Stamp 2 (ie; Prop-2)
BRANCH value, [Case_0, Case_1, Case_2]

The Basic Stamp 2 and other processors like the BS2SX use only brackets in the code. That's why you see what you are thinking there is typo in Jon's coding example.
William Stefan
The Basic Stamp Nut

bsnut

Quote from: Jeff Haas on July 09, 2012, 02:56:42 AM
I set up a Prop-2 to signal a Prop-1 (using above code and a Trainer on the Prop-1) and connected them with a servo cable.  I noticed that the Prop-2 powers the Prop-1 via the servo cable.  If I only connect the W and R pins then the Prop-1 barely works, but if you turn its power on it's fully functional.
When you connected the servo cable to Prop-1 like you did, you powered up and charged the two capacitors that is on the 5VDC side of the voltage regulator which it allow it to barely work.

I have this question for you. Did you connect a power supply to the Prop-1 when you connected the W and R pins then the Prop-1? If you did this, that would be the reason why the Prop-1 worked correctly when the switch was turned on.
QuoteI didn't expect this, it it OK?  Is there a configuration option to keep it from happening?
From what it sounds like you didn't have any blue magic smoke from the Prop-1 I don't see a problem, but Jon can better answer this for you.

Now, when you connect the Prop-1 (if that's what you plan on) to the Prop-2, I would use a relay connected to one of OUTx to isolate the two boards from each other. This way you won't have any problems and you can use whatever voltage for the chasing lights and the Prop-2. This way you keep the blue magic smoke inside were it belongs. 
William Stefan
The Basic Stamp Nut

JonnyMac

Jeff,

As William pointed out, the syntax for my demo program was for the Prop-1. It uses () in places where the Prop-2 uses [].

What I should have pointed out is that you only want to connect the Px.B (black) and Px.W (white) wires on a TTL header. The reason is that both have their own 5v supplies (red) and you don't want to tie them together.  What I do is snip the red wire on both ends of an extender and mark the cable so I don't use it elsewhere.

Recap: For this strategy you need to have the controller grounds (black) connected, and the signal wire (white) connected between the output pin of the master and an input pin of the slave.
Jon McPhalen
EFX-TEK Hollywood Office

Jeff Haas

bsnut,

You misread my comment - I didn't say that I found a typo in Jon's code (it works on the Prop-1) but in the Basic Stamp Help File itself.  Look at this picture of the BRANCH command in the Help File:



Jon, thanks for the clarification. 

JonnyMac

Indeed you have. I will send a not to my friends at Parallax.
Jon McPhalen
EFX-TEK Hollywood Office

bsnut

Jeff,

Thanks for sharing what the Basic Stamp Editor help file is showing for the BRANCH command and I stand corrected :o. You must have an older version of the Basic Stamp Editor help file, because mine isn't showing this typo.
William Stefan
The Basic Stamp Nut

JonnyMac

It is in fact in the latest help file (which is a few years old because I created the original HTML back in 2001 while working for Parallax). 
Jon McPhalen
EFX-TEK Hollywood Office