Help with simple project

Hello im kinda new to arduino programming and i ran into a problem with this program. im sure you people can help me.

Im making a program to measure a temp sensor and use that temp value to control a relay.

The temp sensor is working fine but i get a error when trying to use the relay.

I have attached project

thanks in advance

Leon

Temp.ino (717 Bytes)

See the comments at the error locations.

Please read the how to use this forum-please read stickies to see how to post code. If you have compile errors, post the entire error message.

This compiles without errors:

int ThermistorPin = 0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
const int pump = 2; //pump relay

int pstart = 45.00;
int pstop = 40.00;

void setup()
{
   Serial.begin(9600);
   pinMode (pump, OUTPUT);

}

void loop()
{
   {
      Vo = analogRead(ThermistorPin);
      R2 = R1 * (1023.0 / (float)Vo - 1.0);
      logR2 = log(R2);
      T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
      Tc = T - 273.15;
      
      Serial.print("Temperature: ");
      Serial.print(Tc);
      Serial.println(" C");
   }
   if (Tc > pstart )    // stray comma
   {
      digitalWrite(pump, LOW);
   }
   else if (Tc < pstop )    // stray comma
   {
      digitalWrite(pump, HIGH);
   }
   delay(500);
}

Thank you so much:)

groundFungus:
See the comments at the error locations.

Please read the how to use this forum-please read stickies to see how to post code. If you have compile errors, post the entire error message.

This compiles without errors:

int ThermistorPin = 0;

int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
const int pump = 2; //pump relay

int pstart = 45.00;
int pstop = 40.00;

void setup()
{
  Serial.begin(9600);
  pinMode (pump, OUTPUT);

}

void loop()
{
  {
      Vo = analogRead(ThermistorPin);
      R2 = R1 * (1023.0 / (float)Vo - 1.0);
      logR2 = log(R2);
      T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
      Tc = T - 273.15;
     
      Serial.print("Temperature: ");
      Serial.print(Tc);
      Serial.println(" C");
  }
  if (Tc > pstart )    // stray comma
  {
      digitalWrite(pump, LOW);
  }
  else if (Tc < pstop )    // stray comma
  {
      digitalWrite(pump, HIGH);
  }
  delay(500);
}

Btw is it possible to set a time so the temp needs to be over something like 30 degrees for 2 seconds before the relay turns on?

groundFungus:
See the comments at the error locations.

Please read the how to use this forum-please read stickies to see how to post code. If you have compile errors, post the entire error message.

This compiles without errors:

int ThermistorPin = 0;

int Vo;
float R1 = 10000;
float logR2, R2, T, Tc;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
const int pump = 2; //pump relay

int pstart = 45.00;
int pstop = 40.00;

void setup()
{
  Serial.begin(9600);
  pinMode (pump, OUTPUT);

}

void loop()
{
  {
      Vo = analogRead(ThermistorPin);
      R2 = R1 * (1023.0 / (float)Vo - 1.0);
      logR2 = log(R2);
      T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
      Tc = T - 273.15;
     
      Serial.print("Temperature: ");
      Serial.print(Tc);
      Serial.println(" C");
  }
  if (Tc > pstart )    // stray comma
  {
      digitalWrite(pump, LOW);
  }
  else if (Tc < pstop )    // stray comma
  {
      digitalWrite(pump, HIGH);
  }
  delay(500);
}

Yes, refer to the beginners guide to millis() and the several things at a time tutorial to see how to use millis() (and micros()) for timing.

Here is an illustration of setting up a timer and resetting the timer when temp is less than 512* so that the relay (led in this code) will not actuate till 3 seconds after temp reaches 512. Once temp drops below 512 the LED (relay) turns off.

*I don't have a temp sensor or relay handy so I used a pot and LED to test the code.

const byte analogInPin = A0;
const byte ledPin = 4;

unsigned long timer = 0;
unsigned long tempDelay = 3000; // 3 seconds
unsigned int temp = 0;
unsigned int tempThreshold = 512;

void setup()
{
   Serial.begin(115200); 
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH); //common anode led off  
}

void loop()
{
  if(millis() - timer >= tempDelay)
  {
    // if it gets here temp is ovet 512 for 3 seconds
    digitalWrite(ledPin, LOW);
  }
  else
  {
    digitalWrite(ledPin, HIGH);
  }
  temp = analogRead(analogInPin);
  if(temp < tempThreshold)
  {
    timer = millis();  //reset timer
  }
}