break void function when the condition is met

in the code below a small program represent a push button when i shot once it sent 1 pulse for a variable pushbuttoncounter the counter will be = 1 it turns on a led when i shot again it will send a second pulse to pushbuttoncounter the counter will be= 2 , but it does not exit void function.
how to break void function when the condition is met.

const int  buttonPin = 2;        
int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;     
int leddo=12;
int ledPin=13;
void setup() {
  pinMode(buttonPin,INPUT);
  pinMode(leddo,OUTPUT);
  pinMode(ledPin,OUTPUT);

}

void loop1() {digitalWrite(leddo,HIGH);
}
void loop2(){digitalWrite(ledPin,HIGH);
buttonPushCounter=0;}
void loop(){
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
      
    } 
    delay(50);
  }
  
  lastButtonState = buttonState;

  if (buttonPushCounter == 1) {
loop1();
  }
  else if(buttonPushCounter ==2){
    loop2();}
  }

return

2 Likes

MarkT:
return

Do you mean in void loop or void function ?

What do you want to break aka return from... :wink:

septillion:
What do you want to break aka return from... :wink:

i want to break loop1()

If you want to exit a function of return type void, use the return statement (without a value).

Alaxwora:
i want to break loop1()

loop1 just has a single call to digitalWrite, so this makes no sense.

Alaxwora:
but it does not exit void function.

Your incorrect use of language is making this very confusing.

When you say void loop in Reply #2 that should be the function loop(). The word void just tells the compiler that the function does not return any value.

The confusion then comes from your use of the term void function because the names of the other functions in your program are loop1() and loop2(). You don't (thank goodness) have any function named "function".

The net result is that I do not understand which of your functions is not behaving the way you want.

...R

learn to format your code correctly.

Try like this:

const int  buttonPin = 2;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int leddo = 12;
int ledPin = 13;


void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(leddo, OUTPUT);
  pinMode(ledPin, OUTPUT);

}

void loop1() {
  digitalWrite(leddo, HIGH);
}

void loop2() {
  digitalWrite(ledPin, HIGH);
  buttonPushCounter = 0;
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
    }
    delay(50);
  } else {
    return;
  }
  lastButtonState = buttonState;
  if (buttonPushCounter == 1) {
    loop1();
  }
  else if (buttonPushCounter == 2) {
    loop2();
  }
}

thanks in advance.
But for me the problem is not in the code formatting ,the problem when the counter=2 the 2nd led turns on while the 1st one still working . i just need the first led turns off once the 2nd led turns on( counter=2)
hope you understand me ;(..

Alaxwora:
But for me the problem is not in the code formatting

You do not seem to have noticed Reply #7

And formatting code so it is easy to read can often help YOU to find errors. It also makes it a great deal easier for other people to help you.

...R

Do you have a pulldown resistor(10k) on pin 2?
Where do you turn the LEDs LOW?

outsider:
Do you have a pulldown resistor(10k) on pin 2?
Where do you turn the LEDs LOW?

yes ,there is a 10k resistor parallel with push button

I give up.

...R