Run If Statement Once, Until it comes true again - 1st Day (Be Easy)

(FIRST POST - 1ST 24 HOURS WITH MY ARDUINO UNO)

Ok. A quick background on me.
I specialize in Home Automation and install/program High End Residential & Commercial using Control4.
So I'm not new to programming, just new to Arduino Programming :sunglasses:

I could certainly accomplish this in Control4 (which of course is completely different from what we are talking about), but I'm wondering how to accomplish this on an Arduino Uno. As everyone knows the beauty of the Arduino is that it can accomplish basic tasks at a fraction of the price of other controllers.

I see that people on these forums can be pretty nasty in the beginning to people who don't speak Arduino Lingo, so I ask, please take it easy on me at first.

My example is just going to be an example, which I would like to use over and over again for different applications using the Arduino.

For this example I'm going to flip a switch to the off position (no continuity to ground) and have an LED blink 3 times. Once that LED Blinks 3 times I want it to stop. Then once I flip that switch to the On position (back to ground) have the Arduino ready to look at the first IF statement again in regards to when the switch flips to the off position. Like a Security Sensor contact, only make a Buzz sound once, until its closed and opened again etc....

Hope I write this properly for you. I tried to cut out all the fat so I don't get confused and my example makes sense.

const int ledPin = 13;
const int Switch = 5;
int SwitchPosition = 0;

void setup() {
  //I presume that this "setup location" only means that the Arduino will run this once during the inatial boot Correct?
pinMode(ledPin, OUTPUT);
pinMode(Switch, INPUT);
}
void loop() {
  // THIS IS WHERE I JUST WANT THE LED TO BLINK 3 TIMES WHEN THE SWITCH IS "HIGH"  THEN STOP, UNTIL THE SWITCH IS FLIPPED LOW THEN HIGH AGAIN
  SwitchPosition = digitalRead(Switch);

if (SwitchPosition ==HIGH) {
  //blink the LED 3 Times.... Again only want it 3 times, and not to repeat until triggered again.  
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    delay(150);
    digitalWrite(ledPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  
    delay(150);
    digitalWrite(ledPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  
    delay(150);
    digitalWrite(ledPin, LOW);
    }
    else {
          // turn LED off:
    digitalWrite(ledPin, LOW);
    }
}

Please let me know if you have ANY questions.

Thanks in advance,

DJM

Is your complaint that it keeps flashing if you hold down or keep the switch on? If so, you just have a "debouncing" issue. You want to test for a low-to-high transition of the switch, rather than just its current state. You already have a variable to keep track of the last known state of the switch, which is a prerequisite for debouncing. You just need to test it at the beginning of the loop to make sure it's not already set. If that confuses you, you can research switch debouncing, which is a richly explored area of knowledge.

aarg:
Is your complaint that it keeps flashing if you hold down or keep the switch on? If so, you just have a "debouncing" issue. You want to test for a low-to-high transition of the switch, rather than just its current state. You already have a variable to keep track of the last known state of the switch, which is a prerequisite for debouncing. You just need to test it at the beginning of the loop to make sure it's not already set. If that confuses you, you can research switch debouncing, which is a richly explored area of knowledge.

Debouncing is an entirely different issue, and what you describe (simply testing for a low-to-high transition) does not solve switch bouncing.

christop:
Debouncing is an entirely different issue, and what you describe (simply testing for a low-to-high transition) does not solve switch bouncing.

My bad, that's right. I really need another coffee. Right now he doesn't need it, since he's waiting quite a while before reading the switch again. That is actually the crudest form of debounce. If he develops the program to the point where the LED flashes happen after the button push is recognized (i.e. the command is sent immediately, which would usually be a good design), he may have to deal with debounce, so it's a good thing for him to be aware of.

aarg:
Is your complaint that it keeps flashing if you hold down or keep the switch on? If so, you just have a "debouncing" issue. You want to test for a low-to-high transition of the switch, rather than just its current state. You already have a variable to keep track of the last known state of the switch, which is a prerequisite for debouncing. You just need to test it at the beginning of the loop to make sure it's not already set. If that confuses you, you can research switch debouncing, which is a richly explored area of knowledge.

Yes my complaint would be that it keeps flashing until I flip the switch back ON (continuity to ground). I will 100% look into debouncing.

and of course I may even answer my own question after using Arduino for a period of time. But I would like to ask Christop what he means when he says that debouncing won't solve my problem. Sad part is I'm responding before researching it... Yes.. Newb...

After running this test with a few more if statements involved, please correct me if I'm wrong (question within a question), but if one, "IF" statement loop is true, or (HIGH) will the loop disregard any following "IF" statements within the loop?

Hopefully Debouncing is the answer I'm looking for. I will certainly look into it.

If anyone else has any input I'm very eager to learn.

Thanks.

Just wait for the switch to go LOW after the flash sequence...

doFlashingStuff();
while (digitalRead(switch) == HIGH);  // keep looping if switch is still HIGH

So if the button is still pressed at the end of the flashing sequence, it will just wait for you to release the button.

And you don't need the else statement.

But if you're using a switch which is LOW when on, then you need to reverse the logic: HIGH=off, LOW=on, so swapping LOWs to HIGHs and vice versa will be needed.

Yes, the while loop is a perfect solution.

Love the answer. And I'm sure it will work perfect :slight_smile:

But... My NEWBISM... So I'm X-ing the "IF" statement and moving to a "while" statement within the loop, (which of course I will look up)... How would this example look as a whole?

I suppose I don't get the "doFlashingStuff();"

Keep the IF statement but just add the while statement where you've got the else statement.
Have you got a pull-up resistor on your switch? A floating input will give all sorts of problems.

const int ledPin = 13;
const int Switch = 5;
int SwitchPosition = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(Switch, INPUT_PULLUP); // <<< needed if no external pullup
}

void loop() {

  SwitchPosition = digitalRead(Switch);

if (SwitchPosition ==HIGH) {
  //blink the LED 3 Times.... Again only want it 3 times, and not to repeat until triggered again.  
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    delay(150);
    digitalWrite(ledPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  
    delay(150);
    digitalWrite(ledPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH);  
    delay(150);
    digitalWrite(ledPin, LOW);

    while (digitalRead(Switch)==HIGH); // do nothing until state changes

    }
}

Not tested, but it looks OK.

Martin-X:
Keep the IF statement but just add the while statement where you've got the else statement.
Have you got a pull-up resistor on your switch? A floating input will give all sorts of problems.

const int ledPin = 13;

const int Switch = 5;
int SwitchPosition = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(Switch, INPUT_PULLUP); // <<< needed if no external pullup
}

void loop() {

SwitchPosition = digitalRead(Switch);

if (SwitchPosition ==HIGH) {
  //blink the LED 3 Times.... Again only want it 3 times, and not to repeat until triggered again. 
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
    delay(150);
    digitalWrite(ledPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH); 
    delay(150);
    digitalWrite(ledPin, LOW);
    delay(150);
    digitalWrite(ledPin, HIGH); 
    delay(150);
    digitalWrite(ledPin, LOW);

while (digitalRead(Switch)==HIGH); // do nothing until state changes

}
}




Not tested, but it looks OK.

Perfect! This is exactly what I asked for. Looking forward to learning more about this platform.
The DJ"M" stands for Martin... So Thanks Martin-X!