Shop OBEX P1 Docs P2 Docs Learn Events
IF condition for array — Parallax Forums

IF condition for array

krimskrims Posts: 8
edited 2009-05-18 22:44 in BASIC Stamp
hi,

I·am using array to store 4databits.·I·have 2 approaches· to check the data bits using·IF...Else.. condition.

Approahc 1:

t VAR Bit(4)· 't declared as array of 4-bits
i VAR Nib
......
FOR i= 0 TO 3 'Start of the Command(data bits)
····· OUT3=IN2
····· t(i)=IN2
····· PAUSE 75 'time period of the each databit (75ms)
NEXT
......
'check· for the condition whether the·data is recieved or not
IF t=%1010 THEN
LOW 10· ' ON the RED LED
HIGH 11· ' OFF the Green LED
ELSE
LOW 11· 'ON the Green LED
HIGH 10· 'OFF the Red LED
ENDIF
Approach 2:

t VAR Bit(4)· 't declared as array of 4-bits
i VAR Nib
......
FOR i= 0 TO 3 'Start of the Command(data bits)
····· OUT3=IN2
····· t(i)=IN2
····· PAUSE 75 'time period of the each databit (75ms)
NEXT
......
'check· for the condition whether the·data is recieved or not
IF t(0)=%1 THEN
· IF t(1)=%0 THEN
··· IF t(2)=%1 THEN
····· IF t(3)=%0 THEN
········· LOW 10
········· HIGH 11
····· ELSE
········· LOW 11
········· HIGH 10
···· ENDIF
·· ENDIF
·ENDIF
ENDIF

out of the·above 2 approaches, Approach 2 is working but 1 is not. I would like approach 1 to be use in my application, i was wondering if any one could help me out.
·Thanks· in advance.

·

Comments

  • techgeektechgeek Posts: 12
    edited 2009-05-15 04:17
    is the data comming from the computer or from a switch on the board?
    if it is comming from a switch on the board, then please provide schematics.
    might be able to help....
  • SRLMSRLM Posts: 5,045
    edited 2009-05-15 05:14
    Why not add the incoming bit to a nib, and shift it left each time? Something like this (pseudo code)

    Loop 4
    input <<= input
    input += IN2
    PAUSE 75

    I don't think you can access an array as the smashed together four bits (pretend nib?)
  • RiJoRiRiJoRi Posts: 157
    edited 2009-05-15 16:27
    I may be missing something here, but
    FOR i= 0 TO 3 'Start of the Command(data bits)
          OUT3=IN2
          t(i)=IN2
          PAUSE 75 'time period of the each databit (75ms)
    NEXT
    



    will read whatever is on Pin2 for 225 milliseconds, and report it. The chances are VERY good that it will NOT match %1010*. There are two solutions for this: one is to use what is called a START bit to notify the Stamp that data is about to start arriving; the other is to use a separate CLOCK signal to inform the Stamp that there is a valid DATA bit on the PIN.

    I suggest you look around here, then elsewhere on the web, for information on Serial Communications.

    --Rich

    * Even if the data line has a 6.67 Hz square wave on it, chances are 50-50 that you will start reading at the wrong level.
  • krimskrims Posts: 8
    edited 2009-05-15 22:30
    actually i am getting data on PIN2 like 1010 each bit is 75ms duration. before the data i have synch pulse of 100ms(ON+OFF), and a start bit of 150ms (ON+OFF) then the data will start. Data is coming from synapse board.

    for this my code,

    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    '
    'Asynchronous Communication between PROTOBOARD and BASIC STAMP
    '

    t VAR Bit(4) 't declared as array of 4-bits
    i VAR Nib

    INPUT 2 'PIN 2 of Basic stamp is configured as INPUT
    OUTPUT 3 'PIN 3 of Basic stamp is configured as OUTPUT


    tloop:

    IF IN2=1 THEN 'check for SYNCH pulse
    PAUSE 100 'time period of the SYNCH pulse (ON+OFF)

    IF IN2=1 THEN ' check for START Bit
    PAUSE 150 ' time period of the START bit (ON+OFF)

    FOR i= 0 TO 3 'Start of the Command(data bits)
    OUT3=IN2
    t(i)=IN2
    PAUSE 75 'time period of the each databit (75ms)

    NEXT
    ENDIF
    ENDIF

    'check for the condition whether the command is recieved or not
    IF t=%1010 THEN
    LOW 10
    HIGH 11
    ELSE
    LOW 11
    HIGH 10
    ENDIF
    GOTO tloop


    at the end i had IF..Else... condition i got problem with this only. Already i mentioned my 2 approaches.
  • krimskrims Posts: 8
    edited 2009-05-15 22:31
    i will try that shift left then i reply u people. thanks for ur suggestions
  • krimskrims Posts: 8
    edited 2009-05-18 17:57
    thanq very much SRLM, i got the output. i used shift operator like this
    ......
    FOR i= 1 TO 4 'Start of the Command(data bits)
    t=t<<1
    t=t+IN2
    PAUSE 75 'time period of the each databit (75ms)
    NEXT
    .......
  • vaclav_salvaclav_sal Posts: 451
    edited 2009-05-18 22:44
    Friend,
    You may run into few problems with your approach.
    You are checking only for start / beginning of both synch and start pulses / conditions.
    Same with your data, only one snapshot of the input pin after 75ms pause.
    I would suggest investigating SERIN.
    Run SERIN to detect the synch pulse (using timeout),
    than run it again to detect START pulse.
    Than run SERIN to detect the data. You should be able to run it as 8 bit serial data and than mask off the unwanted bits.
    Your 150 ms start pulse could also be programmed as two start bits and the bit time of 75 ms should correspond to appropriate baud rate. (See SERIN description for rate calculation details).
    Also would like to know how do you detect when synch pulse stops· and start pulse begins.
    Cheers Vaclav
    ·
    ·
Sign In or Register to comment.