Using Flags

Hi:
I want to bring this subject that I took part in the Spanish forum. It may result interesting for some here also. I haven't noticed the use of software flags in the codes I've looked into posted here. I'm not such a regular forum user though, so maybe wrong. Anyways, this person was getting repeated serial monitor print outs of the same event. He wanted only one per event occurrence. I suggested using software flags to prevent recurrence. These Flags were common use back in time when coding in assembly language. For me they are still very useful and allow me to avoid using delay in many instances which I consider almost always undesirable as it delays everything else also as you know. I also use them in some instances with Processing to redirect program flow sometimes.
I would like to post here the code I posted there for your consideration. I put the delay in there, just because he had it in his original code; but I think its not required. The point is that I think the code can read digital inputs without using delays for debouncing in the case keys are used (if only one key is pressed at a time). I haven't tried that physically building the thing though. I would like to hear your opinions on this.
Here is the original post in case you want to read it:
http://forum.arduino.cc/index.php?topic=222441.0

int MinInput=2;
int MaxInput=9;
boolean PrintFlag[8];
void setup()
{
  for (int i=0;i<8;i++)
  {
    PrintFlag[i]=false;
    pinMode((i+2), INPUT);
  }
  Serial.begin(9600);
}

void loop()
{
  for (int i=MinInput; i<MaxInput+1;i++)
  {
    if(digitalRead(i)==HIGH)
    {
      if(PrintFlag[i-MinInput]==false)
      {
        Serial.println("paso "+i);
        PrintFlag[i-MinInput]=true;// Sets the flag so it won't print the next time it finds the input in HIGH.    
        for (int j=MinInput;j<MaxInput+1;j++)
        {
          if((j==i)==false)// Resets all other Flags when anyone gets activated.
          {
            PrintFlag[j-MinInput]=false; 
          }         
        }
      }
       delay(1000);
    }
  }
}

I learned the technique way back when, it may also have been with assembly language. I still use flags and I don't think they're uncommon. First-time flags, flags for ISRs to communicate to the main code, etc. I was [trying] to read Stroustrup recently and he mentions them, so I don't know how much more legitimate the technique could be! :smiley:

I haven't noticed the use of software flags in the codes I've looked into posted here. I'm not such a regular forum user though, so maybe wrong.

Thanks, I just haven't seen them then :fearful:

I use them all the time.

Hi,

I know it is quite late but starting in Arduino and programming some stuff I found this article very interesting.

Yesterday I was able to get out of a Problem using Flags :sunglasses:

Thank you and regards Rainer

Their use is hardly a secret :slight_smile: since even the built-in examples use them. Look for example at StateChangeDetection which uses a flag called lastButtonState.

is there a video on explaining flags and how to use them?

1 Like

tarbear123:
is there a video on explaining flags and how to use them?

Google is your friend.

Try Google (you might have to read a bit) : Programming flags

I use flags in interrupt procedures , where you don’t want too much code anyhow , just to show the interrupt occurred outside of the interrupt routine.
Nice of u to bring this up tho- it’s a good “trick”.