RANDOM problem - Need help with code
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!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
·
·
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
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):
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
I tried as word and byte, but did not try nib. I'll try this code.
Thanks again
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
·
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 -->
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
·
See if that works.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I'm a little slow on the draw-- grat minds thinking alike and all that.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The modulo operator, now that's real inside baseball.
Post Edited (PJ Allen) : 9/17/2006 4:28:56 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
·
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
Jeff T.
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
·
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.
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.
Thanks again.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Mike
·