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