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