Shop OBEX P1 Docs P2 Docs Learn Events
Program with the Basic Stamp. — Parallax Forums

Program with the Basic Stamp.

cedtinsicedtinsi Posts: 45
edited 2011-03-04 18:57 in BASIC Stamp
Welcome and Thank you for having any kind of interest in my title: Program with the Basic Stamp.

I actually have a question, and I would be pleased to have it answered.

The question is about writing codes executable by the Basic Stamp.

Here is the question: Can I force a IF... ELSE statement into another IF... ELSE statement ( I hope you understand what I am trying to insinuate)?
In a programing language such as the C language, I can do it.

I do not know how to do it with the PBASIC language.

Please you are welcome to give me some directions.

thank you.

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2011-03-04 12:32
    Yes, you can have several levels of IF statements within others. If you have the Programming environment loaded on your computer, the Helpfile gives a good explanation of every command and the common variations.
    There is also a CASE statement which functions like multiple IF....THEN statements, but allows cleaner looking code.


    Cheers,
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-04 12:43
    That is an answer for which I am happy to say Thank you.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-04 13:08
    Dear Sir,

    The answer you provided me with was exactly what I needed; now, I am tempted to ask another question.

    It concerns the decision time of the Basic Stamp when it tries to execute the commands of a program.
    Specially, for the specific purpose of my question, I am interested in knowing the decision time associated to a AND linking.
    For example (taken from page 78 of "What is a Microcontroller?" manual) consider the statement: IF (IN3=1) AND (IN4=1) THEN ................ELSEIF (IN4=1) THEN.....

    In the statement above, what is the maximum time that the Basic Stamp allows to pass before it makes the decision on whether _both pin3 and pin4 are high_ or _only pin4 is high ?

    The reason why I ask this question is because: It is practically impossible to know for sure that two different pins have been pressed at an exact same time; often time the pins are not pressed at an exact same time, and this to me can make a huge difference.

    Again, your expertise will be greatly appreciated.
    thank you.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-04 13:30
    What are you trying to please give an example

    It is practically impossible to know for sure that two different pins have been pressed at an exact same time; often time the pins are not pressed at an exact same time, and this to me can make a huge difference.
    What you could do is something like this 
    
    IF IN0 THEN          '  this button would have to be first 
    IF IN1 THEN          '  this button would have to be seconds for this to work  
    ' Your code here
    ENDIF
    ENDIF
    
    

    Here is the question: Can I force a IF... ELSE statement into another IF... ELSE statement
    IF IN0 THEN                             "What you want to do in your routine if this is ture
    ' Your code 
    ELSE   '..............................................>"What you want to do if not true
    IF IN1 THEN                                          What you want to do  in your routine 
    ' Your code 
    ENDIF
    ENDIF
    
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-04 13:39
    Sir,
    I would like to know the maximum amount of time difference between ( say, the moment pin3 is high and the moment pin4 is high) That will still cause the Basic Stamp to report or sense that both pins are high.

    Thank you in advance.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-03-04 13:41
    This link will point you the right direction. You didn't mention specifically which BS you are using, but the link will give you some ideas.
    http://www.emesystems.com/BS2speed.htm

    If this is an operator pressed pushbutton, I think you are underestimating the speed of the micro. The BS is so fast it will be nearly impossible to push the buttons at the same time.... far as the BS is concerned BUT!! you would write your code in such a way that you "Capture" the event. For example

    main:
    IF IN1 AND IN2 THEN GOSUB simult
    GOTO main

    simult:
    DEBUG "Both buttons pressed!"
    RETURN

    This little routine can be this simple or written as complicated as you need it to be.

    If you press both buttons seemingly at the same time, but to the Stamp it sees a 20us difference between the two.... no matter. When the first button is pressed, the BS sees it, but it runs the check again because they're not both pressed and keeps checking until they are..... 20microseconds later the BS is still checking for button presses and sees both are made which fulfills all conditions. THEN prints "Both buttons pressed!" on your screen.
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-03-04 13:47
    cedtinsi wrote: »
    Sir,
    I would like to know the maximum amount of time difference between ( say, the moment pin3 is high and the moment pin4 is high) That will still cause the Basic Stamp to report or sense that both pins are high.

    Thank you in advance.

    My guess would be 475 + 143 + 475 + 143 microseconds or 1.2ms for the BS2, but that's a guess.


    Edit: Actually I bet it'll be just 475 + 143 microseconds (time for the first IF) but that's still a guess.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-04 13:51
    It dose not work that way the only thing that you can do is one thing at a time
    The only thing that you can do is this IF IN0 AND IN1 THEN " Your code

    If you press both buttons seemingly at the same time, but to the Stamp it sees a 20us difference between the two.... no matter. When the first button is pressed, the BS sees it, but it runs the check again because they're not both pressed and keeps checking until they are..... 20microseconds later the BS is still checking for button presses and sees both are made which fulfills all conditions. THEN prints "Both buttons pressed!" on your screen. <...This the way this works

    Now if you are asking is there a way to see if an input is HIGH for some amount of time and if goes LOW before that amount of time has past then you could use this
    [B]
    chg             PIN         1               '  To input pin
    cntr            VAR        Byte          ' Loop Counter
    
    cntr = cntr + 1 * Chg
    IF (cntr = 100) THEN EXIT                    ' Or your code instead of EXIT 
    
    ' If you use EXIT  the you next code routine would be here
    
    
    [/B]
    

    Now if you ask me if you use two Input ( the code routine above ) in a IF THEN I do not know that this will work for sure I have not try this
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-03-04 13:59
    Um, that was stupid. Sorry Sam is right. I'll edit the code.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-04 14:48
    Dear Sir,

    First and foremost, Thank you for your continuous help.
    Yet I am still concerned.
    Let's say that I have the following code lines in my program.
    IF (IN3 = 1) AND (IN4 = 1) THEN
    HIGH 14
    HIGH 15
    PAUSE 50
    ELSEIF (IN3 = 1) THEN
    HIGH 14
    PAUSE 50
    .
    .
    .
    .
    .

    This is no invention of mine; in fact it can be found at page 78 of the "What is a Microcontroller ?" manual from Parallax.
    My concern is:
    The most chances being that pin3 and pin4 are never high at the same exact time, and assuming that pin3 is the first to be high, and pin4 follows with a delay of, say, 20us; based on the code lines above, can you please tell me what is going to happen?

    Thank you.


  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-03-04 15:09
    If the Basic Stamps see the input 3 first this would the part of the code that would run if you put this in a DO LOOP
    Now if the Basic Stamps see input 4 first it would loop agian
    ELSEIF (IN3 = 1) THEN
    HIGH 14
    PAUSE 50
    

    This why I had this above what would happen is that if you have both button pushed basicly at the same time the top routine would run and if it only see input 3 was high only the it would run the bottom routine
    DO
    
    IF (IN3 = 1) THEN
    PAUSE 100                         ' The PAUSE may not be need 
    IF (IN4 = 1) THEN
    HIGH 14
    HIGH 15
    PAUSE 50
    
    ELSEIF (IN3 = 1) THEN
    HIGH 14
    PAUSE 50
    
    ENDIF
    ENDIF
    
    LOOP
    

    If you put this in a DO LOOP It may not see that both button are pushed
    IF (IN3 = 1) AND (IN4 = 1) THEN
    HIGH 14
    HIGH 15
    PAUSE 50
    ELSEIF (IN3 = 1) THEN
    HIGH 14
    PAUSE 50
    
    ENDIF
    
    
    
  • PublisonPublison Posts: 12,366
    edited 2011-03-04 15:58
    I think I understand the question, although I don't have an answer.

    I believe the question is, if you where to invoke the "IF (IN3 = 1) AND [(IN4 = 1) THEN" statement, what would happen if IN3 equals 1 at the beginning of the statement and IN4 went HIGH 20us after the IF command started processing, would it catch the IN4?

    According to Dr. Tracy, the IF (Branching) command take about 340-450us to complete, if I'm reading them correctly:

    http://www.emesystems.com/BS2speed.htm

    I would think that after the command started processing, the inputs would already been read, so any change during the ~400us would not matter.

    Again, not an answer, but a calculated guess based on information I have looked at..



    Yet I am still concerned.
    Let's say that I have the following code lines in my program.
    IF (IN3 = 1) AND [(IN4 = 1) THEN
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-04 18:17
    Thank you to all of you.
    I do appreciate your outstanding help.

    Mr. Sam_Sam_Sam, I am impressed by the way you find solutions to problems, that is inspiring.
    The code you wrote makes perfect sense to me.

    I am still curious as to know the maximum amount of time delay that can be allowed, between the HIGH of one pin and the HIGH of another pin, such that the Basic Stamp will assume the pins to have been set at HIGH at the same time.

    My curiosity is founded.
    The foundation is that, I read the "What is a Microcontroller?" manual from parallax, and through the activity from page 77-78 , It clearly springs out that there is indeed the existence of such a 'maximum time delay'.
    Having a precise idea of the range of that time delay is my inquiry.

    I rest assure knowing that a help will come soon.

    Thank you.

    IF (IN3=1) AND (IN4=1) THEN
    ____
    ____
  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-04 18:37
    Actually, you are not likely to get that answer. There has been no analysis of the execution time of the individual Stamp Basic operators in a specific context, like the IF statement. The EmeSystems website has a lot of information and you can make some estimates, but that's not what you're asking for. The interpreter code (for the PIC16F57 and the SX microprocessors) is proprietary to Parallax and will not be made public. Parallax does not have the resources to do this kind of analysis nor is there enough need for it to justify the resources. If you need very accurate timing, use a Propeller which has the capability of making these sorts of measurements with a resolution of 12.5ns.
  • PublisonPublison Posts: 12,366
    edited 2011-03-04 18:52
    Mike,

    Could it be assumed, from the above example, that IN3 and IN4 are both tested for, and latched, before the IF is invoked?
    Mike Green wrote: »
    Actually, you are not likely to get that answer. There has been no analysis of the execution time of the individual Stamp Basic operators in a specific context, like the IF statement. The EmeSystems website has a lot of information and you can make some estimates, but that's not what you're asking for. The interpreter code (for the PIC16F57 and the SX microprocessors) is proprietary to Parallax and will not be made public. Parallax does not have the resources to do this kind of analysis nor is there enough need for it to justify the resources. If you need very accurate timing, use a Propeller which has the capability of making these sorts of measurements with a resolution of 12.5ns.
  • cedtinsicedtinsi Posts: 45
    edited 2011-03-04 18:57
    Mr. Green,
    It is a pleasure to know that you are around the corner to share your precious knowledge.
    Sometimes an answer may just be empty; but still I appreciate the awesome job of yours, a valuable part in making this forum what it is.

    Thanks.
Sign In or Register to comment.