Multiway switching / corridor ligths control

Hi there.
I was looking for some projects about multiway switching with arduino but could not find any.
I have two traditional wall switches which can output On or Off state.
There is no problem if there was only one push button but I have two normal wall switches (in the future I would like to add web control) which I would like to control one light from multiple locations.

How could you do that?
Can you modify those two codes into one allowing one wall switch cancelling another?

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2.
 
 
 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {    
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
/*
  State change detection (edge detection)
   
 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
 
 This example shows how to detect when a button or button changes from off to on
 and on to off.
   
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)
 
 created  27 Sep 2005
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.
   
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
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() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

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

 
  // 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 % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}

If I understand you correctly, it sounds like you currently have two standard light switches that can either be in the on or off position, giving you a potential of four different combinations of switches right (on on, on off, off on, off off)? I'm not sure how you would even control this using a web interface, since the switches are physically in the on or off position (unless you have a mechanical way of changing the position). What's the desired result for each of the four combinations?
May I suggest checking out this very good set of tutorials (although maybe over the top for someone like me) SuperHouseTV #1: Home Automation System Architecture 2012 - YouTube
This guy goes through some theory and a lot of brainstorming and pitfalls to home automation infrastructure. Maybe this will help you on your quest!

Yes, you do have four different combinations but I thought you could just use != lastButtonState
and somehow modify the code so each change of the switch or the button on the website would change the light.

// read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;

I am still learning and trying to figure it out.
I have seen SuperHouseTV many times (all episodes) in the past and even wrote a message to him about the PHP post function to and from Arduino but he did not respond.

Well if you have two "traditional wall switches" that you are connecting to the Arduino, then that's a little different than having a momentary push button at either point. But you're right, you'd have to track the last state of each switch, then if you detect a change toggle the light.

// pseudocode
lightState = off;
switch1State = digitalRead(switch1);
switch2State = digitalRead(switch2);

function loop() {
    if ((digitalRead(switch1) != switch1State) || (digitalRead(switch2) != switch2State)) {
        // then toggle the light and store the current switch states
        if (lightState == off) {
            lightState = on;
        }
        else {
            lightState = off;
        }

        switch1State = digitalRead(switch1);
        switch2State = digitalRead(switch2);
    }
}

A simpler approach, if you need the same functionality as what is already existing, would be to turn the light on/off based on the known switch states (if both switches are off, or both are on, then the light should be off, otherwise if they are both switched to the opposite position, then the light should be on):

function loop() {
    if digitalread(switch1) != digitalread(switch2) {
        //turn light on
    }
    else {
        //turn light off
    }
}

If you are using momentary push buttons instead of toggle switches, you can do the same thing, but instead of tracking the button states, you can just track a button press (if button is pressed) and don't forget to debounce in that case. I'm not sure if debouncing is necessary for this type of switch.

Anyone, please correct me if my logic is flawed.

In my opinion, the easiest thing to do would be to add a spring to both switches. If it's possible that is.
I don't know what kind of switches you have but most of them we (in Sweden) use it's possible to add a spring.

Like this: https://support.elot.se/forum/attachments/hide-lite/1631d1327566117-vad-aer-en-aterfjaedrande-tryckknapp-montering-fjaeder-s90-och-s100.jpg

I use it www.wiore.commyself. Works well.
Best regards

Thank you linucksrox and LarryXz. I am still waiting for my Arduino to arrive.
The only thing that I could say to swera is: Shame on you, you spammer.
I have checked all your posts and in all of them you have the same message about the expensive system that we could make here more practical and cheaper...

FasterThanX:
Hi there.
I was looking for some projects about multiway switching with arduino but could not find any.
I have two traditional wall switches which can output On or Off state.
There is no problem if there was only one push button but I have two normal wall switches (in the future I would like to add web control) which I would like to control one light from multiple locations.

How could you do that?
Can you modify those two codes into one allowing one wall switch cancelling another?

You need to have a handle on how multi switch lighting works to understand how to wire this up with an arduino - essentially (if i understand correctly) you want to have the two wall switches still in place and be able to turn the light either on or off as before and you then want to add the arduino as a third switch again toggling the switch will either turn the lights on or off - dependent on their current state.

If you have a look here you will see the way that there are a number of key components.

Once you understand this it is trivial to put in the correct relay to an arduino so it also becomes part of the circuit.

Craig