Go Down

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

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;
}


Go Up