Shop OBEX P1 Docs P2 Docs Learn Events
Comparison on basic stamp 2 — Parallax Forums

Comparison on basic stamp 2

debonenedebonene Posts: 19
edited 2009-11-25 05:26 in BASIC Stamp
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

Comments

  • debonenedebonene Posts: 19
    edited 2009-11-24 22:25
    In Summary: I a have a defined positive and negative value move, whenever it does a comparison and the result is a negative, it still carry out the positive move even though the result is negative. How can I correct that error so that it can do positive move only when positive and negative move when negative.
  • debonenedebonene Posts: 19
    edited 2009-11-24 22:26
    In Summary: I have a defined positive and negative Action based on the result( either positive ir negative), whenever it does a comparison and the result is a negative, it still carry out the positive action even though the result is negative. How can I correct that error so that it can do positive action only when positive and negative action when negative.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-24 23:26
    You have to separate out the negative and the positive values.· The Stamp does all its arithmetic with positive numbers from 0 to 65535.· You can also represent negative values from -32768 to -1 by using the positive numbers from 32768 to 65535 and addition and subtraction will work properly, but comparisons between negative and positive numbers won't work.
  • debonenedebonene Posts: 19
    edited 2009-11-25 01:09
    Thank you Mike Green for your quick response as usual. Please how do i declare the variable to be able seperate the positive and negative value or could you give me a sample code so that i can fully understand you explanation in the earlier response.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-25 01:16
    One way to manage this is to do:

    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
  • debonenedebonene Posts: 19
    edited 2009-11-25 04:11
    Thanks Mike Green.
    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
  • FranklinFranklin Posts: 4,747
    edited 2009-11-25 04:14
    Please attach your actual code as an attachment to your next post. What you have so far won't work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • debonenedebonene Posts: 19
    edited 2009-11-25 04:26
    ' {$STAMP BS2}
    ' {$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
  • debonenedebonene Posts: 19
    edited 2009-11-25 04:30
    Thanks Franklin, have pasted it.
  • FranklinFranklin Posts: 4,747
    edited 2009-11-25 04:51
    You really need to ATTACH it using the attachment manager when you click the POST REPLY button.
    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
  • debonenedebonene Posts: 19
    edited 2009-11-25 05:14
    IF (mVolts1 > mVolts0) THEN <
    ' 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
  • JDJD Posts: 570
    edited 2009-11-25 05:26
    Debonene,

    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
Sign In or Register to comment.