Buttons

im trying to write some code which allows a 2 buttons to turn on 2relays

this is the code ive written my self, but its not working 100% hopefully someone can point out where im going wrong, im new to c++ but id like to think im on the right path

int relay1 = 2; // relay power point 1
int relay2 = 3; // relay power point 2
int start = 4; // start button
int limit1 = 5; // limit switch
int limit2 = 6; // limit switch 2
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
 


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);  // initialize serial communication
pinMode(relay1, OUTPUT);  // set relay1 as output
pinMode(relay2, OUTPUT); // set relay as output
pinMode(start, INPUT); // set start button as input
pinMode(limit1, INPUT_PULLUP); // set limit1 switch as pullup
pinMode(limit2, INPUT_PULLUP); // set limit2 switch as pullup

}

void loop() {
  // put your main code here, to run repeatedly:
buttonState = digitalRead(relay1); // read which input the button is stored on
if (buttonState != lastButtonState) { // compare the buttonState to its previous state
    
    if (buttonState == HIGH) { // if the state has changed, increment the counter & if the current state is HIGH then the button went from off to on:
       buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      Serial.println("off");       // if the current state is LOW then the button went from on to off:
    }
  }
  lastButtonState = buttonState;   // save the current state as the last state, for next time through the loop

 
  // turns on the LED every two button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(relay1, LOW); // turn led off
  } else {
    digitalWrite(relay1, HIGH);

  }


}

what i want to know is how i can use 2 buttons to turn on & off a relay each,

button 1 / relay 1
button 2 / relay 2

the way i thought i could do it is as follows

button1

buttonState = digitalRead(switch1); // read which input the button is stored on
if (buttonState != lastButtonState) { // compare the buttonState to its previous state
    
    if (buttonState == HIGH) { // if the state has changed, increment the counter & if the current state is HIGH then the button went from off to on:
       buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      Serial.println("off");       // if the current state is LOW then the button went from on to off:
    }
  }
  lastButtonState = buttonState;   // save the current state as the last state, for next time through the loop

 
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledRed1, LOW); // turn led off
  } else {
   digitalWrite(ledRed1, HIGH); // turn led on
  }

button2

buttonState2 = digitalRead(switch2); // read which input the button is stored on
if (buttonState2 != lastButtonState2) { // compare the buttonState to its previous state
    
    if (buttonState2 == HIGH) { // if the state has changed, increment the counter & if the current state is HIGH then the button went from off to on:
       buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      Serial.println("off");       // if the current state is LOW then the button went from on to off:
    }
  }
  lastButtonState2 = buttonState2;   // save the current state as the last state, for next time through the loop

 
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(ledRed1, LOW); // turn led off
  } else {
   digitalWrite(ledRed1, HIGH); // turn led on

  }

hopefully this makes more sence

What's the question?

Is this your code?
Does it work?

ive updated the 1st topic, lol... someone came into the room whilst i was writing and i got distracted.

Hi,

In the example, the input pin is pulled down,

but in your code yours are pulled up.

int relay1 = 2; // relay power point 1
int relay2 = 3; // relay power point 2
int start = 4; // start button
int limit1 = 5; // limit switch
int limit2 = 6; // limit switch 2
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
 


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);  // initialize serial communication
pinMode(relay1, OUTPUT);  // set relay1 as output
pinMode(relay2, OUTPUT); // set relay as output
pinMode(start, INPUT); // set start button as input
pinMode(limit1, INPUT_PULLUP); // set limit1 switch as pullup
pinMode(limit2, INPUT_PULLUP); // set limit2 switch as pullup

}

void loop() {
  // put your main code here, to run repeatedly:
buttonState = digitalRead(relay1); // read which input the button is stored on
if (buttonState != lastButtonState) { // compare the buttonState to its previous state
    
    if (buttonState == HIGH) { // if the state has changed, increment the counter & if the current state is HIGH then the button went from off to on:
       buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      Serial.println("off");       // if the current state is LOW then the button went from on to off:
    }
  }
  lastButtonState = buttonState;   // save the current state as the last state, for next time through the loop

 
  // turns on the LED every two button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    digitalWrite(relay1, LOW); // turn led off
  } else {
    digitalWrite(relay1, HIGH);

  }


}

Now you may look at the original code and correct your mistakes.

Pullup means button state is high.
pulldown means the opposite.