two or more on off buttons program? [solved]

Hi everyone,
i've been having this problem with my arduino uno r3.
I tried to program a push button so that it would become a on/off switch.
I succeeded but right now i have a problem with proramming two or more seperate buttons so that they become an on/off switch and so that they each control a seperate LED.
I tried to make it work this way:

int pbuttonPin = 4;
int relayPin = 5;
int p2buttonPin = 2;
int relay2Pin = 3;

int val = 0; 
int lightON = 0;
int light2ON = 0;
int pushed = 0;
int pushed2 = 0;
int val2 = 0;


void setup() {

  Serial.begin(9600);
  pinMode(pbuttonPin, INPUT_PULLUP); 
  pinMode(relayPin, OUTPUT);
  pinMode(p2buttonPin, INPUT_PULLUP);
  pinMode(relay2Pin, OUTPUT);
}

void loop() {

  val = digitalRead(pbuttonPin);
 
  
  if(val == HIGH && lightON == LOW){

    pushed = 1-pushed;
    delay(100);
  }    
  

  
  lightON = val;

      if(pushed == HIGH){
        Serial.println("Light ON");
        digitalWrite(relayPin, LOW); 
       
      }else{
        Serial.println("Light OFF");
        digitalWrite(relayPin, HIGH);
        
       
   
      
      }
      
      
      
    
    
    
  val2 = digitalRead(p2buttonPin);   
  
  
  if(val2 == HIGH && light2ON == LOW){

    pushed2 = 1-pushed;
    delay(100);
  }   
 
  light2ON = val2;

      if(pushed2 == HIGH){
       Serial.println("Light ON");
       digitalWrite(relay2Pin, LOW); 
        
        
        
      }else{
        Serial.println("Light OFF");
        digitalWrite(relay2Pin, HIGH);
   
      }    


  delay(100);
}

There aren't any error messages and
relayPin works fine but relay2Pin stays on all the time.
Could anybody please help me with this code?
I would greatly aprecciate it.

Thanks in advance.

Of course we like to help you. But without you posting your program and describing the problem(s) that's going to be very hard.

Before you post the program, have a look at How to use the forum in order for you to post is correct and complete.

Although on second thought, cross posting isn't going to get you the credit for help...

    pushed2 = 1-pushed;
    delay(100);

Did you really mean to mix pushed2 and pushed?

Steve

First, a little clean up:

int pbuttonPin = 4;
int relayPin = 5;
int p2buttonPin = 2;
int relay2Pin = 3;

int val = 0;
int lightON = 0;
int light2ON = 0;
int pushed = 0;
int pushed2 = 0;
int val2 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pbuttonPin, INPUT_PULLUP);
  pinMode(relayPin, OUTPUT);
  pinMode(p2buttonPin, INPUT_PULLUP);
  pinMode(relay2Pin, OUTPUT);
}

void loop() {
  val = digitalRead(pbuttonPin);
  if (val == HIGH && lightON == LOW) {
    pushed = 1 - pushed;
    delay(100);
  }
  lightON = val;

  if (pushed == HIGH) {
    Serial.println("Light ON");
    digitalWrite(relayPin, LOW);
  } 
  else {
    Serial.println("Light OFF");
    digitalWrite(relayPin, HIGH);
  }

  val2 = digitalRead(p2buttonPin);
  if (val2 == HIGH && light2ON == LOW) {
    pushed2 = 1 - pushed;
    delay(100);
  }
  light2ON = val2;

  if (pushed2 == HIGH) {
    Serial.println("Light ON");
    digitalWrite(relay2Pin, LOW);
  }
  else {
    Serial.println("Light OFF");
    digitalWrite(relay2Pin, HIGH);
  }

  delay(100);
}
pushed2 = 1-pushed;

Should that not also read pushed2?

It's a great example of why using arrays is easier. Or to at least number EVERY variable, not only the second :wink:

But there are some more flaws in the code. For example, lightON has nothing to do with the light is on or off. It's the last button state. Which is good because you need state change detection.

Little example how it can look if you use Bounce2 to handle the heavy lifting of the buttons and arrays to make it scale:

#include <Bounce2.h>

//you can add pins to RelayPins and ButtonPins and it will automatically scale.
const byte RelayPins[] {5, 3};
const byte NrRelays = sizeof(RelayPins);
const byte ButtonPins[NrRelays] = {4, 2};

Bounce buttons[NrRelays];

void setup(){
  Serial.begin(115200);

  for(byte i = 0; i < NrRelays; i++){
    buttons[i].attach(ButtonPins[i], INPUT_PULLUP);
    pinMode(RelayPins[i], OUTPUT);
  }
}

void loop(){
  for(byte i = 0; i < NrRelays; i++){
    buttons[i].update();
    //if the button became pressed
    if(buttons[i].fell()){
      digitalToggle(RelayPins[i]);

      //print te status
      Serial.print(F("Relay "));
      Serial.print(i);
      if(digitalRead(RelayPins[i])){
        Serial.println(F(" ON"));
      }
      else{
        Serial.println(F(" OFF"));
      }
    }
  }
}

//Just a quick function to toggle a pin
inline void digitalToggle(byte pin){
  digitalWrite(pin, !digitalRead(pin));
}

Thank you guys for replying.
both your answers were right it was that missing 2 at

pushed2 = 1-pushed;

I found it out myself while looking over the program for the 3994999 time.
It certainly was one of those little tiny mistakes that mess up the whole program.
im happy you guys read my program better than me.

Thanks again,

Josmu30