Double check switch state. Help !

HI,

I am trying to build a SMS sender(via websire) for backup to a pager for a rescue team. It works too well !

example: pager has a relay in charging base which closes a switch for 5 secs when pager goes off. It is connected to the arduino digital pin. Ethernet shield then visits a URL (GET REQUEST)

Problem is its too sensitive and the arduino sometimes sends a message by itself. I think I have it debounced correctly.

I was hoping to test for the switch closed, then pause for 2 seconds then test again if it is still closed before sending the message.

Could somebody help me with that. It would be much appreciated.

Here is what I have working. - but too sensitive. (Using a 1k pull down resistor)

#include <Ethernet.h>

byte mac[] = { 55E, 05, 055E, 055F, 55FE, 55D };
byte ip[] = { 15, 555, 555, 555 };
byte gateway[] = { 555, 555, 5, 555 };
byte subnet[] = { 555, 55, 555,555 };
byte server[] = { 555, 555, 555, 555 }; // These changed for security reasons

Client client(server, 80);

void setup()
{
pinMode(4, INPUT);
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
delay(1000);

Serial.println("initialized!");
}

int f = 0;
int prevactivation = 0;
long time = 0;

#define DEBOUNCE 60000

void loop()
{

f = digitalRead(4);
if( (prevactivation == 0) && (f == 1) && (millis() - time > DEBOUNCE) ) {
time = millis();
sendUpdate();
}

prevactivation = f;

}

/// Functions ////

void sendUpdate()
{

Serial.print("connecting... ");

if (client.connect()) {

Serial.println("connected");
client.println("GET /~tothephpscript.php?pass='password'");
client.println();
Serial.println("message sent!");
client.stop();
} else {
Serial.println("connection failed");
}

}

void fetchUpdate()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}

while(!client.connected()) {
client.stop();
}

}

I was hoping to test for the switch closed, then pause for 2 seconds then test again if it is still closed before sending the message.

#define DEBOUNCE 60000

Your time to ignore changes is set to 1 minute (60000 milliseconds).

Nevertheless it works a little bit :slight_smile:
The reason is that "time" is a left over from the last successful activation. This is not how debouncing (or deglitching) works....

I remember two threads with better algorithms; both use just counts, not millis(); but there are many ways...