Shop OBEX P1 Docs P2 Docs Learn Events
Define range for o/p from adc0831 — Parallax Forums

Define range for o/p from adc0831

rickrockerrickrocker Posts: 7
edited 2008-03-31 03:10 in BASIC Stamp
Hi All,

I'm having issues defining a range of values that the ADC0831 outputs using the IF..ELSE loop. Could someone help.

This is what I have so far:

Calc_Value:

IF (adcBits <= 26) THEN value = 2
ELSEIF (adcBits > 26 AND adcBits <= 77) THEN value = 3
ELSEIF (adcBits > 77 AND adcBits <= 102) THEN value = 4
...
...
...
...
...
ELSE (adcBits >·204 AND adcBits <= 255) THEN value = 10
ENDIF
RETURN

The error its giving me is 'ELSEIF' must be preceded by'IF'. Why is it showing that when ELSEIF is actually preceded by IF.

Also, Can someone tell me what raw value is the o/p of the 0831adc. It has to be a binary 8-bit o/p..and if that is the case can I use that instead of the decimal values like 26, 77 etc·to define my range?

Any help is greatly apprecaited!smile.gif

Comments

  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-03-29 02:43
    ickrrocker
    The error its giving me is 'ELSEIF' must be preceded by'IF'. Why is it showing that when ELSEIF is actually preceded by IF.

    I would also reveiw the What is a Mirocontroller·Book·it starts at page 101

    You need to write this way ELSEIF··· ·with·no space


    I have not test code routine so i do not if it will work the way you want to work

    ·Main:
    · DO
    ··· GOSUB Read_0831···························· ' read the ADC
    ··· mVolts = result */ Cnts2Mv················· ' convert to millivolts


    ··· DEBUG HOME,································ ' report
    ········· CRSRXY, 9, 0, DEC result, CLREOL,
    ········· CRSRXY, 9, 1, DEC mVolts DIG 3,
    ······················· ".", DEC3 mVolts


    ·IF (adcBits <= 26) THEN
    ·value = 2
    ELSEIF (adcBits > 26) AND (adcBits <= 77) THEN
    ·value = 3
    ELSEIF (adcBits > 77) AND (adcBits <= 102) THEN
    ·value = 4
    ELSEIF (adcBits > 204) AND (adcBits <= 255) THEN
    ·value = 10




    ·ENDIF


    RETURN


    ··· PAUSE 100
    ·LOOP


    ·



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 3/29/2008 4:39:20 AM GMT
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-03-29 02:57
    rickrocker,

    ·You·must write the statements like this:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    adcBits  VAR  Byte
    value   VAR  Byte
    
    Calc_Value:
     
    [color=red]IF (adcBits <= 26) THEN
     value = 2
    ELSEIF (adcBits > 26) AND (adcBits <= 77) THEN
     value = 3
    ELSEIF (adcBits > 77) AND (adcBits <= 102) THEN
     value = 4
    ELSEIF (adcBits > 204) AND (adcBits <= 255) THEN
     value = 10[/color]
    ENDIF
    RETURN
    

    Post Edit -- Emphasis on placement of the conditionals and statements and the succeeding ELSEIFs.

    Post Edited (PJ Allen) : 3/29/2008 3:41:10 AM GMT
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-03-29 03:13
    PJ Allen

    It will not compile because you can use ·value = 2····(·Error ·Expected " : " or end of the line)·· and what the problem is the " = "

    that is why i had· GOSUB·value2··

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-03-29 03:43
    sam_sam_sam,

    Sometimes, OM, I can't quite fathom your depth.

    Anyway, it compiles for me (attached.)
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-03-29 04:24
    PJ Allen

    Sorry about that I did not put····· ·value·· VAR· Byte

    That is why it gave me that error

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2008-03-29 18:56
    The goal might be better met with a different command syntax...

    ' implements the first true case...
    SELECT adcbits
    CASE <=26
      value=2
    CASE <=77
      value=3
    CASE <= 102
      value=4
    '... & so on
    CASE <=255
      value=10
    ENDSELECT
    



    or better yet...

    LOOKDOWN adcbits, <=[noparse][[/noparse]26,77,102,...255],value  ' ... nine values, 0 to 8
    value=value+2 ' values need to be 2 to 10
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • rickrockerrickrocker Posts: 7
    edited 2008-03-31 03:10
    Thanks all! I've got it working smile.gif
Sign In or Register to comment.