Go Down

Topic: Having a problem with a variable used as a flag (Read 1 time) previous topic - next topic

Hi - I am having problems using a flag in my sketch to switch from going forward to going backward (programming a robot).  Essentially, my code is:
------------------------------------------
int direction = 0; //flag that tells my direction

void setup()
{
}

void loop()
{
if (direction == 0)
{
<<do some stuff>>
direction = 1;
}

if (direction == 1)
{
<<do some stuff>>
direction = 0;
}
}
---------------------------

The problem is, it never gets to the second part of the program.  By using Serial.println, I confirm the it does set direction = 1, but then it immediately gets set back to 0.  What I am wondering is if somehow it is the original declaration of the variable, at the beginning, which is constantly setting it back to zero before the second IF statement gets run.  Any ideas?

Thanks for your help!

wildbill

Looks fine, so for better help, you'll need to post your actual sketch.

astrofrostbyte

There is always the small change that your system 'crashes' and resets when you do something in that second loop,  maybe the powersupply i loaded too much by a load you activate in the second loop.
Gear: Arduino- Uno,Due,Ethernet,  OLS, Buspirate, J-Link, TDS1002, Rigol DG1022

Could WHILE statements be used instead of the IFs?

lloyddean

Perhaps more concerning the robots movement systems would help with a more useful response?

retrolefty

Put a serial print line statement in your setup function like "starting sketch" and monitor the sketch running in your serial monitor. If you see the setup function getting called over and over you know you are likely having a reset condition being forced on your board, possibly from something drawing too much current? If not you will have to post your complete sketch to see what might be happening.

Lefty


spatula

Don't know what you expected, but
Code: [Select]

void loop()
{
if (direction == 0) // this is the initial condition, so true
{
 direction = 1; // this assignment makes the following condition true
}
if (direction == 1) // so this will be true as well
{
 direction = 0; // this restores the initial condition
}


I would expect whatever action to be done and undone very often.

UKHeliBob

An else instead of the second if would stop the direction immediately flipping back of course, but it will still change back on the next pass through loop() unless action is taken to prevent it.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

scottyjr

I tried using direction as a variable in a sketch of mine and the IDE tagged it as a keyword. - Scotty

sev1962

As far as I can see the Routine is performing exactly the way it should.

I sets the flag to one.  But does not skip the next check,  which now evaluates to true and resets it to 0
I would suggest an If else


if(Flag==1)
{ (do Something);
    Flag=0;
} else
{ (do Something);
   Flag=1;
}


PaulS

Quote
I tried using direction as a variable in a sketch of mine and the IDE tagged it as a keyword. - Scotty

The color of a piece of text on the screen is completely irrelevant.

Thanks for the tips!

I have another way to attack this - can I have two looping functions?

basically, I would want one function to continue looping until a condition is met, then jump to another function and loop it.

That would work, I just am not sure how to make a second `loop`function.

UKHeliBob

Have a look at while in the reference pages, or understand and fix the current problem
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

spatula


basically, I would want one function to continue looping until a condition is met, then jump to another function and loop it.


Yes, you need to define a condition, you didn't state it and we have been guessing what it could be. And no, you don't need another loop:

Code: [Select]

void loop()
{
  if (condition_is_met)
  {
    flag_to_do_other_function = true;
  }
  else // maybe you need another condition to return to the normal state?
  {
  }

  if (flag_to_do_other_function)
  {
    do_other_function();
  }
  else
  {
    do_normal_function();
  }
}

Go Up