my code can't work ,need help in it !
mogu93
Posts: 22
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
' {$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
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] [/noparse]
Also, have you verified that the text to speech is working properly?
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.
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.