Shop OBEX P1 Docs P2 Docs Learn Events
Random number / Branch — Parallax Forums

Random number / Branch

ArchiverArchiver Posts: 46,084
edited 2002-12-02 14:49 in General Discussion
Hello all, I need some help.
I am quite new to the basic stamp world, I am writting a program to
select and activate two different pin base on a random number. I
have a list of different cases, such as case 1: will make pin 0 and
1 high for a certain amount of time, then make them low again, then
I generate another random number, say 3, case 3: makes pin 0 and 3
high for an amount of time then make them low and so on as you will
see in the attached program below. the main problem I have is that
when the numbers are generated, the selection that is supposed to be
made is not made, meaning the pin I want to be high are not
necessarily the ones that are high and also they stay high. I know
that their might be a better way to do this. any help will be
greatly appreciated. thanks in advance


'{$STAMP BS2} 'STAMP directive (specifies
a BS2)

'SOUT = serial out: 1 connects to pcserialport RX pin for prograing
'SIN = Serial in: 2 connects to pc serial port TX pin for programing
'ATTN Atteention: 3 connects to PC serial Port DTR pin for programing
'VSS: system groung: 4 connects to pc serial port GND for programing
'P0-P15General purpose I/O pins
'VDD = 5 volts DC input/output (21)
'RES = Reset input/ouput: goes low when power supply is less than
approxemately 4.2 volt,
'causing the basic stamp to reset
'VSS = System ground: connects to to power supply's ground (GND)
terminal
'VIN = Unregulated power in: axxepsts 6 - 60 VDC which is internally
regulated for 5 volts.



'INPUT 7 ' Make I/O pin 7 an
input.
'OUT7 = 0 ' Write 0 to output
latch.

Elect1 con 0 ' Make I/O pin 0 an
output.
Elect2 con 1 ' Make I/O pin 1 an
output.
Elect3 con 2 ' Make I/O pin 2 an
output.
Elect4 con 3 ' Make I/O pin 3 an
output.
Elect5 con 4 ' Make I/O pin 4 an
output.
Elect6 con 5 ' Make I/O pin 5 an
output.
Elect7 con 6 ' Make I/O pin 6 an
output.

'POLARITY con 7 ' Make I/O
pin 1 an output.


'OUTPUT 7 ' Make I/O pin 7 an
output.

RNDWORD VAR WORD
RND1 VAR WORD


'RANDOM NumGen

LOOP:
RANDOM rndword
RND1 = rndword/13555 MIN 1 'GEGNERATE A RAND # BETWEEN 1 AND 7,
1 AND 8 WITH 7555
'RND1 = RNDWORD.LOWnIB(0) * %0111
debug? RND1
BRANCH RND1, [noparse][[/noparse]Case1,Case2,Case3,Case4,Case5]
', Loop6, Loop7, Loop8]

PAUSE 10000
'goto loop

Case1:
HIGH 0
'PAUSE 10000
HIGH 1
PAUSE 10000
LOW 0
LOW1
GOTO LOOP

Case2:
HIGH 0
'PAUSE 10000
HIGH 2
PAUSE 10000
LOW 0
LOW 2
GOTO LOOP

Case3:
HIGH 0
'PAUSE 10000
HIGH 3
PAUSE 10000
LOW 0
LOW3
GOTO LOOP


Case4:
HIGH 0
'PAUSE 10000
HIGH 4
PAUSE 10000
low 0
low14
GOTO LOOP



