Shop OBEX P1 Docs P2 Docs Learn Events
RANDOM problem - Need help with code — Parallax Forums

RANDOM problem - Need help with code

SN96SN96 Posts: 318
edited 2006-09-22 22:36 in BASIC Stamp
Hello,
·
I have been trying to do a simple program using the RANDOM command. Here is my problem:
·
When I try to run the program the value of "phrase" has several non-number characters. I can't figure out how to make the phrase variable to be a pure number.
·
What I am trying to do is have 9 phrases for the speakjet to say. I want to randomly cycle through 9 possible phrases. In the example code it keeps picking the same phrase over and over.
·
Look under the section called "Main program" for the "random" code
Look at the very bottom under "SpeakJet Phrases" I am trying to use those labels in the branch line of code.
·
Any help would be greatly appreciated!

' =========================================================================
'   -----------------------Botwire Technologies----------------------------
'
'   File.......... ADC08x32.BS2
'   Description... RSCU
'   Author........ Mike Bookbinder
'   E-mail........ [url=mailto:botwire@yahoo.com]botwire@yahoo.com[/url]
'   Web........... [url=http://www.geocities.com/botwire]www.geocities.com/botwire[/url]
'   Updated....... 1-14-06
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' -----[noparse][[/noparse] Program Description ]---------------------------------------------
'
' This program is modified code from the Parallax ADC0832.bs2 program example.
' I have modified this code to work with the Xtreme 3 Auidio Sensor Board.
' For the original code example, please visit [url=http://www.parallax.com/]www.parallax.com[/url] for more
' information.
' -----[noparse][[/noparse] Revision History ]------------------------------------------------
' PA0             CON     0                       ' pauses
PA1             CON     1
PA2             CON     2
PA3             CON     3
PA4             CON     4
PA5             CON     5
PA6             CON     6
Fast            CON     7
Slow            CON     8
Stress          CON     14
Relax           CON     15
_Wait           CON     16                      ' note underscore
Soft            CON     18
Volume          CON     20
Speed           CON     21
Pitch           CON     22
Bend            CON     23
PortCtr         CON     24
Port            CON     25
Repeat          CON     26
CallPhr         CON     28
GotoPhr         CON     29
Delay           CON     30
Reset           CON     31
IY              CON     128
IH              CON     129
EY              CON     130
EH              CON     131
AY              CON     132
AX              CON     133
UX              CON     134
OH              CON     135
AW              CON     136
OW              CON     137
UH              CON     138
UW              CON     139
MM              CON     140
NE              CON     141
NO              CON     142
NGE             CON     143
NGO             CON     144
LE              CON     145
LO              CON     146
WW              CON     147
RR              CON     148
IYRR            CON     149
EYRR            CON     150
AXRR            CON     151
AWRR            CON     152
OWRR            CON     153
EYIY            CON     154
OHIY            CON     155
OWIY            CON     156
OHIH            CON     157
IYEH            CON     158
EHLL            CON     159
IYUW            CON     160
AXUW            CON     161
IHWW            CON     162
AYWW            CON     163
OWWW            CON     164
JH              CON     165
VV              CON     166
ZZ              CON     167
ZH              CON     168
DH              CON     169
BE              CON     170
BO              CON     171
EB              CON     172
OB              CON     173
DE              CON     174
_DO             CON     175                     ' note underscore
ED              CON     176
OD              CON     177
GE              CON     178
GO              CON     179
EG              CON     180
OG              CON     181
CH              CON     182
HE              CON     183
HO              CON     184
WH              CON     185
FF              CON     186
SE              CON     187
SO              CON     188
SH              CON     189
TH              CON     190
TT              CON     191
TU              CON     192
TS              CON     193
KE              CON     194
KO              CON     195
EK              CON     196
OK              CON     197
PE              CON     198
PO              CON     199
RO              CON     200
R1              CON     201
R2              CON     202
R3              CON     203
R4              CON     204
R5              CON     205
R6              CON     206
R7              CON     207
R8              CON     208
R9              CON     209
A0              CON     210
A1              CON     211
A2              CON     212
A3              CON     213
A4              CON     214
A5              CON     215
A6              CON     216
A7              CON     217
A8              CON     218
A9              CON     219
_B0             CON     220                     ' note underscore
_B1             CON     221
_B2             CON     222
_B3             CON     223
_B4             CON     224
_B5             CON     225
_B6             CON     226
_B7             CON     227
_B8             CON     228
_B9             CON     229
C0              CON     230
C1              CON     231
C2              CON     232
C3              CON     233
C4              CON     234
C5              CON     235
C6              CON     236
C7              CON     237
C8              CON     238
C9              CON     239
D0              CON     240                     ' DTMF tones
D1              CON     241
D2              CON     242
D3              CON     243
D4              CON     244
D5              CON     245
D6              CON     246
D7              CON     247
D8              CON     248
D9              CON     249
D10             CON     250
D11             CON     251
M0              CON     252                     ' sonar pin
M1              CON     253                     ' pistol shot
M2              CON     254                     ' WOW
EOS             CON     255
TX              PIN     9
RDY             PIN     10
 
