Shop OBEX P1 Docs P2 Docs Learn Events
Help Please — Parallax Forums

Help Please

cedtinsicedtinsi Posts: 45
edited 2011-04-02 12:29 in BASIC Stamp
Thank you and Welcome

I am having some issues with my code.
The compilation step goes well; but at execution, it is exactly what I do not want.

The Basic Stamp skips everything and executes ' wewillsee1' at the very bottom of the code.

Down below is the code.

I mean when all the pins are not receiving any signal, the Stamp nevertheless executes 'wewillsee1'

I do not get it, please Help.


Thank you.





' {$STAMP BS2}
' {$PBASIC 2.5}
timeCounter VAR Word

Main:
timeCounter=0
PAUSE 4000
timeCounter=timeCounter + 4000
IF (IN2=1) THEN
GOTO wewillsee1
ELSEIF (IN3=1) THEN
GOTO wewillsee1
ELSEIF (IN4=1) THEN
GOTO wewillsee1
ELSEIF ((IN1=1) AND (IN2=1))THEN
GOTO wewillsee1
ELSEIF ((IN1=1) AND (IN3=1) )THEN
GOTO wewillsee1
ELSEIF ((IN1=1) AND (IN4=1))THEN
GOTO wewillsee1
ELSEIF ((IN2=1) AND (IN3=1)) THEN
GOTO wewillsee1
ELSEIF ((IN1=1) AND (IN2=1) AND (IN3=1)) THEN
GOTO wewillsee1
ELSEIF ((IN1=1) AND (IN2=1) AND (IN4=1)) THEN
GOTO wewillsee1
ELSEIF ((IN2=1) AND (IN3=1) AND (IN4=1)) THEN
GOTO wewillsee1
ELSEIF ((IN1=1) AND (IN2=1) AND (IN3=1) AND (IN4=1)) THEN
GOTO wewillsee1
ENDIF
IF (IN1=1) THEN
PAUSE 2000
ELSE
PAUSE 1
END
IF (IN1=1) THEN
IF (IN2=1) THEN
PAUSE 2000
ELSEIF (IN3=1) THEN
GOTO wewillsee2
ELSEIF (IN4=1) THEN
GOTO wewillsee2
ELSE
PAUSE 1
ENDIF
ENDIF
IF (IN1=1)THEN
IF (IN2=1) THEN
IF (IN3=1) THEN
PAUSE 4000
HIGH 15
LOW 14
ELSEIF (IN4=1) THEN
GOTO wewillsee3
ELSE
PAUSE 1
ENDIF
ENDIF
ENDIF
PAUSE 5000
IF (IN1=1)THEN
GOTO wewillsee4
ELSEIF (IN2=1)THEN
GOTO wewillsee4
ELSEIF (IN3=1)THEN
GOTO wewillsee4
ELSEIF (IN4=1)THEN
GOTO wewillsee4
ELSEIF (IN1=1)AND (IN2=1)THEN
GOTO wewillsee4
ELSEIF (IN1=1)AND (IN3=1)THEN
GOTO wewillsee4
ELSEIF (IN1=1)AND(IN4=1)THEN
GOTO wewillsee4
ELSEIF (IN2=1)AND(IN3=1)THEN
GOTO wewillsee4
ELSEIF (IN1=1)AND(IN2=1)THEN
IF (IN3=1)THEN
GOTO wewillsee4
ELSE
IN3=1
GOTO wewillsee4
ENDIF
ELSEIF (IN1=1)AND(IN2=1)THEN
IF (IN4=1)THEN
GOTO wewillsee4
ELSE
IN4=1
GOTO wewillsee4
ENDIF
ELSEIF (IN2=1)AND(IN3=1)THEN
IF (IN4=1)THEN
GOTO wewillsee4
ELSE
IN4=1
GOTO wewillsee4
ENDIF
ELSE
IF (IN1=1)AND(IN2=1)THEN
IF (IN3=1)AND(IN4=1)THEN
GOTO wewillsee4
ENDIF
ENDIF
ENDIF
ENDIF
PAUSE 1000
IF (timeCounter>=9000)THEN
GOTO wewillsee5
ELSE
PAUSE 1
ENDIF
GOTO Main

wewillsee1:
PAUSE 8000
HIGH 13
LOW 12
PAUSE 5000
LOW 13
LOW 12
GOTO Main
END
wewillsee2:
PAUSE 6000
HIGH 13
LOW 12
PAUSE 5000
LOW 13
LOW 12
GOTO Main
END
wewillsee3:
PAUSE 8000
HIGH 13
LOW 12
PAUSE 5000
LOW 13
LOW 12
GOTO Main
END
wewillsee4:
PAUSE 5
HIGH 11
LOW 10
PAUSE 5000
LOW 11
LOW 10
GOTO Main
END
wewillsee5:
PAUSE 1
HIGH 13
LOW 12
PAUSE 5000
LOW 13
LOW 12
GOTO Main
END
«1

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-09 17:24
    I suggest you place some DEBUG statements in your code so that you can tell where the code is branching to wewillsee1. You can also use DEBUG to show the values for IN1, IN2, IN3, and IN4. Your program is somewhat complex, with no comments. You need to learn how to debug it yourself and, once you've gathered some data, ask again why it doesn't behave the way you expect ... and present the data and what you've learned from it (and still don't understand).
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-09 17:24
    OK, cedtinsi, time to start Attaching your code.
    What arrangement are you using for your Inputs? Are they pulled low?
    Obviously, it's convinced one of your IFs is being satisfied (some INPUT or
    combination "= 1").
    By placing some DEBUG statements you could tell when/where that occurs.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-09 17:51
    It occurs at the very first IF.

    Why is that?

    Nothing in the program tells me that it should do so

    The first IF condition is satisfied.
    Why?

    I checked my circuit as well and pin2 does not receive any current.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-09 18:02
    PJ asked...
    "What arrangement are you using for your Inputs? Are they pulled low?"
    and got no reply.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-09 23:38
    Thank you and welcome

    My new and improved code, please see attachment.






    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    timeCounter VAR Word
    'please refer to the attached file for a schematic of the circuit, thank you.
    Main:
    DO
    IF((IN1=1)OR(IN2=1)OR(IN3=1)OR(IN4=1))THEN 'at the beginning of the evaluation, we always want to make sure that all the
    'pushbuttons are open; so if only one of them is not open, then we want the blue LED to glow as an indicator to the operator
    'that the pushbuttons must be opened before we even begin
    'all the wewillsee that you see below end will a goto main statement; this in order to care of this same business
    GOTO wewillsee3
    ENDIF
    LOOP UNTIL ((IN1=0)AND(IN2=0)AND(IN3=0)AND(IN4=0))
    PAUSE 4000 'pause for 4 seconds;during this 4 seconds,the operator should have pressed at least one pushbutton
    'so that when the if statements bellow are read, one of those conditions gets satisfied and a response is triggered.
    ' if the operator does not press any during the 4 seconds,then he/she will spend eternity trying to get the right combination
    IF (IN2=1)THEN
    GOTO wewillsee1 'if the first button pressed by the operator is pushbutton2
    'then pause for 6 seconds and red LED glows for 4 seconds and go back to main,we pause for 6 seconds because
    'we do not want the operator to guess in any way that the correct combination does not start by pressing pushbutton2
    'so we want the total amount of time that ellapses before the operator gets a response, to be approximately the same whether he/she
    'is getting the right combination or not
    'we will let him joke around for a little bit
    ELSEIF (IN3=1)THEN
    GOTO wewillsee1 'same thing we want to happen when the first pushbutton pressed is not pushbutton1
    ELSEIF (IN4=1)THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1))THEN 'if after the 4 seconds have ellapsed, the bs2 finds that pushbutton1 and pushbutton2 are pressed
    'then we want the bs2 to do go to wewillsee1 as well, because we want to set it up in a way that the operator who knows the correct
    'combination of pushbuttons has to know as well that he has 2 seconds to press the next pushbutton after he pressed the first one
    'we assumed that the time it takes the bs2 to decide that series of pushbuttons are on ,or pressed, at the same time is very very small
    'let us call this very short amount of time, t
    'so, the time the operator will take to press a pushbutton after having pressed a previous one-- all this happening during the 2 seconds
    ' period that we can change at will-- will be greater than max t . you are probably wondering what is the 2 seconds we are talking
    'about; keep reading, explanation is just down below
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN3=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN4=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN2=1)AND(IN3=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1)AND (IN3=1)) THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1)AND (IN4=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN2=1)AND(IN3=1)AND (IN4=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1)AND (IN3=1)AND(IN4=1))THEN
    GOTO wewillsee1
    ENDIF
    IF (IN1=1)THEN 'now, we consider the case of the right combination of pushbuttons; remember,that is pushbutton1 and then 2 and then 3
    'we suggest you look at the attached document
    PAUSE 2000 'this is the 2 seconds that we talked about earlier, we can adjust it if we judge that we need to increase it a bit
    ENDIF
    timeCounter = 0
    PAUSE 1
    timeCounter = timeCounter + 1 'we initialize timeCounter
    'the purpose of this timeCounter is to help us know whether or not the 2 seconds pause that follows will take place; knowing whether
    'or not it takes place is important to us as we will see shortly
    IF (IN1=1)THEN 'verifies that pushbutton1 is still on before testing for pushbutton2
    IF (IN2=1)THEN
    PAUSE 2000
    ELSEIF (IN3=1) THEN
    GOTO wewillsee1 'wrong combination of code, red LED must shine
    ELSEIF (IN4=1)THEN
    GOTO wewillsee1 ' obviously, wrong combination as well
    ENDIF
    ENDIF
    IF timeCounter = 2001 THEN 'helps verify whether or not pushbutton2 has been pressed
    IF ((IN1=1)AND (IN2=1)AND (IN3=1))THEN ' at this point, we are sure that if pushbutton1 and 2 and 3 are
    'on then we want To have the green LED glow
    GOTO wewillsee2
    ENDIF
    ENDIF



    wewillsee1:
    PAUSE 6000
    HIGH 13
    LOW 12
    PAUSE 4000
    LOW 13
    LOW 12
    GOTO Main
    END
    wewillsee2:
    PAUSE 6000
    HIGH 11
    LOW 10
    PAUSE 4000
    LOW 11
    LOW 10
    GOTO Main
    END
    wewillsee3:
    PAUSE 6000
    HIGH 15
    LOW 14
    PAUSE 4000
    LOW 15
    LOW 14
    GOTO Main
    END
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-03-10 04:39
    I give up.
  • stamptrolstamptrol Posts: 1,731
    edited 2011-03-10 04:58
    The only way the first IF statement will execute is if any of P0, P1, P2, P3 are 1's.

    Somehow, one of those pins is at a voltage level that the Stamp thinks is a 1. The traditional way to guard against this is to have each pin "pulled-down" to ground by a high valued resistor, say 47K.

    Also, the program will be very hard to debug because of all the long delays. You will never know whether the program is mis-behaving or just waiting.

    As has been pointed out several times.....put some DEBUG statements in your program so you know where it is acting up. The other thing to do is to break the program into pieces and test each piece. Start with the first IF statement.

    Cheers,
  • electrosyselectrosys Posts: 212
    edited 2011-03-10 06:24
    @cedtinsi
    Please insert your codes in the # [code] tags. I suggest (if it is possible) you divide you program code in same different ports and try to debug your code, port by port, from start to end.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-03-10 10:19
    cedtinsi wrote: »
    ress any during the 4 seconds,then he/she will spend eternity trying to get the right combination
    IF (IN2=1)THEN
    GOTO wewillsee1
    ELSEIF (IN3=1)THEN
    GOTO wewillsee1
    ELSEIF (IN4=1)THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1))THENGOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN3=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN4=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN2=1)AND(IN3=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1)AND (IN3=1)) THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1)AND (IN4=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN2=1)AND(IN3=1)AND (IN4=1))THEN
    GOTO wewillsee1
    ELSEIF ((IN1=1)AND(IN2=1)AND (IN3=1)AND(IN4=1))THEN
    GOTO wewillsee1
    ENDIF
    IF (IN1=1)THEN
    PAUSE 2000
    ENDIF

    Wow. You sir are my hero, I could never have written that bit...... I start drooling after about the third line :)

    Before you blame your program any more, check for resistors in the input lines. Open inputs will "float" giving random states e.g.: 0-1-0-0-0-1-1-............ You have to pull it to ground through a resistor so that with no voltage on the pin it ALWAYS reads ZERO or OFF (Others have mentioned this, I'm just stating the importance)
  • vaclav_salvaclav_sal Posts: 451
    edited 2011-03-10 10:57
    1. "When the BS1 is powered up, or reset, all memory locations are cleared to 0, so all pins are inputs (DIRS = %00000000..."
    Electronics 101 - an unconnected pin is floating high. The processor - no surprise here - will read it as true, logic 1, high.

    2. Basic program

    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}
    IF IN1 = 1 THEN
    DEBUG ? IN1
    ELSE
    DEBUG ? IN1
    ENDIF

    2. Advanced program

    ' {$STAMP BS2e}
    ' {$PBASIC 2.5}

    IF IN1 THEN
    DEBUG ? IN1
    ELSE
    DEBUG ? IN1
    ENDIF

    3. A No no program - as originally posted in this thread


    Baby steps Bob, baby steps!!!!!


    PS PJ - will I get his attention??
  • vaclav_salvaclav_sal Posts: 451
    edited 2011-03-10 11:03
    Spiral - close but no cigars!
  • PublisonPublison Posts: 12,366
    edited 2011-03-10 11:16
    According to your schematic, there are no pull down resistors on the Input pins. As others have suggested, they are needed on each input. (You never answered PJ's question).

    For future reference, the preferred attachment for documentation is a PDF file. Many people do not own Microsoft Office, let alone Office 2010. I have Office 2003 and have a converter for 2010 files, but it is not something some people want to download. I'm not sure if Open Office can read them.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-10 11:22
    First I would like to thank all of you for your continuous assistance.


    A particular thank you to Mr. Stamptrol, I get that I need to create a path to ground for all the 4 pins through a high value resistor.
    I will insert some debug statements in the code as well, I fully understand the importance of those.

    What I do not get is how to divide the code into pieces and test them individually.
    Also, I would appreciate an effective suggestion of yours on how to minimize the delay time of execution of the code.

    To Mr. Electrosys,
    I appreciate your contribution. I am not good yet, in fact know very little, about partitioning the code to different ports and then debud the code port by port; so, I do not get what you mean.

    To Mr. Spiral_72,
    Thank for providing me with more emphasis on the importance of placing a ground path, through a high value resistor, for the pins. I got it.

    To Mr. Vaclav_sal,
    Sure, an unconnected pin is floating high; we got how to fix it.
    thanks.

    To Mr. Publison
    I'm sorry that I did not answer PJ's question, I was busy figuring out what was wrong; later on, I came back to the forum and found out that PJ's question was actually an answer.
    thank you to both of you.

    Now, how do we minimize the code execution delay? divide the code into pieces and test them individually?

    Your asssistance, as always, I say thank you.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-10 11:44
    In order to speed up things and make testing manageable, you have to think about this completely differently. You don't have just a series of I/O pins that you're testing. You have a pattern of bits. Consider reading all the bits at one time, then testing for patterns in the numbers. It would be easier if you used I/O pins 0-3 instead of 1-4, but it still works. To read the 4 I/O pins and store them in a nibble-size variable "pattern", you'd do:
    pattern = (INL >> 1) & $F
    This reads the least significant 8 I/O pins as a group, shifts pins 1-4 into position and masks them off. If other pins are outputs, that's ok since their output state will be read (and ignored). If you used I/O pins 0-3, you could just get the 4 bit value by using INA.

    Your complicated IF statement is testing for values where bits 2^1, 2^2, and 2^3 are 1. That includes 8-15, 6, 4, 2. It also tests for some odd values: 3 and 5, but also 7. Eventually, everything but 0 and 1 cause a jump to wewillsee1. Is that really what you want?
  • vaclav_salvaclav_sal Posts: 451
    edited 2011-03-10 12:12
    " how do we minimize the code execution delay "
    You may think I am picking on you, but as stated - the answer is - you do not!
    "code execution (delay)" itself is fixed by the processor, however execution of your program is controlled by you.
    For example - you may wish to indicate the correct code entered by turining on the LED's for 10 seconds even after the user quit pushing the buttons.
    Or you may want ot flash them in some fancy sequece. You are in control here.

    As far as modular approach consider this at the begining of you code


    ' any burned out LED's?
    GOSUB CheckLED ' turn each LED on for I second

    ' LED'OK?
    GOSUB AllOK ' print debug "All OK"

    ' check pushbuttons
    GOSUB CheckButtons ' let user push each button and turn on appropriate LED

    ' all buttons works?
    GOSUB AllOK '....

    Modular program like one suggested let you reuse your code - subroutine AllOK - and insert another code where necessary without mucking up the works too much.

    Also, if you are concerend about maintenance of your program by others - comment like the vote in Chicago - early and often!
    A final note - program like yours is a prime cadidate for SELECT and or LOOKUP commands. But that may be too advanced for now.
    For now remeber that IF checks for true condition and skips next statement(s) if false.
    Cheers Vaclav
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-10 12:52
    Thank you Mr. Green

    I know that you knew I would be back to say that 'I do not understand'
    You got me, for I do not get the following:

    "Your complicated IF statement is testing for values where bits 2^1, 2^2, and 2^3 are 1. That includes 8-15, 6, 4, 2. It also tests for some odd values: 3 and 5, but also 7. Eventually, everything but 0 and 1 cause a jump to wewillsee1."

    And what about that "pattern = (INL >> 1) & $F" ; I do not see anything of the like in the Basic Stamp Manual.

    To answer your question: that is not what I want.

    I thought that the code I wrote will sense the state of pins 1-4 and raise the digit 1, when a current is getting into the a pin, for that pin; else raise the digit 0.
    The same reasoning has been used at page 78 of the "What is a Microcontroller?"
    So, how come ?
    I mean I am ready to think about this in a different way; but I need you to further asssist me on this.

    thank you.


    Mr. Vaclav_sal,
    Thank you, I will get better on that.
  • PublisonPublison Posts: 12,366
    edited 2011-03-10 13:19
    It is good that you mentioned page 77 in "What is a Microcontroller"

    Do you have connections indicated on Page 77 showing 10K pulldown resistors to Vss (Ground)? I don't think that question has been answered yet, but asked many times.

    cedtinsi wrote: »
    Thank you Mr. Green

    I know that you knew I would be back to say that 'I do not understand'
    You got me, for I do not get the following:

    "Your complicated IF statement is testing for values where bits 2^1, 2^2, and 2^3 are 1. That includes 8-15, 6, 4, 2. It also tests for some odd values: 3 and 5, but also 7. Eventually, everything but 0 and 1 cause a jump to wewillsee1."

    And what about that "pattern = (INL >> 1) & $F" ; I do not see anything of the like in the Basic Stamp Manual.

    To answer your question: that is not what I want.

    I thought that the code I wrote will sense the state of pins 1-4 and raise the digit 1, when a current is getting into the a pin, for that pin; else raise the digit 0.
    The same reasoning has been used at page 78 of the "What is a Microcontroller?"
    So, how come ?
    I mean I am ready to think about this in a different way; but I need you to further asssist me on this.

    thank you.


    Mr. Vaclav_sal,
    Thank you, I will get better on that.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-10 13:46
    The pull down resistors, taken care of.
    Now, I am trying do understand Mr. green's suggestion on how to speed things up.

    Thank you.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-10 14:39
    Look up the pieces in the Stamp Manual. At a minimum, look for INL, INA, >> (or SHR), and &. There are descriptions of all of these. Try out the statement in a simple test program. See what it does.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-10 18:55
    Mr. Green,
    This is the code that I tested, as you asked me to.
    I went through the manual as well.

    The code below does not respond to the presses of any button; so what am I missing?

    Thank you.



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    pattern VAR Nib
    PAUSE 4000
    pattern = (INL >> 1) & $F
    DO
    IF pattern=%011100000 THEN
    HIGH 15
    LOW 14
    ENDIF
    DEBUG "*",CR
    LOOP
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-10 19:32
    Mr. Green, I tried this, but it does not respond to the push of any button.
    Any suggestions?

    I went into the Stamp Manual and got some information.



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    pattern VAR Nib
    DIR3=0
    PAUSE 4000
    pattern = (INL >> 1) & $F
    DO
    IF IN3=1 THEN
    HIGH 15
    LOW 14
    ENDIF
    DEBUG "*",CR
    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-10 19:40
    Your first attempt doesn't do anything because "pattern" is a nibble ... a 4-bit value that can take on values from 0 to 15 or $0 to $F or %0000 to %1111 (all of these are the same). %01110000 on the other hand is an 8-bit (or 7-bit) value and can never be equal to a 4-bit value. How about using a DEBUG statement to look at the value of pattern after it gets set? After that, you can put the assignment statement and the DEBUG statement in a DO / LOOP so it can repeat.

    Your 2nd attempt has a mix of what I suggested and what you've been doing already. I don't know why you're making one pin high and the other pin low. If you're trying to produce a pulse, that won't do it.

    You clearly have major gaps in your understanding of how a Stamp works and how Parallax Basic works ... all the more reason to start at the beginning of "What's a Microcontroller?" and work through the examples one by one.
  • Mike GMike G Posts: 2,702
    edited 2011-03-10 20:03
    Your code is way more complicated than it needs to be.

    Here is a very simple demo that increments the OUTA register, reads the value (INA), then displays the value (DEBUG). Plus this demo show how to break your code up into small chunks. Mike Green is right, you need to take some time to learn and experiment.
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    counter   VAR   Nib
    value     VAR   Nib
    
    counter = 0
    value = 0
    
    
    Main:
      ' Init
      DIRS = $FF  'Make all pins outputs
      OUTS = $00  'Set pins low
    
      GOSUB WritePins
      PAUSE 100
      GOSUB ReadPins
      PAUSE 100
      GOSUB DisplayPins
      PAUSE 1000
    
    GOTO Main
    
    ' Increment
    WritePins:
      OUTA = counter
      counter = counter + 1
    RETURN
    
    ' Read pins
    ReadPins:
      value = INA
    RETURN
    
    
    'Display pins
    DisplayPins:
      DEBUG CLS
      DEBUG BIN ?value
    RETURN
    
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-10 20:10
    Thank you Mr. Mike G(reen).

    I really do appreciate the way you push me to work harder.


    Thank you more because you have the answer to every single question I have.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-24 10:59
    Thank You and Welcome.

    Down below is the new and improved code; * will appear at the debug terminal whenever pattern is 7.

    The goal is to set up a 3 digit pin code using 4 pushbuttons; each pushbutton acts as a pin. Pressing the right combination of pushbuttons will, say, open a door.

    Using a version of the code below, I am able to open the door, say, if pushbutton 2,3 and 4 are pressed down; but I want to be able to press pushbutton 2, then wait for 2sec and then press pushbutton 3, then wait for 2sec and then press pushbutton 4 before the door get get opened.
    the problem is that: when pattern is, say, 4, when pushbutton 2 is pressed, i need to wait the 2sec and ask if pattern is, say, 2 (corresponding to the press of pushbutton 3)... but i do not know how to make the stamp forget for 2sec that pattern was 4 and test pattern anew.

    I need you help.

    Thank you.






    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    pattern VAR Nib
    DO
    PAUSE 4000
    pattern=(IND >> 1)& $F 'gets the state of the last four pins
    DEBUG ? pattern, CR ' output to the debug terminal
    IF (pattern=7) THEN
    DEBUG "*"
    ENDIF
    LOOP
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-24 11:13
    What you are describing is a "state machine" where each state is represented by a loop in the program. Look up a description of this in the Wikipedia.

    Typically, your program waits in a loop for the proper combination of buttons to be pushed, then that loop exits. Your program then waits for all buttons to be released (a sort of reset), then there's another loop that waits for another combination of buttons to be pushed, then another wait for the buttons to be released, etc.
    do ' state 1
    ' get pattern
    until pattern = <first pattern>
    do ' transition state
    ' get pattern
    until pattern = 0
    do ' state 2
    ' get pattern
    until pattern = <second pattern>
    do  ' transition state
    ' get pattern
    until pattern = 0
    do ' state 3
    ' get pattern
    until pattern = <third pattern>
    ' success
    
    You might put a time limit in each loop, maybe a count that's decremented by one each time through the loop. When the count reaches zero, there's a goto to the beginning of the program. Try an initial count of 5000 and see how long that takes to "time out". You don't need the "& $F" because IND is already only 4 bits and the shift drops one of those bits. Something like:
    timeOut = 5000
    do
    pattern = ind >> 1
    timeOut = timeOut - 1
    if timeOut = 0 then goto beginning
    until pattern = ????
    
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-24 15:24
    Mr. Green,

    You are my man.
    You got exactly what I meant.
    Once again, this forum is " education to all, for free. "

    Thank you, so very much.
  • cedtinsicedtinsi Posts: 45
    edited 2011-04-01 14:03
    Thank You and Welcome.

    Please I would like to know what is the AND NOT operator for all Basic Stamp 2 models.

    thanks.
  • cedtinsicedtinsi Posts: 45
    edited 2011-04-01 16:00
    Thank You and Welcome.

    down below is the code that I executed for my four pushbuttons, mentioned in the previous posts.

    my code gets stuck at the second do loop; it is as if it cannot execute an IF THEN statement.

    Please help me get this.





    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    pattern VAR Nib
    timeout VAR Word
    control VAR Word

    beginning:
    pattern=IND
    DEBUG ? pattern, CR
    DO
    pattern=IND
    DEBUG ? pattern, CR
    LOOP UNTIL pattern=8
    DO
    IF pattern=0 THEN GOTO intern1 ' the code gets stuck here
    LOOP

    intern1:
    timeout=50
    DO
    pattern=IND
    DEBUG ? pattern, CR
    timeout=timeout-1
    LOOP UNTIL pattern=2
    DEBUG SDEC ? timeout
    DO
    IF ((timeout=0)|(timeout<0)) THEN GOTO beginning
    IF (timeout>0) THEN GOTO intern2
    LOOP



    intern2:
    timeout=50
    DO
    pattern=IND
    DEBUG ? pattern, CR
    timeout=timeout-1
    LOOP UNTIL pattern=1
    DEBUG SDEC ? timeout
    DO
    IF ((timeout=0)|(timeout<0)) THEN GOTO beginning
    IF (timeout>0) THEN GOTO intern3
    LOOP
    intern3:
    HIGH 11
    LOW 10
    PAUSE 1000
    LOW 11
    LOW 10
  • cedtinsicedtinsi Posts: 45
    edited 2011-04-01 17:17

    Thank You and Welcome.

    down below is the code that I executed for my four pushbuttons, mentioned in the previous posts.

    my code gets stuck at the second do loop; it is as if it cannot execute an IF THEN statement.
    I did not create an infinite loop, I am sure; because I tried the code, and the intern1 won't even get executed.

    Please help me get this.





    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    pattern VAR Nib
    timeout VAR Word
    control VAR Word

    beginning:
    pattern=IND
    DEBUG ? pattern, CR
    DO
    pattern=IND
    DEBUG ? pattern, CR
    LOOP UNTIL pattern=8
    DO
    IF pattern=0 THEN GOTO intern1 ' the code gets stuck here
    LOOP

    intern1:
    timeout=50
    DO
    pattern=IND
    DEBUG ? pattern, CR
    timeout=timeout-1
    LOOP UNTIL pattern=2
    DEBUG SDEC ? timeout
    DO
    IF ((timeout=0)|(timeout<0)) THEN GOTO beginning
    IF (timeout>0) THEN GOTO intern2
    LOOP



    intern2:
    timeout=50
    DO
    pattern=IND
    DEBUG ? pattern, CR
    timeout=timeout-1
    LOOP UNTIL pattern=1
    DEBUG SDEC ? timeout
    DO
    IF ((timeout=0)|(timeout<0)) THEN GOTO beginning
    IF (timeout>0) THEN GOTO intern3
    LOOP
    intern3:
    HIGH 11
    LOW 10
    PAUSE 1000
    LOW 11
    LOW 10
Sign In or Register to comment.