Push button counter !!! Please help wit the code !!!

Hi Im trying to do a simple light control using my andruino ...

when I push the button once it should switch the first light bulb on
when pushed the second time it switches the second light bulb on and the firtst off
when pushed the third time it switches both of them off.

got problems with getting the count how many times i pushed the button to work
can somebody help with the code please.

if (btuttonw1StateCounter  == 1) {
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
} 
else
{
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
}
if (btuttonw1StateCounter  == 2) {
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
} 
else
{
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
}
if (btuttonw1StateCounter == 3) {
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
} 

 }

sorry wrong code :-[
that’s the one I’m working on

const int ledPin1 = 12;
const int ledPin2 = 8;
const byte w1 = 2;
const byte w2 = 7;
byte buttonStatew1 = 0;
byte buttonStatew2 = 0;
byte lastButtonw1State = LOW;
byte lastButtonw2State = LOW;
byte btuttonw1StateCounter = 0;
byte btuttonw2StateCounter = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(w1, INPUT);
  pinMode(w2, INPUT);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
 buttonStatew1 = digitalRead(w1);
 buttonStatew2 = digitalRead(w2);
 
 if (buttonStatew1 && buttonStatew1 != lastButtonw1State) {
   if (btuttonw1StateCounter < 3){
     btuttonw1StateCounter +=1 ; }
    else
   { btuttonw1StateCounter =0;}
   
 lastButtonw1State =  buttonStatew1; }
   
 Serial.println(btuttonw1StateCounter);
 
if (btuttonw1StateCounter  == 1) {
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
} 
else
{
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
}
if (btuttonw1StateCounter  == 2) {
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
} 
else
{
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
}
if (btuttonw1StateCounter == 3) {
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);
} 

 }

What problems are you having?

Why have you got that else code on the first two 'if' statements? It is negating anything you do on the 'if' == 1

the problem is it dose not count …

when i switch the andruino on the serial monitor shows “0” first push of the button it changes to 1
and stay as 1 no matter how many times the button is pushed

can’t get what you mean with the else why it is negating ?

i based it on that code bellow i fund in other post and that one works …

const byte Button = A0;

byte ButtonState;
byte lastState = LOW;
byte count = 0;

void setup() {
  
  Serial.begin(9600);
  
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  // button latch, no debounce needed!!!!!
  {
    if(count < 9) // This will check to see if the count is within a range of 0 - 9, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      count += 1; // same as count = count + 1;
    else
      count = 0;
    Serial.print("The Score is: ");
    Serial.println(count);
  } 
  lastState = ButtonState;

  if    (count == 0)
{ 
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
}
else if(count == 1)
{ 
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
else if(count == 2)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 1);
}
else if(count == 3)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
}
else if(count == 4)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 5)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 6)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 7)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
else if(count == 8)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 9)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
// 
}

okay it is working now :grin:

if (buttonStatew1 && buttonStatew1 != lastButtonw1State) {
if (btuttonw1StateCounter < 3) {
btuttonw1StateCounter +=1 ; }
else

{btuttonw1StateCounter =0;}
}

lastButtonw1State = buttonStatew1;

Serial.println(btuttonw1StateCounter);

to many brackets there …

can't get what you mean with the else why it is negating ?

Because in that code in reply #1 what ever happens when
btuttonw1StateCounter == 1
is totally undone when
btuttonw1StateCounter == 2

Because of the else statements.