Email works, but returns a false value and keeps sending emails over and over

This is my first post and I've spent some considerable time trying to figure out where I'm going wrong. I have hooked up a potentiometer to my Uno with an Ethernet Shield. The goal of the code is to list the value of the potentiometer in the serial monitor and send an email when the value of the potentiometer exceeds 100. The email should also tell the value of the pot. An email is indeed being sent, but here are the problems:

  1. The email is sent with a pot value of 0, no matter what the actual value is.

  2. The email is sent over and over again, no matter if the pot is turned down below 100.

  3. After the email is sent, the value of the pot is no longer listed in the serial monitor.

My code is as follows:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 177 };    
byte dns_server[] = {192, 168, 2, 1 };
byte gateway[] = {192, 168, 2, 1 };
IPAddress subnet( 255, 255, 255, 0 );

int potPin = 3;  
int value;

char server[] = "smtp.cox.net";

EthernetClient client;

void setup()
{
  Serial.begin(9600);

    Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  delay(2000);
}

void loop()
{
  int value = analogRead(potPin);
Serial.println(value);
delay (500);
  
while(value > 100)
  {
      if(sendEmail()) Serial.println(F("Email sent"));
      else Serial.println(F("Email failed"));
  }}


byte sendEmail()
{

  byte thisByte = 0;
  byte respCode;

  if(client.connect(server,25)) { 
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;
  Serial.println(F("Sending helo"));

  client.println(F("helo 64.180.213.215"));

  if(!eRcv()) return 0;
  Serial.println(F("Sending From"));

  client.println(F("MAIL From: <email@email.com>"));

  if(!eRcv()) return 0;

  Serial.println(F("Sending To"));
  client.println(F("RCPT To: <anyemail@email.com>"));

  if(!eRcv()) return 0;

  Serial.println(F("Sending DATA"));
  client.println(F("DATA"));

  if(!eRcv()) return 0;

  Serial.println(F("Sending email"));

  client.println(F("To: You <anyemail@email.com>"));

  client.println(F("From: Your Sensor <email@email.com>"));

  client.println(F("Subject: Sensor Alert\r\n"));

  client.println(F("The value of the sensor is"));
  delay (50);      // THIS IS TRYING TO CORRECT THE 0 BEING RETURNED 
client.println(value);

  client.println(F("."));

  if(!eRcv()) return 0;

  Serial.println(F("Sending QUIT"));
  client.println(F("QUIT"));
  delay (60000);//      THIS IS TRYING TO CORRECT THE MULTIPLE EMAILS
  

  if(!eRcv()) return 0;

  client.stop();

  Serial.println(F("disconnected"));

  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while(!client.available()) {
    delay(1);
    loopCount++;

  
    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }

  respCode = client.peek();

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
     }

  return 1;
}

void efail()
{
  byte thisByte = 0;
  int loopCount = 0;

  client.println(F("QUIT"));

  while(!client.available()) {
    delay(1);
    loopCount++;

    if(loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return;
    }
    
  }

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();

  Serial.println(F("disconnected"));
  
}

Any help is greatly appreciated.

  while(value > 100)
  {
    if(sendEmail()) Serial.println(F("Email sent"));
    else Serial.println(F("Email failed"));
  }

The variable "value" does not change in that loop, so naturally it will send an awful lot of emails.

sail2steam:

  1. The email is sent with a pot value of 0, no matter what the actual value is.
int value;

...

void loop()
{
  int value = analogRead(potPin);

These two "value" variables are different. Lose the "int" from in front of the second one, ie.

void loop()
{
  value = analogRead(potPin);

Nick, this completely solved my problems. Thanks for your help!