Case5:
HIGH 0
'PAUSE 10000
HIGH 5
PAUSE 10000
low 0
low5
GOTO LOOP

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-12-02 14:49
    well the first thing I noticed was there is only one debug statement.
    Sine debugs are the only way to watch program flow and state in a
    stamp I expect to see a lot of them, you need to know how things are
    working. Comment them out once things work, but they should be there!

    Second I see a lot of duplicate code. dupe code is BAD. Your caseX
    statements are the culprits...

    You wrote a bunch of these
    > Case1:
    > HIGH 0
    > 'PAUSE 10000
    > HIGH 1
    > PAUSE 10000
    > LOW 0
    > LOW1
    > GOTO LOOP

    I'd write

    Case1:
    DEBUG "Case1": CR
    High 0
    High 1
    GOTO LOOP

    Then put this at the top of Loop...
    LOOP:
    DEBUG "LOOP", CR
    PAUSE 10000
    FOR out = 0 to 5
    Low out
    NEXT
    .
    .
    .
    Then you need to make sure rand1 is doing what you think it is doing,
    it's probably not...


    --- In basicstamps@y..., "konpen" <konpen@y...> wrote:
    > Hello all, I need some help.
    > I am quite new to the basic stamp world, I am writting a program to
    > select and activate two different pin base on a random number. I
    > have a list of different cases, such as case 1: will make pin 0 and
    > 1 high for a certain amount of time, then make them low again, then
    > I generate another random number, say 3, case 3: makes pin 0 and 3
    > high for an amount of time then make them low and so on as you will
    > see in the attached program below. the main problem I have is that
    > when the numbers are generated, the selection that is supposed to
    be
    > made is not made, meaning the pin I want to be high are not
    > necessarily the ones that are high and also they stay high. I know
    > that their might be a better way to do this. any help will be
    > greatly appreciated. thanks in advance
    >
    >
    > '{$STAMP BS2} 'STAMP directive
    (specifies
    > a BS2)
    >
    > 'SOUT = serial out: 1 connects to pcserialport RX pin for prograing
    > 'SIN = Serial in: 2 connects to pc serial port TX pin for programing
    > 'ATTN Atteention: 3 connects to PC serial Port DTR pin for
    programing
    > 'VSS: system groung: 4 connects to pc serial port GND for programing
    > 'P0-P15General purpose I/O pins
    > 'VDD = 5 volts DC input/output (21)
    > 'RES = Reset input/ouput: goes low when power supply is less than
    > approxemately 4.2 volt,
    > 'causing the basic stamp to reset
    > 'VSS = System ground: connects to to power supply's ground (GND)
    > terminal
    > 'VIN = Unregulated power in: axxepsts 6 - 60 VDC which is
    internally
    > regulated for 5 volts.
    >
    >
    >
    > 'INPUT 7 ' Make I/O pin 7 an
    > input.
    > 'OUT7 = 0 ' Write 0 to output
    > latch.
    >
    > Elect1 con 0 ' Make I/O
    pin 0 an
    > output.
    > Elect2 con 1 ' Make I/O
    pin 1 an
    > output.
    > Elect3 con 2 ' Make I/O
    pin 2 an
    > output.
    > Elect4 con 3 ' Make I/O
    pin 3 an
    > output.
    > Elect5 con 4 ' Make I/O
    pin 4 an
    > output.
    > Elect6 con 5 ' Make I/O
    pin 5 an
    > output.
    > Elect7 con 6 ' Make I/O
    pin 6 an
    > output.
    >
    > 'POLARITY con 7 '
    Make I/O
    > pin 1 an output.
    >
    >
    > 'OUTPUT 7 ' Make I/O pin 7 an
    > output.
    >
    > RNDWORD VAR WORD
    > RND1 VAR WORD
    >
    >
    > 'RANDOM NumGen
    >
    > LOOP:
    > RANDOM rndword
    > RND1 = rndword/13555 MIN 1 'GEGNERATE A RAND # BETWEEN 1 AND
    7,
    > 1 AND 8 WITH 7555
    > 'RND1 = RNDWORD.LOWnIB(0) * %0111
    > debug? RND1
    > BRANCH RND1, [noparse][[/noparse]Case1,Case2,Case3,Case4,Case5]
    > ', Loop6, Loop7, Loop8]
    >
    > PAUSE 10000
    > 'goto loop
    >
    > Case1:
    > HIGH 0
    > 'PAUSE 10000
    > HIGH 1
    > PAUSE 10000
    > LOW 0
    > LOW1
    > GOTO LOOP
    >
    > Case2:
    > HIGH 0
    > 'PAUSE 10000
    > HIGH 2
    > PAUSE 10000
    > LOW 0
    > LOW 2
    > GOTO LOOP
    >
    > Case3:
    > HIGH 0
    > 'PAUSE 10000
    > HIGH 3
    > PAUSE 10000
    > LOW 0
    > LOW3
    > GOTO LOOP
    >
    >
    > Case4:
    > HIGH 0
    > 'PAUSE 10000
    > HIGH 4
    > PAUSE 10000
    > low 0
    > low14
    > GOTO LOOP
    >
    >
    >
    > Case5:
    > HIGH 0
    > 'PAUSE 10000
    > HIGH 5
    > PAUSE 10000
    > low 0
    > low5
    > GOTO LOOP
Sign In or Register to comment.