Go Down

Topic: Using Flags (Read 4358 times) previous topic - next topic


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:
Code: [Select]

int MinInput=2;
int MaxInput=9;
boolean PrintFlag[8];
void setup()
  for (int i=0;i<8;i++)
    pinMode((i+2), INPUT);

void loop()
  for (int i=MinInput; i<MaxInput+1;i++)
        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.


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! :D


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 :smiley-eek-blue:


I use them all the time.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.



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  :smiley-cool:

Thank you and regards Rainer


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

[gumby]I've got my head stuck in the cupboard[/gumby]

Go Up