Shop OBEX P1 Docs P2 Docs Learn Events
how to compile programming code together with condition ?:( — Parallax Forums

how to compile programming code together with condition ?:(

mogu93mogu93 Posts: 22
edited 2013-10-22 19:34 in BASIC Stamp
Hi there ,I need help in compiling all the programming code together with some condition as I'm using adc ,speakjet (text to speech ) and basic stamp. At certain voltages it will activate the speakjet to work for announcement purpose.,i will be using the final adc reading at the end to do it . so I need different cases for condition for different announcement .And below is my adc with basic stamp programming thank! :smile: ' {$STAMP BS2}
' {$PBASIC 2.5}
'
[ Program Description ]
'
' This program demonstrates reading a variable voltage with an ADC0831
' analog-to-digital converter chip. This program uses a Vref input of
' 5.000 volts (Vdd) for a bit resolution of 19.6 millivolts.
'
[ I/O Definitions ]
CS PIN 0 ' chip select (ADC0831.1)
Clock PIN 1 ' clock (ADC0831.7)
DataIn PIN 2 ' data (ADC0831.6)
'
[ Constants ]
Cnts2Mv CON $139C ' x 19.6 (to millivolts)
'
[ Variables ]
result VAR Byte ' result of conversion
mVolts VAR Word ' millivolts
mVolts1 VAR Word ' millivolts
mVolts2 VAR Word ' millivolts
mVolts3 VAR Word ' millivolts
mVolts4 VAR Word

'
[ Initialization ]
Reset:
DEBUG CLS, ' create report screen
"ADC.... ", CR,
"volts... "
'
[ Program Code ]
Main:
'
'
+
+
'
¦ Initialize ¦
'
+
+
'
' Read the first 3 consecutive ADC values here
GOSUB Read_0831 ' read the ADC
mVolts1 = result */ Cnts2Mv ' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts1 DIG 3,
".", DEC3 mVolts1
PAUSE 1000
GOSUB Read_0831 ' read the ADC
mVolts2 = result */ Cnts2Mv ' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts2 DIG 3,
".", DEC3 mVolts2
PAUSE 1000
GOSUB Read_0831 ' read the ADC
mVolts3 = result */ Cnts2Mv ' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts3 DIG 3,
".", DEC3 mVolts3
PAUSE 1000
'
'
+
+
'
¦ Main Loop ¦
'
+
+
'
'Perpetually read the 4th ADC value here in this loop
DO
GOSUB Read_0831 ' read the ADC
mVolts4 = result */ Cnts2Mv ' convert to millivolts
'
'
+
+
'
¦ Average ¦
'
+
+
'
'Average the 4 ADC results together and display
mVolts = (mVolts1+mVolts2+mVolts3+mVolts4)/4' convert to millivolts
DEBUG HOME, ' report
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts
PAUSE 3000
'

'
+
+
'
¦ Bump the oldest ADC value out ¦
'
+
+
'
mVolts1=mVolts2
mVolts2=mVolts3
mVolts3=mVolts4
LOOP

Read_0831:
'
'
+
+
'
¦ Read_0831 ¦
'
+
+
'
LOW CS ' enable ADC
SHIFTIN DataIn, Clock, MSBPOST, [result\9] ' read ADC
HIGH CS ' disable ADC
RETURN

Comments

  • FranklinFranklin Posts: 4,747
    edited 2013-10-20 21:42
    It would help if you would format your code between [code] tags to maintain formatting and explain what the program does and does not do and what you think the problem is.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-20 22:08
    Have you used the SpeakJet yet? Do you have the helper chip it needs? If you don't already have the helper chip, don't buy it. There's less expensive text to speech chip available. If you're interested, I'll find the link.

    There's a tutorial on posting code to the forum at the link below.

    attachment.php?attachmentid=78421&d=1297987572
  • mogu93mogu93 Posts: 22
    edited 2013-10-20 22:18
    yeah , I used the speakjet for message announcement .But I need to add into the adc programming.But I don't know how .Like some branch .At certain voltages , a announcement will be made.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-20 22:23
    mogu93 wrote: »
    yeah , I used the speakjet for message announcement .But I need to add into the adc programming.But I don't know how .Like some branch .At certain voltages , a announcement will be made.

    Take a look at the program "SELECT-CASE.BS2" in the sample programs that come with the BS editor. It looks like it has an example of doing what you want.
  • mogu93mogu93 Posts: 22
    edited 2013-10-20 22:25
    thanks! but I don't have that, u mind sending me the sample program to me? :)
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-20 22:35
    mogu93 wrote: »
    thanks! but I don't have that, u mind sending me the sample program to me? :)

    It was part of the software installation.

    Here it is:
    ' SELECT-CASE.BS2' This program generates a series of 16-bit random numbers and tests each
    ' to determine odd or even, and where it falls in the possible range:
    ' lower third, middle third, or upper third.  The program is useful for
    ' testing various seed values for RANDOM.
    
    
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    
    test            VAR     Byte            ' counter for tests
    sample          VAR     Word            ' random number to be tested
    odd             VAR     Byte            ' odd throws
    even            VAR     Byte            ' even throws
    isLo            VAR     Byte            ' sample in lower third
    isMid           VAR     Byte            '        in middle thrid
    isHi            VAR     Byte            '        in upper third
    
    
    
    
    Main:
      sample = 11000                        ' initialize seed
      FOR test = 1 TO 100                   ' "throw" 100 times
        RANDOM sample                       ' randomize
    
    
        IF (sample.BIT0) THEN               ' check odd/even bit
          odd = odd + 1                     ' increment odd count
        ELSE
          even = even + 1                   ' increment even count
        ENDIF
    
    
        SELECT sample
          CASE <= 21845                     ' test lower third
            isLo = isLo + 1
    
    
          CASE 21846 TO 43691               ' test middle third
            isMid = isMid + 1
    
    
          CASE ELSE                         ' otherwise upper third
            isHi = isHi + 1
        ENDSELECT
      NEXT
    
    
    Show_Results:
      DEBUG CLS,
            "Odd Throws.... ", DEC odd, "%", CR,
            "Even Throws... ", DEC even, "%", CR,
            "Low........... ", DEC isLo, "%", CR,
            "Mid........... ", DEC isMid, "%", CR,
            "High.......... ", DEC isHi, "%", CR
      END
    

    Doesn't it look pretty with code tags?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-20 22:38
    This is the part I think applies to your application.
    SELECT sample
          CASE <= 21845                     ' test lower third
            isLo = isLo + 1
    
    
          CASE 21846 TO 43691               ' test middle third
            isMid = isMid + 1
    
    
          CASE ELSE                         ' otherwise upper third
            isHi = isHi + 1
        ENDSELECT
    

    Instead of "sample" you'd use your voltage reading. You'd then make each case a range of readings with a message to display for each case.
  • mogu93mogu93 Posts: 22
    edited 2013-10-20 22:42
    got any more simpler code? hehe! of condition ,and cases code?i got 5 value and messages ,need 5 cases for it to implement
  • FranklinFranklin Posts: 4,747
    edited 2013-10-22 19:34
    Taking what Duane gave you and posting with code tags show us what you have now. To add more conditions just make more CASE statements...
Sign In or Register to comment.