Thread: is there an if/else function that implements IMMEDIATELY?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28

    is there an if/else function that implements IMMEDIATELY?

    I'm in the middle of righting a program for a servo motor. I'd like to use an if and else to have the motor exit the main program with a pin going low and I'd like it to do it immediately.

    Right now I have it as an

    Code:
    if ...
    {
    whole bunch of for loops.....
    ...
    ...
    ...
    }
    
    else...
    {
    single command.
    }

    and it's working but the problem is that it doesn't exit out of the if statement's code immediately and that's what I'm after. Right now it's running the code in the if statement from start to finish. I'd rather have it jump to the else immediately and NOT finish the if's code.


    Is there a good way to pull that off?
    Last edited by 777funk; 11-23-2010 at 03:46 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably want a goto.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    One thing I didn't think of....

    while loop

    Would that do what I'm thinking?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want an immediate response you need to get your pin test inside the loops. Depending on the parts of the code you didn't show us a while() or do while() may work for you...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You either need to do a whole bunch of this:
    Code:
    while( ... && !BAILOUT )
    {
        while( ... && !BAILOUT )
        {
            while( ... && !BAILOUT )
            ...
            if( BAILOUT ) break;
        }
        if( BAILOUT ) break;
    }
    Or you just goto BAILOUT; from wherever you need to.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    What about a while loop wrapping that one, important, if?

    Code:
    . . . 
    while (expected common condition exists) {
    
    if Previous-Important-If (test) {
    Response
    Response
    }
    /* Ending the while loop which holds the important IF */ } Other, less important IFs, from the long sequence of IFs before . . .
    Examples of this would be in K&R's C in Chapter 1. There were a bunch of getchar/putchar examples with counting and reporting and replacement that used this in the book.
    Last edited by agxphoto; 11-23-2010 at 04:26 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Here's what I've got as the main bones of my code. I tried the Goto and bailout and it isn't working for some reason. It is not entering the while loop until the condition is met but once it's in, it doesn't leave.

    Code:
    void setup()
    {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
      pinMode(relaypin, INPUT);
    }
     
    
    void loop()  // Program begins here.  
    {
      // read the state of the relay value (checks pin 2 for 5V to see if winder motor is on)  
      relaystate=digitalRead(relaypin);
    
    
    while (relaystate == HIGH)
    {
    // if Relay for the motor is high (5V) then run the pickup program)
      
    if (relaystate == LOW){ goto bailout;}
    
    
    
    
    
      for(pos = (minimum-p*1.3); pos >= maximum+p; pos -= posinc)  
      {                                  
        myservo.write(pos);             
        delay(4);                       
      } 
      
      for(pos = maximum+p; pos<(minimum-p*1.3); pos+= posinc)  /// moving from right to left (i.e. 9 to 146 degrees) 
      {                                
        myservo.write(pos);          
        delay(4);                      
      }
    }
    
    }
    
    
    
      for(pos = 75; pos < slowleft; pos+=1)  
      {                                
        myservo.write(pos);          
        delay(23);                      
      }// transition 
    
    
    }
    
    bailout:
     pos = slowleft; pos+=1;  
                                      
        myservo.write(pos);          
        delay(23);                      
      // goes to slowleft (left side of the pickup) if the winder motor is off.
    
    }

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What the hell is going on with the curly braces? Your indentation has you completely screwed up I think. Or at least it has me all screwed up.

    It looks like your loop() function ends shortly (3 braces) after the delay(4); statement and all the code after that is in non-function limbo land.

    Maybe re-indent your code in a sane way and you'll be able to see what's really going on.
    If you understand what you're doing, you're not learning anything.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your indentation is terrible.
    Code:
    void loop()  // Program begins here.  
    {
        // read the state of the relay value (checks pin 2 for 5V to see if winder motor is on)  
        relaystate=digitalRead(relaypin);
    
        while (relaystate == HIGH)
        {
            // if Relay for the motor is high (5V) then run the pickup program)
            if (relaystate == LOW){ goto bailout;}
    
            for(pos = (minimum-p*1.3); pos >= maximum+p; pos -= posinc)  
            {                                  
                myservo.write(pos);             
                delay(4);                       
            } 
      
            for(pos = maximum+p; pos<(minimum-p*1.3); pos+= posinc)  /// moving from right to left (i.e. 9 to 146 degrees) 
            {                                
                myservo.write(pos);          
                delay(4);                      
            }
        }
    }
     What's all this?
      for(pos = 75; pos < slowleft; pos+=1)  
      {                                
        myservo.write(pos);          
        delay(23);                      
      }// transition 
    
    }
    
    bailout:
     pos = slowleft; pos+=1;  
                                      
        myservo.write(pos);          
        delay(23);                      
      // goes to slowleft (left side of the pickup) if the winder motor is off.
    
    }
    It doesn't reach your goto because it's not in any actual block of code. But, since you're only exiting one loop anyway, why don't you just break?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    That's not all of my code. It's pretty long so the braces may not all add up properly. I cut out most of the code just to give the idea.

    I'm sure the indentation is horrible. It's a result of a bunch of cut and pastes. The main program is doing what I need it to so far.


    by the way I'm not a programmer... I'm a wannabe who's a EE. I got a C in my C++ course. I'm definitely a hack. This is my first real crack at a program I've cared about. Cleaning up will be important if I do this again!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    You either need to do a whole bunch of this:
    Code:
    while( ... && !BAILOUT )
    {
        while( ... && !BAILOUT )
        {
            while( ... && !BAILOUT )
            ...
            if( BAILOUT ) break;
        }
        if( BAILOUT ) break;
    }
    Or you just goto BAILOUT; from wherever you need to.


    Quzah.
    goto is pretty much a last resort these days.
    There are so many other ways of exiting loops it should never be necessary...
    I think most would agree it's to be avoided.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Anyone have an example of how I would do this with a while?

    My digital input trigger to start the while loop is working but it's going into the loop and never exiting. I guess that means (based on what you guys have said) that it's not reading while the loop is running.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    goto is pretty much a last resort these days.
    There are so many other ways of exiting loops it should never be necessary...
    I think most would agree it's to be avoided.
    /golfclap

    He specifically mentioned a "whole bunch of for loops". If you want a simple way to get out of a bunch of nested loops in a single jump, a goto is the clearest option. Otherwise, if they're not nested, then he just needs to wrap them all in a single check, as we've already said.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > by the way I'm not a programmer... I'm a wannabe who's a EE
    Then you must have heard of state machines right?

    Some pseudo code
    Code:
    state = RUNNING;
    while ( state != DONE ) {
      if ( digitalRead(relaypin) )
        state = BAILOUT
      switch ( state )
        case MOVE_LEFT
          // bump motor one step left
          // if goal reached, state = MOVE_RIGHT
        case MOVE_RIGHT
          // bump motor one step right
          // if goal reached, state = DONE
        case BAILOUT
          // bump motor one step wherever
          // if goal reached, state = DONE
        // end case
       delay()
    }
    The code doesn't "get stuck" in some deeply nested loop. There is just one loop with a state, and you decide what to do in each state, and when to move to a new state.


    > it doesn't leave
    Code:
      relaystate=digitalRead(relaypin);
    
    
    while (relaystate == HIGH)
    {
    // if Relay for the motor is high (5V) then run the pickup program)
      
    if (relaystate == LOW){ goto bailout;}
    It doesn't leave because you never call digitalRead(relaypin); inside the loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Ok, now I have it in the while loop:

    Code:
    while (relaystate == HIGH)
    {
    // if Relay for the motor is high (5V) then run the pickup program)
      
        
     pinMode(relaypin, INPUT);
      // read the state of the relay value (checks pin 2 for 5V to see if winder motor is on)  
      relaystate=digitalRead(relaypin);
    
    ....
    Did this now and it will not start until it gets the HIGH signal but still runs through the entire while loop before stopping. I am really stuck on how to get it to exit the while loop with a realtime reading of the pin as it goes LOW.
    Last edited by 777funk; 11-24-2010 at 01:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM