Thread: How to implement this condition..

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    How to implement this condition..

    Hello,

    I need to implement the following condition in to my program which runs in 10ms cycles:

    if (FLAG)F_SW == ON, then nothing needs to happen until F_SW turns OFF, in which I then want it to toggle a different flag.

    I am not certain how to set this up by using IF statements, which is basically all I use since I am not a very good programmer. So I would like to know what the best way to write this out would be? Hopefully it is fairly simple, THANKS!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's wrong with if ((FLAG)F_SW == ON), then?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    My original idea was this:

    Code:
    if ((FLAG)F_SW == ON)
    {
         if ((FLAG)F_SW != ON)
         {
              (FLAG)F_SW_2 = ON;
         }
    }
    I thought that because F_SW can't be both ON and OFF, the condition would never be recognized and it would never work. Am I wrong on this?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If F_SW is volatile (e.g., comes from a hardware switch) then it could change value from one line to the next. The next question is: why not just ditch the outer if statement?

    (You may also want to look into the idea of a "loop", if that's what you're trying to do.)

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    I can't ditch the outer if statement because I only want the second flag (F_SW_2) to trigger after the first flag (F_SW) has been on and then now turned off. Then the second flag will only stay on for a certain amount of time using a counter.

    It is a flag from a hardware switch, so I suppose I will try the current idea that I have posted above, assuming that the value can change from one line to the next. Thank you for your time!

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It sounds like you're trying to track a change of state from "on" to "off", rather than just what state the switch is in. Presumably, there is some loop in your code that keeps running through all these things and checking the state of F_SW. What you need is to keep track of the current state and last state:
    Code:
    FLAG last_state, curr_state;
    last_state = OFF  // initialize to avoid hitting our condition on the first loop
    loop
        curr_state = read switch
        if last_state == ON and curr_state == OFF  // switch went from ON to OFF
            do something

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Yes I think that is exactly what I was looking for. I think I understand your idea, I will try to implement it in to my code. Thanks alot!!

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Ok, I am trying to run this program now, but I am not sure how to set the last_state to equal the previous state. Here is what I have:

    Code:
    FLAG last_state, curr_state;
    last_state = OFF
    
    loop
        curr_state = read switch
        if last_state == ON  &&  curr_state == OFF
             F_SW = ON
    I am trying to put ( last_state = read switch ) at the beginning of the program before the loop. Will this work? Or else how do I get it to work?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't want the last_state to equal the read switch -- you want the current state to be what the switch is. You want the last state to be the old value of current state.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    I understand that last_state is equal to the old value of current state. I do not know how to set last_state equal to the old value.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dswartz2 View Post
    I understand that last_state is equal to the old value of current state. I do not know how to set last_state equal to the old value.
    What's wrong with an equal sign?
    Code:
    last_state = curr_state;

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dswartz2 View Post
    I understand that last_state is equal to the old value of current state. I do not know how to set last_state equal to the old value.
    Code:
    if (switch activated)
     old_state = current_state
     current_state = switch value

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if condition
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 05:25 AM
  2. If condition
    By rits in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 05:54 AM
  3. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  4. What should I Implement next?
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2006, 11:37 AM
  5. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 09:44 PM

Tags for this Thread