Help with sending emails on alarm function

Hi all I am new to the Ardunio world only been playing with it now for a few days and I’m stuck.

I am building a (pump running) event alarm systems that will email me when power is applied to my pump. (the pump only runs when i have a leak, and thats bad)

Anyway, I have successfully built the system using the Arduino Dou and the ethernet shield and a relay switch that when power is applied the switchPin is set to HIGH.
Then on swithPin = HIGH it send me an email via the ethernet shield thru my smtp server.

The problem I am havign is that as long as the power is applied to the pump the switch is HIGH and I get emails every second until I remove power and the switch goes LOW.
How can I have it send one email only then wait say fifteen minutes before sending another email?

Here is my code so far: Thanks in advance for the help!

#include <SPI.h>
#include <Ethernet.h>
#define switchPin 7  // connected to relay switch.

byte mac[] = { 0x40, 0xG2, 0xDY, 0x00, 0xT5, 0x0D }; // mac address
char serverName[] = ""; // My SMTP server
EthernetClient client;

void setup(){
  Serial.println("Message to serial monitor to send email on alarm"); // Message so I can watch in the serial monitor
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // if this failed check connections and dhcp server 
  // show your IP address: assigned to ethershield
  Serial.print("Arduino IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
  pinMode(switchPin, INPUT);

void loop()
  byte inChar;
  inChar = (digitalRead(switchPin)); // if switch contact is closed
  if(inChar == HIGH)
    if(sendEmail()) Serial.println("Email sent"); //start sendEmail()
    else Serial.println("Email failed");

//////Begining of sendEmail function

byte sendEmail()  //sendEmail() function that returns 1 or 0

  //start and check for email server connection
  if (client.connect(serverName, 25)) { 
    Serial.println("connected, sending email...");
  else {
    Serial.println("connection failed");
    return 0; //send 0 (failed) back to sendEmail() function
  //wait for server "queing" response
  while(!client.available()) delay(1000);

  client.println("EHLO canyousendmymail"); /*hello (statement after helo is needed but irrelevant)*/

  //wait for server "hello" response
  while(!client.available()) delay(1000);
  client.println("AUTH LOGIN"); delay(1000); // authorise
client.println(""); delay(1000); // enter your username below
// and password here
client.println("Password"); delay(1000);

  client.println("MAIL From:"); // identify sender, this should be the same as the smtp server you are using*/

  //wait for server "sender ok" response
  while(!client.available()) delay(1000);

  client.println("RCPT To:"); /* identify recipients email address */

  //wait for server "receipent ok" response
  while(!client.available()) delay(1000);


  //wait for server to say "enter your message" response
  while(!client.available()) delay(1000);
  //send email message to server
  client.println("To:"); /* identify recipient */
  client.println("Subject: ALERT!ALERT! Arduino Mail!!"); /* insert subject */
  client.println("The Pump system is RUNNING!!!"); /* insert body */
  client.println("."); /* end email */

  //wait for server "message accepted" response
  while(!client.available()) delay(1000);

  client.println("QUIT"); /* terminate connection */
  //wait for server "goodby" response
  while(!client.available()) delay(1000);

  //stop client connection
  return 1; //send 1 (success) back to sendEmail() function


You need to detect when the switch goes HIGH, not just that the switch IS HIGH.

To do that, you need to store the previous state of the switch, and compare the current state to the previous state. When the states are not the same, a transition has occurred. You can then look at the current state to see which transition (to HIGH or to LOW) occurred.

I'm not following you. Wouldn't I still get the condition of HIGH and it will still keep sending email?

I'm not following you.

Imagine you're standing in a dark room. I switch on the light. You shout "The light is on" The light remains on.

Do you keep on shouting "The light is on" ? No, because you noted the difference between "light not on" and "light on", not the continuous condition "light is on"

If you need example code, take a look at the StateChangeDetection example.

Thanks guys for pointing me in the right direction. I finally got it. I even got it to send different emails with alarm on and alarm off.