'------------------------------[noparse][[/noparse] SpeakJet Setup ]-------------------------------------
PAUSE 3000
SEROUT 9,$0054,[noparse][[/noparse]"\0RX"] 'CLEAR BUFFER
SEROUT TX\RDY,$0054,[noparse][[/noparse]020,127]' set volume to max
SEROUT 9,$0054,[noparse][[/noparse]PITCH,85,BEND,4]
' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
CS              PIN     15                      ' ADC0832.1
Clk             PIN     14                      ' ADC0832.7
Dio             PIN     13                      ' ADC0832.5 / ADC0832.6
 
' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
Dif             CON     %0                      ' differential
Raw2mV          CON     $139B                   ' 19.6 mV per count

' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
sglDif          VAR     Bit                     ' adc mode (1 = SE)
oddSign         VAR     Bit                     ' chan (Sgl), sign (Dif)
adc             VAR     Byte(2)                 ' channel values
mVolts          VAR     Word(2)                 ' millivolts
RightEar        VAR     Word                    ' Right Electret Mic
LeftEar         VAR     Word                    ' Left Electret Mic
counter         VAR     Word                    ' Loop Counter
' -----[noparse][[/noparse] EEPROM Data ]-----------------------------------------------------

' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
'Reset:
  HIGH CS                                       ' deselect ADC
  DEBUG CLS,                                    ' Print display
        "ADC08x32",
        CRSRXY, 0, 7, "Differential", CR, CR,
        "  Ch0:", CR,
        "  Ch1:"

' -----[noparse][[/noparse] Main Program ]----------------------------------------------------
phrase VAR Word
phrase = 8
Main:
  DO
  RANDOM phrase
  DEBUG phrase
  BRANCH phrase, [noparse][[/noparse]ph1,ph2,ph3,ph4,ph5,ph6,ph7,ph8,ph9]
 
    sglDif = Dif
    FOR oddSign = 0 TO 1
      GOSUB Read_0832
      mVolts(oddSign) = adc(oddSign) */ Raw2mV
      DEBUG CRSRXY, 7, (9 + oddSign),
        DEC3 adc(oddSign), TAB,
        DEC mVolts(oddSign) DIG 3, ".", DEC3 mVolts(oddSign)
         LeftEar = adc(0)  'Get value from Channel 0
         RightEar = adc(1)  'Get value from channel 1
      DEBUG HOME, CR, DEC3 RightEar 'Print value of CH1
      DEBUG CR, DEC3 LeftEar        'Print value of CH2
        IF RightEar > 20 THEN          'Threshold 5. If > then go to RightEarSub
          GOSUB RightEarSub
          DEBUG HOME, "RT Ear"
                ELSEIF LeftEar > 10 THEN   'Else if this is > the set threshold, go to LeftEarSub
                     GOSUB LeftEarSub
                       DEBUG HOME, "LT Ear"
                       ELSE
                       DEBUG HOME, "                 "

        ENDIF
    NEXT
  LOOP
  END

' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
' Reads ADC08x32
Read_0832:                                      ' Read ADC Chip
  LOW CS
  ' send start, mode, channel
  SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%1\1, sglDif\1, oddSign\1]
  ' read raw counts from ADC
  SHIFTIN  Dio, Clk, MSBPOST, [noparse][[/noparse]adc(oddSign)\8]
  HIGH CS
  RETURN
LeftEarSub:
LeftEar = 0
SEROUT TX\RDY, $0054,[noparse][[/noparse]PA1,LE,EY,FF,FF,TU,PA6,SE,OHIH,DE,PA2]
FOR counter = 1 TO 150 STEP 5
  PULSOUT 5,1000
  PAUSE 20
NEXT
PAUSE 1000
FOR counter = 1 TO 150 STEP 5
  PULSOUT 5,728
  PAUSE 20
NEXT
FOR counter = 1 TO 50
GOSUB Read_0832
NEXT

RETURN

RightEarSub:
RightEar = 0                                 ' Move servo right
SEROUT TX\RDY, $0054, [noparse][[/noparse]PA1,RR,RR,OHIH,TU,PA6,SE,OHIH,DE,PA2]
'Hello
'SEROUT TX\RDY, $0054,[noparse][[/noparse]HE,EH,LE,OWWW,PA2]
FOR counter = 1 TO 150 STEP 5
  PULSOUT 5,500
  PAUSE 20
NEXT
PAUSE 1000
FOR counter = 1 TO 150 STEP 5
  PULSOUT 5,728
  PAUSE 20
NEXT
FOR counter = 1 TO 50
GOSUB Read_0832
NEXT
RETURN
'--------------------------------[noparse][[/noparse] SpeakJet Phrases ]-----------------------------------

ph1:
'My name is Vecter
SEROUT TX\RDY, $0054,[noparse][[/noparse]MM,OHIH,PA1,NE,EYIY,MM,PA2,IH,IH,SE,SE,PA2,VV,VV,EH,KO,TT,RR,RR,PA2]
RETURN
ph2:
'VERSION 1.0
SEROUT TX\RDY, $0054,[noparse][[/noparse]VV,AXRR,SH,SH,EY,NE,PA5,WW,WW,UX,NE,PO,OWIY,NE,TT,TT,PA5,OWWW,PA2,PA2,PA2,PA2]
RETURN
ph3:
'Object Detected
SEROUT TX\RDY, $0054,[noparse][[/noparse]OH,BE,JH,EY,KO,TT,PA2,DE,IY,TT,EH,EK,TT,EH,ED,PA2]
RETURN
ph4:
'Completed
SEROUT TX\RDY, $0054,[noparse][[/noparse]KO,MM,PO,LE,IY,TT,EH,ED,PA2]
RETURN
ph5:
'Hello
SEROUT TX\RDY, $0054,[noparse][[/noparse]HE,EH,LE,OWWW,PA2]
RETURN
ph6:
'BATTERY LOW
SEROUT TX\RDY, $0054,[noparse][[/noparse]OB,AY,TT,AXRR,IY,PA2,LE,LE,LE,OW,OW,OWWW,PA2]
RETURN
ph7:
'Initializing
SEROUT TX\RDY, $0054,[noparse][[/noparse]FAST,IH,NE,IH,SH,SH,UX,LE,OHIH,ZZ,ZZ,NGE,PA2]
ph8:
'LEFT SIDE
SEROUT TX\RDY, $0054,[noparse][[/noparse]PA1,LE,EY,FF,FF,TU,PA6,SE,OHIH,DE,PA2]
RETURN
ph9:
'RIGHT SIDE
SEROUT TX\RDY, $0054, [noparse][[/noparse]PA1,RR,RR,OHIH,TU,PA6,SE,OHIH,DE,PA2]
RETURN
ph10:
SEROUT TX\RDY, $0054, [noparse][[/noparse]HO,OH,SE,TT,OH,LE,UX,VV,IY,IY,SE,TT,OH,PA2,BE,EYIY,OB,IY,IY,PA2]
RETURN
ph11:
'Status OK
SEROUT TX\RDY, $0054, [noparse][[/noparse]SE,SE,TT,AY,TT,UX,SO,SO,PA2,OW,KE,EYIY]
RETURN
ph12:
'Sonar Ready
SEROUT TX\RDY, $0054, [noparse][[/noparse]SE,OW,NE,AWRR,PA2,RR,EY,DE,IY,PA2]
RETURN
ph13:
'Roaming Mode
SEROUT TX\RDY, $0054, [noparse][[/noparse]RR,OWWW,MM,MM,NGE,PA2,MM,OWWW,ED,PA2]
RETURN
ph14:
'RSCU
SEROUT TX\RDY, $0054, [noparse][[/noparse]AWRR,EH,SE,SE,PA1,SE,IY,IY,PA1,IY,UW,PA1,OB,OWWW,RR,ED,PA2]
RETURN
ph15:
'Processing Data
SEROUT TX\RDY, $0054, [noparse][[/noparse]FAST,PE,RR,OH,SO,SE,EH,SO,NGE,NGE,PA2,_DO,EYIY,DE,UX,pa2]
RETURN
END



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike



