Running a loop once with a button push.

Hey everyone,

I am VERY new when it comes to programming, and need help with a project.

I am trying to flash an LED just once when I hit a button, and not strobe as it runs the loop over and over again. I would like it to wait before the button is released, and then pressed again before it flashes the LED again. Just have it run through the loop ONE time with each press of the button. How would I go about this?

I am trying to make the first if statement run ONCE with “TRIGGER” pressed and “SELECTOR” not pressed, and the second if statement to run over and over while “SELECTOR” is pressed and “TRIGGER” is pressed. Right now it will just repeat both over and over. As I said, I would like the first if statement to just run once with each press.

void loop() {
if (digitalRead(TRIGGER) == HIGH)
if (digitalRead(SELECTOR) == LOW){
digitalWrite(MAIN, HIGH);
delay(NS);
digitalWrite(MAIN,LOW);
delay(NP);
}
if (digitalRead(TRIGGER) == HIGH)
if (digitalRead(SELECTOR) == HIGH){
digitalWrite(MAIN, HIGH);
delay(NS);
digitalWrite(MAIN,LOW);
delay(NP);
}}

Sorry if this doesn’t make sense. As I said, I am completely new to this, and only know what I have read. :confused:

I would like it to wait before the button is released, and then pressed again before it flashes the LED again. Just have it run through the loop ONE time with each press of the button. How would I go about this?

There is an example called state change detection that does exactly what you want.

Thanks a ton! It's funny how a simple solution solved my issue. I would have been fiddling for DAYS. Thanks again!