Shop OBEX P1 Docs P2 Docs Learn Events
my code can't work ,need help in it ! — Parallax Forums

my code can't work ,need help in it !

mogu93mogu93 Posts: 22
edited 2013-10-21 23:19 in BASIC Stamp
Hi there ,I doing a weight sensor that read voltage in using basic stamp and adc0831 and at certain voltage ,above 1.45v, taking the average voltage reading ,a announcement will be made using ic speakjet(text to speech) . using if else loop ,but I try it out but it does not work . is my code wrong ?plus I need add in more condition for more audio announcement . anybody can help me with the code ? thanks!:)
' {$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 ' millivolts

'
[ 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

'announcement
DO
IF mVolts => 145* 10^-2 THEN
SEROUT 8,$0054,[145,8,131,8,8,8,185,8,129,8,8,14,7,186,130,7,128,8,8,14,191,8,14,198,8,8,8,14,187,8,6,141] 'left with 80% announcement

ELSE
GOTO MAIN
ENDIF

PAUSE 100
LOOP

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2013-10-21 21:13
    It would be really helpful if you describe in more detail how it does not work. What part works as you expect, what doesn't and what happens differently than what is supposed to. Often times when writing out these details you discover the error.

    Including a picture of your hardware can also be useful in determining that things are wired up correctly.

    Also, it is helpful to put your code inside code tags - [noparse]
    your code goes here
    
    [/noparse]
    [COLOR=#333333]' {$STAMP BS2}[/COLOR]
    [COLOR=#333333]' {$PBASIC 2.5}[/COLOR]
    [COLOR=#333333]' -----[ Program Description ]---------------------------------------------[/COLOR]
    [COLOR=#333333]'[/COLOR]
    [COLOR=#333333]' This program demonstrates reading a variable voltage with an ADC0831[/COLOR]
    [COLOR=#333333]' analog-to-digital converter chip. This program uses a Vref input of[/COLOR]
    [COLOR=#333333]' 5.000 volts (Vdd) for a bit resolution of 19.6 millivolts.[/COLOR]
    [COLOR=#333333]' -----[ I/O Definitions ]-------------------------------------------------[/COLOR]
    [COLOR=#333333]CS PIN 0 ' chip select (ADC0831.1)[/COLOR]
    [COLOR=#333333]Clock PIN 1 ' clock (ADC0831.7)[/COLOR]
    [COLOR=#333333]DataIn PIN 2 ' data (ADC0831.6)[/COLOR]
    [COLOR=#333333]' -----[ Constants ]-------------------------------------------------------[/COLOR]
    [COLOR=#333333]Cnts2Mv CON $139C ' x 19.6 (to millivolts)[/COLOR]
    [COLOR=#333333]' -----[ Variables ]-------------------------------------------------------[/COLOR]
    [COLOR=#333333]result VAR Byte ' result of conversion[/COLOR]
    [COLOR=#333333]mVolts VAR Word ' millivolts[/COLOR]
    [COLOR=#333333]mVolts1 VAR Word ' millivolts[/COLOR]
    [COLOR=#333333]mVolts2 VAR Word ' millivolts[/COLOR]
    [COLOR=#333333]mVolts3 VAR Word ' millivolts[/COLOR]
    [COLOR=#333333]mVolts4 VAR Word ' millivolts[/COLOR]
    
    [COLOR=#333333]' -----[ Initialization ]--------------------------------------------------[/COLOR]
    [COLOR=#333333]Reset:[/COLOR]
    [COLOR=#333333]DEBUG CLS, ' create report screen[/COLOR]
    [COLOR=#333333]"ADC.... ", CR,[/COLOR]
    [COLOR=#333333]"volts... "[/COLOR]
    [COLOR=#333333]' -----[ Program Code ]----------------------------------------------------[/COLOR]
    [COLOR=#333333]Main:[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+------------+---------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------¦ Initialize ¦---------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+------------+---------------------------[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]' Read the first 3 consecutive ADC values here[/COLOR]
    [COLOR=#333333]GOSUB Read_0831 ' read the ADC[/COLOR]
    [COLOR=#333333]mVolts1 = result */ Cnts2Mv ' convert to millivolts[/COLOR]
    [COLOR=#333333]DEBUG HOME, ' report[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 0, DEC result, CLREOL,[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 1, DEC mVolts1 DIG 3,[/COLOR]
    [COLOR=#333333]".", DEC3 mVolts1[/COLOR]
    [COLOR=#333333]PAUSE 1000[/COLOR]
    [COLOR=#333333]GOSUB Read_0831 ' read the ADC[/COLOR]
    [COLOR=#333333]mVolts2 = result */ Cnts2Mv ' convert to millivolts[/COLOR]
    [COLOR=#333333]DEBUG HOME, ' report[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 0, DEC result, CLREOL,[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 1, DEC mVolts2 DIG 3,[/COLOR]
    [COLOR=#333333]".", DEC3 mVolts2[/COLOR]
    [COLOR=#333333]PAUSE 1000[/COLOR]
    [COLOR=#333333]GOSUB Read_0831 ' read the ADC[/COLOR]
    [COLOR=#333333]mVolts3 = result */ Cnts2Mv ' convert to millivolts[/COLOR]
    [COLOR=#333333]DEBUG HOME, ' report[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 0, DEC result, CLREOL,[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 1, DEC mVolts3 DIG 3,[/COLOR]
    [COLOR=#333333]".", DEC3 mVolts3[/COLOR]
    [COLOR=#333333]PAUSE 1000[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+-----------+----------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------¦ Main Loop ¦----------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+-----------+----------------------------[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]'Perpetually read the 4th ADC value here in this loop[/COLOR]
    [COLOR=#333333]DO[/COLOR]
    [COLOR=#333333]GOSUB Read_0831 ' read the ADC[/COLOR]
    [COLOR=#333333]mVolts4 = result */ Cnts2Mv ' convert to millivolts[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+---------+------------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------¦ Average ¦------------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+---------+------------------------------[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]'Average the 4 ADC results together and display[/COLOR]
    [COLOR=#333333]mVolts = (mVolts1+mVolts2+mVolts3+mVolts4)/4' convert to millivolts[/COLOR]
    [COLOR=#333333]DEBUG HOME, ' report[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 0, DEC result, CLREOL,[/COLOR]
    [COLOR=#333333]CRSRXY, 9, 1, DEC mVolts DIG 3,[/COLOR]
    [COLOR=#333333]".", DEC3 mVolts[/COLOR]
    [COLOR=#333333]PAUSE 3000[/COLOR]
    [COLOR=#333333]'--------- ----------------------------------------------------------------[/COLOR]
    [COLOR=#333333]'------------------------+-------------------------------+----------------[/COLOR]
    [COLOR=#333333]'------------------------¦ Bump the oldest ADC value out ¦----------------[/COLOR]
    [COLOR=#333333]'------------------------+-------------------------------+----------------[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]mVolts1=mVolts2[/COLOR]
    [COLOR=#333333]mVolts2=mVolts3[/COLOR]
    [COLOR=#333333]mVolts3=mVolts4[/COLOR]
    [COLOR=#333333]LOOP[/COLOR]
    
    
    
    [COLOR=#333333]Read_0831:[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+-----------+----------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------¦ Read_0831 ¦----------------------------[/COLOR]
    [COLOR=#333333]'--------------------------------+-----------+----------------------------[/COLOR]
    [COLOR=#333333]'-------------------------------------------------------------------------[/COLOR]
    [COLOR=#333333]LOW CS ' enable ADC[/COLOR]
    [COLOR=#333333]SHIFTIN DataIn, Clock, MSBPOST, [result\9] ' read ADC[/COLOR]
    [COLOR=#333333]HIGH CS ' disable ADC[/COLOR]
    [COLOR=#333333]RETURN[/COLOR]
    
    [COLOR=#333333]'announcement[/COLOR]
    [COLOR=#333333]DO[/COLOR]
    [COLOR=#333333]IF mVolts => 145* 10^-2 THEN[/COLOR]
    [COLOR=#333333]SEROUT 8,$0054,[145,8,131,8,8,8,185,8,129,8,8,14,7,186,130,7,128,8 ,8,14,191,8,14,198,8,8,8,14,187,8,6,141] 'left with 80% announcement[/COLOR]
    
    [COLOR=#333333]ELSE[/COLOR]
    [COLOR=#333333]GOTO MAIN[/COLOR]
    [COLOR=#333333]ENDIF[/COLOR]
    
    [COLOR=#333333]PAUSE 100[/COLOR]
    [COLOR=#333333]LOOP[/COLOR]
    
  • mogu93mogu93 Posts: 22
    edited 2013-10-21 21:25
    I doing a weight sensor that read voltage in using basic stamp and adc0831 and at certain voltage ,above 1.45v, taking the average voltage reading ,a announcement will be made using ic speakjet(text to speech) . using "if else" loop ,but I try it out but it does not work . is my code wrong ?plus I need add in more condition for more audio announcement. the audio announcement part doesn't work when compile together with the adc voltage reading programming code. when the given condition when above 1.45v the audio announcement didn't sound out .(eg, Left with 80%), sorry I using school computer that does not allow to upload any picture :( -
    'announcement
    [COLOR=#333333]DO[/COLOR]
    [COLOR=#333333]IF mVolts => 145* 10^-2 THEN[/COLOR]
    [COLOR=#333333]SEROUT 8,$0054,[145,8,131,8,8,8,185,8,129,8,8,14,7,186,130,7,128,8 ,8,14,191,8,14,198,8,8,8,14,187,8,6,141] 'left with 80% announcement[/COLOR]
    
    [COLOR=#333333]ELSE[/COLOR]
    [COLOR=#333333]GOTO MAIN[/COLOR]
    [COLOR=#333333]ENDIF[/COLOR]
    
    [COLOR=#333333]PAUSE 100[/COLOR]
    [COLOR=#333333]LOOP
    
    and I also doesn't know where to add in this code in .[/COLOR]
  • W9GFOW9GFO Posts: 4,010
    edited 2013-10-21 21:35
    I did not realize that the Basic Stamp supported this kind of math;


    IF mVolts => 145* 10^-2
    

    Also, have you verified that the text to speech is working properly?
  • mogu93mogu93 Posts: 22
    edited 2013-10-21 21:38
    yeah it's working properly ,I wanted some decimal value in it but I don't how how to add it. there's some error while keying in decimal value,but I don't know how to correct it.
  • W9GFOW9GFO Posts: 4,010
    edited 2013-10-21 21:55
    The Basic Stamp does not support floating point, only integers.

    If you want the text to speech to speak the decimal value you will need it to speak the first digit, point, then the remaining digits.
  • mogu93mogu93 Posts: 22
    edited 2013-10-21 22:21
    thanks! but my code still can't work :frown: still need to figure it out .
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-21 23:19
    This seems like the same problem you asked about in this thread. It's against forum rule to make multiple posts about the same issue.

    You've been repeatedly asked to use code tags when posting code. Please do so. In the other thread on this topic, I provided a link to a tutorial on how to use code tags.
Sign In or Register to comment.