Shop OBEX P1 Docs P2 Docs Learn Events
Program Code Help — Parallax Forums

Program Code Help

bjsterilitebjsterilite Posts: 2
edited 2010-12-03 07:12 in BASIC Stamp
Hello, I'm new to the Basic Stamp and PBASIC.

I'm looking for some help with my code.
I have the Stamp USB Kit and this is my code:
' =========================================================================
'
' File...... ADC08x32.BS2
' Purpose... Experiments with the ADC08x32 ADC
' Author.... Jon Williams
' E-mail.... jwilliams@parallax.com
' Started...
' Updated... 06 DEC 2004
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================
' -----[ Program Description ]---------------------------------------------
'
' This program demonstrates the various modes of the ADC08x82 ADC.
' -----[ Revision History ]------------------------------------------------
'
' -----[ I/O Definitions ]-------------------------------------------------
CS PIN 15      ' ADC0832.1
Clk PIN 14     ' ADC0832.7
Dio PIN 13     ' ADC0832.5 / ADC0832.6
' -----[ Constants ]-------------------------------------------------------
Sgl CON %1         ' single ended
Dif CON %0         ' differential
Raw2mV CON $139B   ' 19.6 mV per count
' -----[ Variables ]-------------------------------------------------------
sglDif VAR Bit     ' adc mode (1 = SE)
oddSign VAR Bit    ' chan (Sgl), sign (Dif)
adc VAR Byte(2)    ' channel values
mVolts VAR Word(2) ' millivolts
' -----[ EEPROM Data ]-----------------------------------------------------
' -----[ Initialization ]--------------------------------------------------
Reset:
  HIGH CS ' deselect ADC

  DEBUG CLS,
        "ADC08x32",
        CRSRXY, 0, 2, "Single-Ended", CR, CR,
        " Ch0:", CR,
        " Ch1:", CR,
        CRSRXY, 0, 7, "Differential", CR, CR,
        " Ch0:", CR,
        " Ch1:"

' -----[ Program Code ]----------------------------------------------------

Main:
  DO
    sglDif = Sgl
    FOR oddSign = 0 TO 1
      GOSUB Read_0832
      mVolts(oddSign) = adc(oddSign) */ Raw2mV
      DEBUG CRSRXY, 7, (4 + oddSign),
            DEC3 adc(oddSign), TAB,
            DEC mVolts(oddSign) DIG 3, ".", DEC3 mVolts(oddSign)
    IF oddSign = 0 AND (adc >= 15) THEN check_0
    IF oddSign = 1 AND (adc >= 10) THEN check_1
    PAUSE 0100
    NEXT
  LOOP

  END

' -----[ Subroutines ]-----------------------------------------------------

check_0:
  IF (adc >= 27) THEN
    HIGH 10
  ELSE
    HIGH 10
    PAUSE 0125
    LOW 10
  ENDIF
  RETURN

check_1:
  IF (adc >= 30) THEN
    HIGH 9
  ELSE
    HIGH 9
    PAUSE 0125
    LOW 9
  ENDIF
  RETURN

Read_0832:
  LOW CS
  ' send start, mode, channel
  SHIFTOUT Dio, Clk, MSBFIRST, [%1\1, sglDif\1, oddSign\1]
  ' read raw counts from ADC
  SHIFTIN Dio, Clk, MSBPOST, [adc(oddSign)\8]
  HIGH CS
  RETURN
I'm using the code I found in an issue of Nuts and Volts
that talks about interfacing the ADC0831/2 with the BS2. I have a
similar set up and I'm using two IR distance sensors on Channel 0 & 1.
The problem I'm having is where the code says...
IF oddSign = 0 AND (adc >= 15) THEN check_0
IF oddSign = 1 AND (adc >= 10) THEN check_1

I can get it to execute the check_0 subroutine which lights up my first led (LED0)
but the second IF...THEN... command doesn't seem like its working at all.
It should be lighting up my second led (LED1) if the conditions are fulfilled,
and I know that they are, but it doesn't even blink.

Is there something wrong with my code? I've tried looking it over but I can't
figure out what it could be. Thanks in advance for your help out there! :D

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2010-12-03 02:09
    The Read_0832 is saving the value into one of the two array values for adc. Since the ADC is read every iteration and the value used every iteration I don't see the need for an array. You could just reuse adc each time.

    I would either add the (oddsign) to the adc values in the IF statements or remove the (oddsign) from all occurrences of adc.

    Rich H
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-12-03 07:12
    OR try this



    IF oddSign = 1 AND (adc >= 10) THEN
    GOSUB check_1

    ELSE

    IF oddSign = 0 AND (adc >= 15) THEN
    GOSUB check_0

    ENDIF
    ENDIF
Sign In or Register to comment.