Comparison on basic stamp 2
Please am designing a solar tracking system that reads voltage digital signals and based on the magnitude of the voltage , it will rotate to the highest voltage incoporating a motor. Am having problem·carrying out·the action based on the·comparison result, I used the If and Then statement to compare the voltage differences and also check if the result·is greater than a certain constant value.
for example
find the voltage difference·voltage1 and voltage 2
if the difference is greater than the· constant , then make a move
if not then go and do another check:· voltage 2 and voltage1
if the difference is greater than a certain constant, then make a move
elseif·both are less than the·difference then go back to the start of the code.
This is the code...............................
'
[noparse][[/noparse] Variables ]
voltage1· var· word
voltage2·· var·· word
'
' Main Routine
'
Main:
····· 'PAUSE 50······································· 'adjust this delay so the time through the Main'loop is approximately 1 second
······answer10 = (mVolts1 - mVolts0)··<
THE PROBLEM IS·HERE , IF answer10 < 100 (·i.e ANSWER10 =·NEGATIVE ANSWER), IT STILL PERFORM THE SAME·ACTION(·MOVE 1 TO 0)··OF WHEN·answer10 >100 BUT I WANT IT TO GO TO THE NEXT CHECK answer01 AND·MOVE 0 TO 1.
······· 'DEBUG CR, CR, " Answer10:", SDEC answer10
······· PAUSE 100
······· IF ABS(mVolts1 - mVolts0) > 100 THEN····················· 'l
········· GOTO Move1:
······· ELSE
········· GOTO Sun_check0greater1
······· ENDIF
LOOP
'
[noparse][[/noparse] Conditions ]
Sun_check0greater1:
··················· answer01 = (mVolts0 - mVolts1)······························
··················· DEBUG CR, CR, " Answer01:", SDEC answer01
··················· PAUSE 100
··················· IF ABS(mVolts0 - mVolts1) > 100 THEN
····················· GOTO Move2:
··················· ELSE
······················GOTO Main
··················· ENDIF
·Move1[noparse]:D[/noparse]EBUG CR,CR, "Look to 0- 180 degrees (RIGHT).", CR
·············· PAUSE 100
·············· FOR counter = 1 TO 2····················· ' CLOCKWISE MOVEMENT (right)
·············· PULSOUT 15, LimitRight
·············· PAUSE 270
·············· NEXT
·············· GOTO Sun_check3greater2
Move2[noparse]:D[/noparse]EBUG CR,CR, "Look from 180- 0 degrees (LEFT).", CR
············· PAUSE 100
············· FOR counter = 1 TO 2······················ ' COUNTERCLOCKWISE MOVEMENT (left)
············· PULSOUT 15, LimitLeft
············· PAUSE 270
············· NEXT
············· GOTO Sun_check2greater3
for example
find the voltage difference·voltage1 and voltage 2
if the difference is greater than the· constant , then make a move
if not then go and do another check:· voltage 2 and voltage1
if the difference is greater than a certain constant, then make a move
elseif·both are less than the·difference then go back to the start of the code.
This is the code...............................
'
[noparse][[/noparse] Variables ]
voltage1· var· word
voltage2·· var·· word
'
' Main Routine
'
Main:
····· 'PAUSE 50······································· 'adjust this delay so the time through the Main'loop is approximately 1 second
······answer10 = (mVolts1 - mVolts0)··<
THE PROBLEM IS·HERE , IF answer10 < 100 (·i.e ANSWER10 =·NEGATIVE ANSWER), IT STILL PERFORM THE SAME·ACTION(·MOVE 1 TO 0)··OF WHEN·answer10 >100 BUT I WANT IT TO GO TO THE NEXT CHECK answer01 AND·MOVE 0 TO 1.
······· 'DEBUG CR, CR, " Answer10:", SDEC answer10
······· PAUSE 100
······· IF ABS(mVolts1 - mVolts0) > 100 THEN····················· 'l
········· GOTO Move1:
······· ELSE
········· GOTO Sun_check0greater1
······· ENDIF
LOOP
'
[noparse][[/noparse] Conditions ]
Sun_check0greater1:
··················· answer01 = (mVolts0 - mVolts1)······························
··················· DEBUG CR, CR, " Answer01:", SDEC answer01
··················· PAUSE 100
··················· IF ABS(mVolts0 - mVolts1) > 100 THEN
····················· GOTO Move2:
··················· ELSE
······················GOTO Main
··················· ENDIF
·Move1[noparse]:D[/noparse]EBUG CR,CR, "Look to 0- 180 degrees (RIGHT).", CR
·············· PAUSE 100
·············· FOR counter = 1 TO 2····················· ' CLOCKWISE MOVEMENT (right)
·············· PULSOUT 15, LimitRight
·············· PAUSE 270
·············· NEXT
·············· GOTO Sun_check3greater2
Move2[noparse]:D[/noparse]EBUG CR,CR, "Look from 180- 0 degrees (LEFT).", CR
············· PAUSE 100
············· FOR counter = 1 TO 2······················ ' COUNTERCLOCKWISE MOVEMENT (left)
············· PULSOUT 15, LimitLeft
············· PAUSE 270
············· NEXT
············· GOTO Sun_check2greater3
Comments
if value1 >= value0 then
positiveDifference = value1 - value0
' Here you'd do something with positiveDifference.· All arithmetic operations would give expected results.
else
negativeDifference = value0 - value1
' Here you'd do something with negativeDifference.· This value is positive, so all arithmetic operations would give expected results.· You'd have to be careful if you try to combine positiveDifference and negativeDifference values like if you attempted to average them or determine a maximum or minimum.
endif
Please Am getting an Error on my code - "Expected a constant, variable, uniray operator, or '('... I cant figure out why it is giving me that error
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' {$PBASIC 2.5}
' {$PORT COM4}
'
[noparse][[/noparse] I/O Definitions ]
CS PIN 0 ' Chip Select (MCP3204.8)
Clock PIN 1 ' Clock (MCP3204.11)
DataOut PIN 2 ' Dout on ADC (MCP3204.10)
DataIn PIN 3 ' Din on ADC (MCP3204.9)
OUTPUT CS
OUTPUT Clock
OUTPUT DataIn
INPUT DataOut
'
[noparse][[/noparse] Constants ]
'Cnts2Mv CON $0139 ' x 1.22 (To Millivolts)
'
[noparse][[/noparse] Variables ]
result0 VAR Word ' Conversion Result CH0
result1 VAR Word ' Conversion Result CH1
mVolts0 VAR Word ' Result0 -- mVolts
mVolts1 VAR Word ' Result1 -- mVolts
value0 VAR Word
value0 = %0000111111111111
value1 VAR Word
value1 = %0000111111111111
counter VAR Bit ' Loop index
'answer01 VAR Word
'answer10 VAR Word
result2 VAR Word ' Conversion Result CH2
result3 VAR Word ' Conversion Result CH3
value2 VAR Word
value2 = %0000111111111111
value3 VAR Word
value3 = %0000111111111111
mVolts2 VAR Word ' Result2 -- mVolts
mVolts3 VAR Word ' Result3 -- mVolts
'value0 = value1
'value0 = value2
DEBUG CLS, "ADC CH 0 : ", CR, "Volts :", CR,
"ADC CH 1 : ", CR, "Volts :", CR,
"ADC CH 2 : ", CR, "Volts :", CR,
"ADC CH 3 : ", CR, "Volts :", CR
'
[noparse][[/noparse] Program Code ]
Main:
PAUSE 1000
DO
LOW CS
SHIFTOUT DataIn, Clock, LSBFIRST, [noparse][[/noparse]3\5] ' Select CH0, Single-Ended
SHIFTIN DataOut, Clock, MSBPOST, [noparse][[/noparse]result0\13] ' Read ADC
HIGH CS ' Disable ADC
result0 = result0 & value0
mVolts0 = result0 *(5000/4095) ' Convert To Millivolts
'mVolts0 = result0 ** 14484 + result0
LOW CS ' Enable ADC
SHIFTOUT DataIn, Clock, LSBFIRST,[noparse][[/noparse]19\5] ' Select CH1, Single-Ended
SHIFTIN DataOut, Clock, MSBPOST, [noparse][[/noparse]result1\13] ' Read ADC
HIGH CS ' Disable ADC
result1 = result1 & value1
mVolts1 = result1 *(5000/4095) ' Convert To Millivolts
'mVolts1 = result1 ** 14484 + result1
LOW CS ' Enable ADC
SHIFTOUT DataIn, Clock, LSBFIRST, [noparse][[/noparse]11\5] ' Select CH2, Single-Ended
SHIFTIN DataOut, Clock, MSBPOST, [noparse][[/noparse]result2\13] ' Read ADC
HIGH CS ' Disable ADC
result2 = result2 & value2
mVolts2 = result2 *(5000/4095) ' Convert To Millivolts
LOW CS ' Enable ADC
SHIFTOUT DataIn, Clock, LSBFIRST, [noparse][[/noparse]27\5] ' Select CH2, Single-Ended
SHIFTIN DataOut, Clock, MSBPOST, [noparse][[/noparse]result3\13] ' Read ADC
HIGH CS ' Disable ADC
result3 = result3 & value3
mVolts3 = result3 *(5000/4095)
Display:
DEBUG HOME
DEBUG "12 - bits binary value0: ", BIN12 result0
DEBUG CR,CR, "Decimal value0: ", DEC4 result0
DEBUG CR,CR, "mvolts0 Reading0: ", DEC4 mVolts0 '
PAUSE 9000
DEBUG "12 - bits binary value1: ", BIN12 result1
DEBUG CR,CR, "Decimal value1: ", DEC4 result1
DEBUG CR, CR,"mvolt1 Reading1: ", DEC4 mVolts1 '
PAUSE 9000
DEBUG "12 - bits binary value2: ", BIN12 result2
DEBUG CR, CR, "Decimal value2: ", DEC4 result2
DEBUG CR, CR , "mvolts2 Reading2: ", DEC4 mVolts2 ,
'PAUSE 9000
' DEBUG "12 - bits binary value3: ", BIN12 result3
' DEBUG CR, CR, "Decimal value3: ", DEC4 result3
' DEBUG CR, CR , "mvolts3 Reading3`: ", DEC4 mVolts3 ,
' PAUSE 9000
'
' Main Routine
'
IF mVolts1 > mVolts0 THEN
GOTO Move1
ELSEIF (mVolts1 < mVolts0) THEN
GOTO Move2
ELSE
GOTO Sun_check
ENDIF
LOOP
'
[noparse][[/noparse] Conditions ]
Sun_check: IF (mVolts0 = mVolts1 = mVolts2 = mVolts3) THEN
PAUSE 10000
ELSE
GOTO Main:
Move1[noparse]:D[/noparse]EBUG CR,CR, "Moving zero towards One(0 to 180 degrees) (RIGHT).", CR
PAUSE 50
FOR counter = 1 TO 2 STEP -1 ' CLOCKWISE MOVEMENT (right)
PULSOUT 15, 250
PAUSE 10000
NEXT
IF (mVolts3 > mVolts2) THEN
GOTO Move3
ELSE
GOTO Move4
ENDIF
Move2[noparse]:D[/noparse]EBUG CR,CR, "Moving one towards zero (180 to 0 degrees)(LEFT).", CR
PAUSE 1000
FOR counter = 1 TO 2 STEP -1 ' COUNTERCLOCKWISE MOVEMENT (left)
PULSOUT 15,1000
PAUSE 10000
NEXT
IF (mVolts2 > mVolts3) THEN
GOTO Move4
ELSE
GOTO Move3
ENDIF
Move3:'DEBUG CR,CR, "Moving two towards three" (180 TO 0 degrees)(LEFT)."
PAUSE 50
FOR counter = 1 TO 2 STEP -1 ' COUNTERCLOCKWISE MOVEMENT (left)
PULSOUT 13,250
PAUSE 10000
NEXT
GOTO Main
Move4[noparse]:D[/noparse]EBUG CR,CR, "Moving three towards two (180 to 0 degrees)(LEFT).", CR
PAUSE 1000
FOR counter = 1 TO 2 STEP -1 ' COUNTERCLOCKWISE MOVEMENT (left)
PULSOUT 13,1000
PAUSE 10000
NEXT
GOTO Main
You are missing () in the if statement and then you have a comma where you want a single apostrophe and then you have an if without an end.
Hit F7 to compile your code and it will tell you what is wrong and will highlight where. Fix and try again until it compiles without errors and you can download and try it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
' IS STILL GIVING AN ERROR MESSAGE ON THIS LINE, Expect a constant , variable , unary operator or '('
GOTO Move1
ELSEIF (mVolts1 < mVolts0) THEN
GOTO Move2
ELSE
GOTO Sun_check
ENDIF
LOOP
Can you use the attachment manager and post the program in question? It's easier on the members trying to provide you with assistance on code, when they can view it the IDE.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Respectfully,
Joshua Donelson
www.parallax.com