how to add debounce code to pushingbox code

Hi All,

I am using pushingbox code (posted below) to notify me when my doorbell has been pressed

I have the two wires from the door bell button going to the 5v pin and the other to 3rd input pin with a 10K resistor between GND and the 3rd pin.

This all works and the code sends the info to the pushing box servers.

but am having a problem with false triggers to the code i would assume its interference from another cable nearby

how would I go about adding the debounce library to this code Arduino Playground - HomePage

any help would be much appreciated

////
//
// General code from http://www.pushingbox.com for Arduino + Ethernet Shield (official) v1.2
//
////

#include <SPI.h>
#include <Ethernet.h>

  /////////////////
 // MODIFY HERE //
/////////////////
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x19 }; // Be sure this address is unique in your network

//Your secret DevID from PushingBox.com. You can use multiple DevID on multiple Pin if you want
char DEVID1[] = "Your_DevID_Here"; //Scenario : "The mailbox is open"

//Numeric Pin where you connect your switch
uint8_t pinDevid1 = 3; // Example : the mailbox switch is connect to the Pin 3

// Debug mode
boolean DEBUG = true;
  //////////////
 // End //
//////////////


char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false; // Save the last state of the Pin for DEVID1
boolean lastConnected = false; // State of the connection last time through the main loop


// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  Serial.begin(9600);
  pinMode(pinDevid1, INPUT);
  
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  else{
    Serial.println("Ethernet ready");
    // print the Ethernet board/shield's IP address:
    Serial.print("My IP address: ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop()
{
      ////
      // Listening for the pinDevid1 state
      ////
      if (digitalRead(pinDevid1) == HIGH && pinDevid1State == false) // switch on pinDevid1 is ON
      {
        if(DEBUG){Serial.println("pinDevid1 is HIGH");}
        pinDevid1State = true;
        //Sending request to PushingBox when the pin is HIGHT
        sendToPushingBox(DEVID1);
      }
       if (digitalRead(pinDevid1) == LOW && pinDevid1State == true) // switch on pinDevid1 is OFF
      {
        if(DEBUG){Serial.println("pinDevid1 is LOW");}
        pinDevid1State = false;
        //Sending request to PushingBox when the pin is LOW
        //sendToPushingBox(DEVID1); //Here you can run an other scenario by creating a DEVID2 variable
      }
      
      
      //DEBUG part
      // this write the respons from PushingBox Server.
      // You should see a "200 OK"
      if (client.available()) {
        char c = client.read();
        if(DEBUG){Serial.print(c);}
      }
      
      // if there's no net connection, but there was one last time
      // through the loop, then stop the client:
      if (!client.connected() && lastConnected) {
        if(DEBUG){Serial.println();}
        if(DEBUG){Serial.println("disconnecting.");}
        client.stop();
      }
      lastConnected = client.connected();
}


//Function for sending the request to PushingBox
void sendToPushingBox(char devid[]){
  client.stop();
  if(DEBUG){Serial.println("connecting...");}

  if (client.connect(serverName, 80)) {
    if(DEBUG){Serial.println("connected");}

    if(DEBUG){Serial.println("sendind request");}
    client.print("GET /pushingbox?devid=");
    client.print(devid);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(serverName);
    client.println("User-Agent: Arduino");
    client.println();
  }
  else {
    if(DEBUG){Serial.println("connection failed");}
  }
}

May be I do not fully understand your circuit, but why do you use external resistor?
Best method is using internal pull-up resistors. You can do this by writing HIGH to the input.

pinMode(KEY, INPUT);
digitalWrite(KEY, HIGH);

In this case you have to connect the key to GND and to the input pin. The logic is inverted in this case, meaning the key is activated when the input gets low.

It is no good idea to make it opposite and pull the input down to gnd with a resistor and connect the switch to VCC and input!
The reason is the very low threshold for the low signal you are generating with the resistor at the input with an open input. It is ony 0.8V and it will generally lead to false events.

Thanks for your reply

if I remove the 10k resistor do i have one wire to pin 3 and one to GND or do I link 3 and GND together and the other wire to 5v

then do i just change digitalRead(pinDevid1) to digitalwrite(pinDevid1)

Or have i got this totally wrong?

  if (digitalRead(pinDevid1) == HIGH && pinDevid1State == false) // switch on pinDevid1 is ON
      {
        if(DEBUG){Serial.println("pinDevid1 is HIGH");}
        pinDevid1State = true;
        //Sending request to PushingBox when the pin is HIGH
        sendToPushingBox(DEVID1);
      }
       if (digitalRead(pinDevid1) == LOW && pinDevid1State == true) // switch on pinDevid1 is OFF
      {
        if(DEBUG){Serial.println("pinDevid1 is LOW");}
        pinDevid1State = false;
        //Sending request to PushingBox when the pin is LOW
        //sendToPushingBox(DEVID1); //Here you can run an other scenario by creating a DEVID2 variable
      }

You change your code in setup() to

pinMode(KEY, INPUT);  
digitalWrite(KEY, HIGH);

this initializes the pin as an input with a 20k ohm internal pullup resistor. This holds the pin HIGH unless something acts upon it.
You connect the button from the pin to ground.

Thus, the pin is held HIGH until the button is pressed -- then it goes LOW.

I didn't read your code but find where you are reading the pin (be sure to check for LOW now, instead of HIGH) with the digitalRead() and then add delay(100) to wait 1/10th of a second and then check that it is still HIGH. That will usually be enough.

Try this code. It should lit the onboard LED when pressing the key at pin 3...

#include <Bounce.h>
// search for this lib and read the instructions...
// to have a comfortable debouncing and key reading

#define KEY 3
#define LED 13


Bounce key = Bounce( KEY, 50 ); //Debounce for 50ms


void setup() {                

  pinMode(KEY, INPUT);  
  digitalWrite(KEY, HIGH);

  pinMode(LED,OUTPUT);  

}

void loop() {

  processKey();
  
}  

void processKey(){
  key.update(); // Bounce check for key status
  if ( key.fallingEdge())  // key pressed
    {
    digitalWrite(LED,HIGH); //LED on
    }
  else
    digitalWrite(LED,LOW); //LED off
     
  }

Thanks for the info both

I have done as you said and removed the 10k resistor and use the built in one with the digitalWrite(KEY, HIGH); this works great no false triggers yet and I also implemented the debounce code as well.

Thank you both for your help

hi another newbie facing the same false triggers problem for pushing box code. please can you post the full modified code which solved your problem.

Thanks in advance

Have a look here:

http://forum.arduino.cc/index.php?topic=197621.msg1461635#msg1461635

please check and help

You have the hardware. You have the pushing box account. Guess who gets to check that code? It isn't us.

Guess who gets to describe what help is needed. It isn't us.