Go Down

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

robotcanuck

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

robotcanuck

Could WHILE statements be used instead of the IFs?

lloyddean

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

Go Up