Pages: [1]   Go Down
Author Topic: Having a problem with a variable used as a flag  (Read 695 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3407
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Belgium
Offline Offline
Full Member
***
Karma: 5
Posts: 135
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Gear: Arduino- Uno,Due,Ethernet,  OLS, Buspirate, J-Link, TDS1002, Rigol DG1022

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could WHILE statements be used instead of the IFs?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16514
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't know what you expected, but
Code:
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.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3464
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pottstown, PA
Offline Offline
Sr. Member
****
Karma: 4
Posts: 297
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3464
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have a look at while in the reference pages, or understand and fix the current problem
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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();
  }
}
Logged

Pages: [1]   Go Up
Jump to: