Thread: Question about variable's value being changed

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

    Question about variable's value being changed

    Hello,

    I have a basic question about setting a value to a certain variable, and then having the program change or update that value as it is executed. The program is pretty simple, it is a switch that monitors another variable that decides when to turn on and off. It is shown below.

    Code:
     main( VOID )
    {
      if ( vssAD_RLYD2 == 1 )
      {
        if ( vssAD_VAC < 550 )
        {
          vssAD_RLYD2 = 1;
        }
        else
        {
          vssAD_RLYD2 = 0;
        }
      }
      else
      {
        if ( vssAD_VAC <= 450 )
        {
          vssAD_RLYD2 = 1;
        }
        else
        {
          vssAD_RLYD2 = 0;
        }
      }
    }
    The program first checks to see if vssAD_RLYD2 = 1 (on). If it does equal 1, then it checks if the value of vssAD_VAC is < 550. If it is true, then it keeps vssAD_RLYD2 equal to 1. If it is false, it changes vssAD_RLYD2 to 0 (off).

    If in the beginning vssAD_RLYD2 does NOT equal 1 (=0), then it checks if vssAD_VAC is <= 450. If this is true, then it changes vssAD_RLYD2 to 1. If it is false, it keeps vssAD_RLYD2 at 0.

    After vssAD_RLYD2 is changed to whichever gets executed, it will start over by checking to see if vssAD_RLYD2 = 1.
    ---------------------------------------------------------------------------------------------
    So, the variable vssAD_RLYD2 is the one that it needs to check for in the beginning (if its 0 or 1) then throughout the rest of the program, if certain statements are true, it needs to change that value of vssAD_RLYD2, and it will stay that value until it is changed again (like an on off switch)

    I am not a computer science engineer, so that is why I am here asking for help. Hopefully this makes sense, if not let me know and I will explain what I can.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're asking how to start over, you need to use a loop.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Yes, I need it to start over using the value of vssAD_RLYD2 that it got changed to during the program.

    Which type of loop would be best to change this to?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Presumably this will run forever in some embedded system. Many people use a simple
    Code:
    while (1) {
        if (vssAD_RLYD2 == 1)
        ...
    }

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Ok, so would it be as simple as just replacing the first IF statement with a WHILE loop statement like this...?




    Code:
    main( VOID )
    {
      while ( vssAD_RLYD2 = 1 )
      {
        if ( vssAD_VAC < 550 )
        {
          vssAD_RLYD2 = 1;
        }
        else
        {
          vssAD_RLYD2 = 0;
        }
      }
    }
    {
      while ( vssAD_RLYD2 = 0 )
      {
        if ( vssAD_VAC <= 450 )
        {
          vssAD_RLYD2 = 1;
        }
        else
        {
          vssAD_RLYD2 = 0;
        }
      }
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nope. For one, there is a difference between = and ==. For two, you need to put everything you wish to repeat inside the loop. If you put things you wish to repeat outside the loop, they will not repeat. Right now (assuming you change = to ==), the first loop will execute so long as your variable is 1. Then your second loop will execute so long as your variable is 0. And then that's it. Once your variable goes back to 1 again, you have no way of getting back up.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Ok, so if I used my original code and set up the while loop before it, then it should be ok...?



    Code:
    main( VOID )
    
    x = 1
    while ( x == 1 )
    {
      if ( vssAD_RLYD2 == 1 )
      {
        if ( vssAD_VAC < 550 )
        {
          vssAD_RLYD2 = 1;
        }
        else
        {
          vssAD_RLYD2 = 0;
        }
      }
      else
      {
        if ( vssAD_VAC <= 450 )
        {
          vssAD_RLYD2 = 1;
        }
        else
        {
          vssAD_RLYD2 = 0;
        }
      }
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Right. (Assuming you put a semicolon after x=1.) (Also there's no real need for the variable x; you can just do "while (1)".)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about initializing variables.
    By brack in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2010, 06:56 AM
  2. simple question about variables and loops
    By InvariantLoop in forum C Programming
    Replies: 2
    Last Post: 01-26-2005, 09:47 AM
  3. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  4. Quick Question regarding Contructor Variables
    By cram in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2004, 08:29 PM
  5. Data Storage Question, and Dynamic variables?
    By Zeusbwr in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2004, 11:01 PM

Tags for this Thread