Shop OBEX P1 Docs P2 Docs Learn Events
one part of code is working while the equivalent is not (BUTTON / IF...THEN) — Parallax Forums

one part of code is working while the equivalent is not (BUTTON / IF...THEN)

SebastienSebastien Posts: 7
edited 2005-08-26 11:56 in BASIC Stamp
A very strange problem that I have. I don't understand anything.

I have a moving object that can be detected (and thus stopped ) by a reflective sensor (I exposed my machine in a recent thread (http://forums.parallax.com/showthread.php?p=547608).

When I use a BUTTON function to detect the changing of state of my sensor I encounter no problem, but when I use IF...THEN or DO...WHILE/UNTIL...LOOP functions there's a problem.

common code part :
'main pins

 main_ENG_CLK PIN 4 'Clock engine
 main_ENG_ENA PIN 5 'Enable engine
 main_ENG_ROT PIN 6 'way of rotation
'+ other pins declared
 
'aux pins

 aux_sensor PIN 4 'reflective sensor

'+other pins declared


that part works fine :
main:
      BUTTON aux_sensor,1,0,0,workspace,1,program        'if sensor = 1 goto "program" 
      [noparse][[/noparse]...]
 
program:
'bla bla bla, the code stops the engine...



but that one is NOT working, whereas it is an equivalent coding (well, I think it is). Even when my object is static in front of my sensor (I know my sensor is working fine btw) and when "program" is supposed to be executed I'm falling into the "[noparse][[/noparse]...]" code part.

main:
      IF (aux_sensor = 1) THEN program 
      [noparse][[/noparse]...]
 
program:
'bla bla bla


Even this is not working, I'm entering in an infinite loop.
main:
       DO
       [noparse][[/noparse]...]
       UNTIL aux_sensor 'until = 1
 
program:
[noparse][[/noparse]...]


I have conducted many different tests so as to understand what is the matter. In the end, I only come with these code lines. On one hand it's OK, on the other hand it's NOK.

Anybody got an idea ?

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-25 13:13
    Are you leaving out an AUXIO instruction somewhere?

    You should be able to detect the pin status and see it change with this:

    Test:
    · AUXIO
    · DO
    ··· DEBUG HOME, BIN1 Aux_Sensor
    · LOOP

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • SebastienSebastien Posts: 7
    edited 2005-08-25 14:03
    Sorry, I forgot to write the AUXIO function here, but I didn't forgot it in my code.
    When doing my 2 tests I'm strictly in the same conditions, nothing else is modified.

    first test :
             AUXIO
             BUTTON aux_sensor,1,0,0,workspace,1,program
    
    


    => OK

    second test :
             AUXIO
             IF (aux_sensor = 1) THEN program
    
    


    => NOK

    Sadly, I'm not able to use the debug mode for the moment. My PC and my machine are not next to each other.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-25 15:04
    There has got to be something more to it -- you're specifying a downstate of 1 and looking for 1 in your other example; clearly the BS2p can see the 1. If you have a serial LCD you could connect it as a debugging tool to use in the shop enviroment to help you sort this out.

    Just a thought: how long is that input low? BUTTON is sitting there waiting while the code in a loop is loading and reloading instructions -- a very quick low input could be missed.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • ManuelManuel Posts: 105
    edited 2005-08-25 18:50
    I have a project involving the DO..LOOP functions and it works perfectly. Maybe your not getting enugh voltage from your sensor. I had that problem and used a NAND chip...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Best Regards

    Manuel C. Reinhard
  • SebastienSebastien Posts: 7
    edited 2005-08-26 08:43
    WOW ! This is VERY strange.
    I am now able to use the DEBUG function with my PC. Thus, I verified the states of my sensor, depending on the position of my object.

    If I use the debug loop you gave me Jon, the state of my sensor is never changing, but still the BUTTON function works whereas the IF...THEN doesn't.
    Now, here comes the strange part :
    If I write a BUTTON command line before (just before or at the very first beginning of my program, just after declarations) the debug test function, then the sensor behave the right way : 0 by default, 1 when the object is present. That means also that the IF...THEN function is now functionnal. I don't know if I'm clear so :

    AUXIO
    IF (aux_sensor = 1) THEN program
    
    


    => NOK

    AUXIO
    BUTTON aux_sensor,1,0,0,workspace,1,void_function 'nevermind the function, I don't want it to be executed
    
     
    [noparse][[/noparse]...] 'nevermind the quantity of code lines
     
    AUXIO
    IF (aux_sensor = 1) THEN program
    
    


    => OK !! Here the IF...THEN function works ! Very weird.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-26 11:56
    What I suspect is that the output from the sensor is not static; it's probably a very short pulse that can be captured by BUTTON but gets missed between instruction loads when using the othe code. The only way to verify that would be to connect a scope.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.