Wireless Switching for Light Switch and Garage Door Opener

I am working on a project using an arduino to control a relay to activate a device over the web ultimately to open my garage door with my cell phone. The plan at this point is to host the wireless network and php based control site on a raspberry pi. At this stage I am working on getting the Arduino switching portion working and so far am having decent success based off of a couple of example codes that allow me to turn on an LED (baby steps) either with a push button or a serial signal. The problem I am having is controling the momentary switch so that when pressed once regardless of the length of the press it switches the light on or off. At this point I am using the delay function, but that is a bandaid.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into
int ledState;          //a variable to store the LED state in
int buttonState;       // a variable to store the button state in
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
  delay (175);
    // read the state of the LED
  ledState = digitalRead(ledPin); 
  //read the sate of the pushbutton
  buttonState = digitalRead(buttonPin);
  // see if there's incoming serial data:
  
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    {
    if (incomingByte == '0') {
      if (ledState == HIGH){
      Serial.println("Off");
     digitalWrite(ledPin, LOW);
  }
    // otherwise turn it on (if it is off)
     else {
    // turn LED off:
      Serial.println("On");
      digitalWrite(ledPin, HIGH); 
    }
  }}}
 if (buttonState == HIGH) {
   // if the LED is on turn it off
   if (ledState == HIGH)
   {
     digitalWrite(ledPin, LOW);
     Serial.println("Off");
   }
   //if the led is of turn it on
   else {
     digitalWrite(ledPin, HIGH);
     Serial.println("On");
   }
}
    }

I apologize for the messy code I am new at this and learning please point any errors or simplifications that can be made. I also cut out the comments giving credit to the people who wrote the example code for the purpose of brevity.

Hi,

A couple of things to try. Firstly, you might check out something on the topic of debouncing so that you get a solid and accurate read of your switch. This video from Jeremy Blum explains a simple way (and why): (youtube)

To get over the situation you’re trying to work around, you could store a global variable for the current state of the button, and only send your signal if it changes. Initialise that variable with a debounced read of your switch pin in the init() function and then on you’ll be fine.

Cheers ! Geoff

To get over the situation you're trying to work around, you could store a global variable for the current state of the button, and only send your signal if it changes.

Actually, the global variable should contain the previous state. A local variable with the current state is sufficient. Or the previous state variable can be local and static.

In any case, the idea is to do something only if the current state is not equal to the previous state (the switch is pressed and was not, or the switch is not pressed but was).

Once you determine that a state change occurred, you can then determine whether it was to-pressed or to-released.

Be sure to set the previous state to the current state at the end of loop (or when done using the previous state).

The debouncing makes sense as I was having issues with that when I was building a basic circuit using a relay, any chance I could get an example of how to create a variable that only changes when the state of the switch changes, I am very new to programming. Thanks!

any chance I could get an example of how to create a variable that only changes when the state of the switch changes

Here's how to detect that the switch state has changed:

int currState;
int prevState = HIGH; // Assumes pullup resistor so LOW is pressed
int switchPin =  2; // Change as appropriate

void loop()
{
  currState = digitalRead(switchPin);
  if(currState != prevState)
  {
     // A transition occurred
     if(currState == LOW)
     {
        // To pressed
     }
  }
  prevState = currState;
}

Where it says // To pressed, you'd put your code to count presses.

After the previous equals current assignment, put the rest of your code to deal with the number of presses.

OK after watching that video I have cleaned up my code alot, commented it to death, and everything works flawlessly now on the wireless connection, and the webserver stuff thanks everyone for your help this is alot of fun!

My Current Code

/* Compiled By Alex Kayl 11-18-2012 with help from examples and the arduino
forum none of this is my code per say but was pieced together from examples
in from the arduino program as well as as found online a special thanks to 
this tutorial http://www.youtube.com/watch?v=_LCCGFSMOr4&feature=youtu.be
which helped solve my debouncing and blinking problems!
 */
int buttonPin = 2;     // the number of the pushbutton pin
int ledPin = 13;       // the pin that the switched device is attached to
int incomingByte;      // a variable to read incoming serial data into
boolean ledState;          //a variable to store the LED state in
boolean currentButton;  // a variable to store the button state in
boolean lastButton;  //a variable to store the last button state
void setup() 
{

  Serial.begin(9600);   // initialize serial communication:
  pinMode(ledPin, OUTPUT);    // initialize the LED pin as an output:
  pinMode(buttonPin, INPUT);  // ser button pin as input
}

boolean debounce(boolean last)                 //deboucne button (removes erratic readings from button press)
  boolean current = digitalRead(buttonPin);    // set current button variable to current button position
  if (last != current)                         // if it has changed store current position in current variable
  {
    delay(5);                                  // this is the part that allows the button to stabilize to eliminate bouncing
    current = digitalRead(buttonPin);         // store the bow debounced button position as current
  }
  return current;
}

void loop() {
  currentButton = debounce(lastButton);          // reads the current button state
  ledState = digitalRead(ledPin);                // read the state of the LED:
  
  
  if (Serial.available() > 0) {                  // see if there's incoming serial data:
    incomingByte = Serial.read();               // read the oldest byte in the serial buffer:
    {
    if (incomingByte == '0') {                 // if read value is 0 switch LEDState
     ledState = !ledState;
      Serial.println(ledState);               //return the current LED value to serial port
     }
  }
}
  digitalWrite(ledPin, ledState);   // write the ledstate as returned by serial port function
  
  
  currentButton = debounce(lastButton);          // run your debounce program from above
  if (lastButton == LOW && currentButton == HIGH) // if the button has been pressed (changed state from off to on)
  {
    ledState = !ledState;                        // change the led state
    Serial.println(ledState);                    // print the current ledstate to the serial port
  }
  lastButton = currentButton;                   // set the last button state
  
  digitalWrite(ledPin, ledState);               // switch the LED


}