·

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-09-17 13:56
    You don't have as many phrases (8)·as WORD has numbers (65,536).
    You need to pare down your results from a WORD to something considerably smaller.

    You could truncate everything greater than the first three bits.· Can RANDOM use Nib instead of Word?· Then phrase could get truncated (mask out the highest bit of the Nib):

    phrase VAR Nib
    phrase = 4
    Main:
      DO
      RANDOM phrase
      [color=red]phrase = phrase & 7       'Bit mask so phrase = 0000 - 0111[/color]
      DEBUG phrase
      BRANCH phrase, [noparse][[/noparse]ph1,ph2,ph3,ph4,ph5,ph6,ph7,ph8,ph9]
    
    


    And with Nib you won't gobble up·as much RAM as with Word.

    Post Edited (PJ Allen) : 9/17/2006 2:45:08 PM GMT
  • SN96SN96 Posts: 318
    edited 2006-09-17 14:34
    Thanks!

    I tried as word and byte, but did not try nib. I'll try this code.

    Thanks again

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike



    ·
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-09-17 14:43
    OK -- but, still, Nib will bounce back a number from 0 - 15 (0000 - 1111).· You have 9 phrases and if RANDOM comes back with "11", you have nowhere to go (??)· So, you either have to mask out the high bit (truncate) and go with 8 phrases... or tell it which phrases to BRANCH to if RAMDOM comes up with 9 - 15 (1001 - 1111).
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-09-17 15:36
    PJ -

    I have to step in here and disagree. RANDOM is BOUNDED by the VALUE of PHRASE, not the size of the variable. Random "expects" (but does not demand) a WORD variable; thus any sized variable should be acceptable.

    Beyond that, BRANCH does do a bounds check (unlike some other facets of PBASIC). If the value of the target field is out of bounds, you immediately drop through to the next sequential statement.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • SN96SN96 Posts: 318
    edited 2006-09-17 15:43
    The code still does not work. For some reason I can't get a good random value in the range 0-8. All the code keeps doing in branching to the same phrase. If I manually type in branch values 0-8, it branches to each phrase with no problem. For some reason the value generated by RANDOM is garbage values. I don't know why that is.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike



    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-17 16:09
    Please reread the description of RANDOM in the PBasic manual. It takes the current value of a word variable and scrambles it in a pseudo-random way to get another word value. To get a value in a range other than 0-65535, you have to do some arithmetic on the value. I suggest using the modulo operator like this:
    myRandom VAR word
    myRandom = 999 ' This can be any value.  It "seeds" the pseudo-random sequence
    DO
      RANDOM myRandom
      BRANCH myRandom//9,[noparse][[/noparse]ph1,ph2,ph3,ph4,ph5,ph6,ph7,ph8,ph9]
      ' other statements go here
    LOOP
    
    
  • LarryLarry Posts: 212
    edited 2006-09-17 16:17
    Go back to your original code and replace the BRANCH statement with the following code:

    BRANCH phrase // 9, [noparse][[/noparse]ph1,ph2,ph3,ph4,ph5,ph6,ph7,ph8,ph9]
    
    



    See if that works.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • LarryLarry Posts: 212
    edited 2006-09-17 16:19
    woops,

    I'm a little slow on the draw-- grat minds thinking alike and all that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-09-17 16:25
    Does phrase VAR Nib not narrow the pool (from Word's $0000·- $FFFF down to Nib's $0·- $F)?· The following line,·phrase = x, establishes a seed value; it's not a limiter.
    The modulo operator, now that's real inside baseball.

    Post Edited (PJ Allen) : 9/17/2006 4:28:56 PM GMT
  • SN96SN96 Posts: 318
    edited 2006-09-17 17:11
    None of these approaches seem to work. I still get only one branch value

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike



    ·
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-09-17 17:21
    Hey, Mike, you're using a DEBUG phrase line -- what values are bouncing back?

    I'm not a BRANCH expert, I'd go with ON... GOSUB

    DO· RANDOM phrase
    · DEBUG phrase
    · ON phrase GOSUB ph1, ph2 ph3, ph4, ph5, ph6, ph7, ph8, ph9
    · PAUSE 500
    · IF A < 0 THEN Main
    · IF A > 8 THEN Main

    Post Edited (PJ Allen) : 9/17/2006 5:31:27 PM GMT
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-09-17 17:59
    Mike, the "phrase=8" sets the seed for the Pbasic Random command, as long as phrase=8 the "random" numbers generated will always be the same sequence of numbers. The Pbasic help file shows the use of the BUTTON command to "vary" the seed. You could·declare a ·VAR word for phrase to increase the possible values of the seed then mask or mod·phrase after the RANDOM statement·to a usable number for your BRANCH statement.

    Jeff T.
  • SN96SN96 Posts: 318
    edited 2006-09-21 20:09
    I'm still having problems with the random code. It does not branch to any of the random numbers generated except only one number (9). Can some one try and see if they can get a simple random number to branch to say 3 subroutines?

    In each subroutine have the debug print "sub1", "sub2" , "sub3" as a test. If you can get that to work, then I can try it to see if it will work.

    If I manually type in the value for phrase, it will branch to each indexed sub routine, based on the value manually given.

    confused.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike



    ·
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-21 20:25
    I just noticed that each of your phrase routines ends with a RETURN. There's nothing for them to return to! You have to pair a GOSUB with a RETURN.
    Put a label after the branch statement. Call it "keepGoing:". Replace the RETURNs after your phrase routines with a "goto keepGoing". Things should work better.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-09-22 18:03
    As Mike Green observed you dont use a RETURN statement to go back to the BRANCH you should as he suggests us a label instead. Here is a simple piece of code to play with , maybe it will work in your app. I have used pin 15 as a switch or button for each time you want a random number.

    idx VAR Word
    INPUT 15

    main:
    DO WHILE IN15=0·· '......wait for pin 15 to go high
    idx =idx+1······· ·'..........generate a seed for the random command
    IF idx > 65534 THEN·· '..make sure we dont go out of bounds
    idx=0

    ENDIF
    LOOP
    RANDOM idx··· ·'...........generate a random number between 0 and 65535
    idx=idx // 10 +1···· ·'.....MOD idx to get a number between 1 and 10
    DEBUG DEC idx,CR·· '.....display
    PAUSE 200··· ·'............allow time to remove the input from pin 15
    GOTO main··· ·'............repeat

    Jeff T.
  • SN96SN96 Posts: 318
    edited 2006-09-22 22:36
    Thanks for all the help guys, that worked. I didn't realize the RETURN misuse. I learned something today!

    Thanks again.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Mike



    ·
Sign In or Register to comment.