Push button on-off

Hallo everybody, could you help me a little? As long as I push a button I want the output pin1 to became HIGH and output pin2 LOW, else LOW and LOW, When I push the button for second time I want pin1 to became LOW and pin2 HIGH, else LOW and LOW. Third time begin cycle again. Any idea? Thanks a lot.

Any idea?

Yeah. Look at the state change detection example. It describes exactly what you are asking about, except that it deals with one output pin. Trivial to make it deal with two pins instead.

That's a tad ambiguous, OP....

Do you mean while the button is pressed the first time the outputs are high/low and when released low/low, followed by another held press low/high and back to low/low on release?

In other words while the button unpressed the output is always low/low, and while it is pressed an odd time, outputs are high/low and low/high during even presses?

If that's right you need to do what PaulS just said to see a new press, and the example includes doing a count so it's easy to test for odd / even, BUT you also need to handle the unpressed case.

(Iff I understood you correctly, OP.)

Thats right, neiklot. A piece of code please?

dragonflex:
Thats right, neiklot. A piece of code please?

If that's right you need to do what PaulS just said

Here's a piece of code:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Fill in the rest with your best try.

if (buttonPushCounter % 1 ) {
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
} else {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
if (buttonPushCounter % 2 ) {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH);
} else {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}

this do not work

dragonflex:
this do not work

No big surprise. There is no setup() and no loop().

Not to mention no digitalRead of the push button pin.
No assigning a value to the undeclared/undefined variable "buttonPushCounter"

const int  buttonPin = 2;    
const int ledPin3 = 8;
const int ledPin4 = 9;          

// Variables will change:
int buttonPushCounter = 0;  
int buttonState = 0;         
int lastButtonState = 0;     
void setup() {
 
  pinMode(buttonPin, INPUT_PULLUP);

  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
 
  Serial.begin(9600);
}


void loop() {
 
  buttonState = digitalRead(buttonPin);


  if (buttonState != lastButtonState) {
   
    if (buttonState == LOW) {
      
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
    
      Serial.println("off");
    }
    
    delay(50);
  }

  lastButtonState = buttonState;

  if (buttonPushCounter % 1 ) {
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
  } else {
   digitalWrite(ledPin3, LOW);
 digitalWrite(ledPin4, LOW);
  }
  if (buttonPushCounter % 2 ) {
  digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
  } else {
   digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
  }
  }[code] 
The code

This makes no sense:

buttonPushCounter % 1

It's always 0, so your code always sets your two outputs low unconditionally.

You want three states, so if you're going to use the % operator, use 3, not 1.

Then add a switch statement based on the result of that modulo calculation with a case for 0, 1 and 2.

  if (buttonPushCounter % 1 ) {
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
  }

What will be the remainder of dividing an int by 1?

That logic counts the new presses and you only do the led switching once you come out of the main if. You need to do your odd/even thing inside the main if where it detects the button press since you said the leds are only on WHILE the button is pressed.

It's too late, where you do it.

And an easy way to test for odd or even is a bitwise and:

if (count & 1)
{ 
    //odd
}
  else
{ 
   //even 
}