Delay using pot not quite working as it should.

Hi,

My project is to add a time delay to a mechanical thermostat.

The thermostat is one of those that has a gas vial where the gas expands /contracts and throws a switch at a user set temperature. The on/off temperature differential is small and the thermostat switches too frequently. After it reaches temperature and switches off I want to introduce a delay of up to about 10 minutes to lock it out before being allowed to switch on.

I’ve built the circuit and plugged it in and it does everything that it should but the delay isn’t working as it should. When the pot is turned low there is a delay but after about a third of a turn the delay becomes permanent. This isn’t the idea!!

Something is wrong.

Any help much appreciated.

Thanks

/* sketch 1
  Introduce additional delay to thermostat.

*/
int pinBeforesensor = 7; //pin to connect input from sensor BEFORE thermostat
int pinAftersensor = 8; //pin to connect input from sensor AFTER thermostat
int pinPot = A0 ;    // pin to connect the potentiometer
int pinRelay = 2; //pin to connect the Relay
int stateBeforesensor = 1; // initial value of BEFORE sensor
int stateAftersensor = 1; // initial value of AFTER sensor
int val = 0;       // initial value of pot

void setup() {
  pinMode(pinBeforesensor, INPUT_PULLUP); //set switch 1 pin as INPUT
  pinMode(pinAftersensor, INPUT_PULLUP); //set switch 2 pin as INPUT
  pinMode(pinRelay, OUTPUT); //set the LED pin as OUTPUT
}
void loop()
{
  delay(1000);
  val = (analogRead(pinPot) * 600);    // read the value from the pot x 600
  int stateBeforesensor = digitalRead(pinBeforesensor); //read the state of BEFORE sensor
  int stateAftersensor = digitalRead(pinAftersensor); //read the state of AFTER sensor

  if ((stateBeforesensor == LOW) && (stateAftersensor == HIGH))
  {
    digitalWrite(pinRelay, HIGH);  // turn the Relay on
    delay(val);
    digitalWrite(pinRelay, LOW);
    while (digitalRead(pinAftersensor) == HIGH)
      delay(5);

  }  else
  {
    digitalWrite(pinRelay, LOW);  // turn the Relay off
  }
}

val = (analogRead(pinPot) * 600); doesn’t fit in an int.

Hint for the future - use “print” to debug.

Thanks,

Please could you expand on both of these?

Thanks

int val = 0;       // initial value of pot
val = (analogRead(pinPot) * 600);

Suppose that the analogRead() returns 1023 and that you multiply it by 600. The result is 613,800 which is a larger value than an int, especially a signed int that you used, can hold. Consider declaring val as an unsigned long instead of int

When debugging programs it is helpful to print the value of variables at various places in the program so that you can understand what is going on

Hi thanks,

I didn't realise what the upper int value was, well actually I didn't know there was one one. Steep learning curve!

I've modded the code. Have I done it correctly?

Thanks

/* sketch 1
  Introduce additional delay to thermostat.

*/
int pinBeforesensor = 7; //pin to connect input from sensor BEFORE thermostat
int pinAftersensor = 8; //pin to connect input from sensor AFTER thermostat
int pinPot = A0 ;    // pin to connect the potentiometer
int pinRelay = 2; //pin to connect the Relay
int stateBeforesensor = 1; // initial value of BEFORE sensor
int stateAftersensor = 1; // initial value of AFTER sensor
int val = 0;       // initial value of pot
unsigned long var = val;

void setup() {
  pinMode(pinBeforesensor, INPUT_PULLUP); //set switch 1 pin as INPUT
  pinMode(pinAftersensor, INPUT_PULLUP); //set switch 2 pin as INPUT
  pinMode(pinRelay, OUTPUT); //set the LED pin as OUTPUT
}
void loop()
{
  delay(1000);
  val = (analogRead(pinPot) * 600UL);    // read the value from the pot x 600
  int stateBeforesensor = digitalRead(pinBeforesensor); //read the state of BEFORE sensor
  int stateAftersensor = digitalRead(pinAftersensor); //read the state of AFTER sensor

  if ((stateBeforesensor == LOW) && (stateAftersensor == HIGH))
  {
    digitalWrite(pinRelay, HIGH);  // turn the Relay on
    delay(val);
    digitalWrite(pinRelay, LOW);
    while (digitalRead(pinAftersensor) == HIGH)
      delay(5);

  }  else
  {
    digitalWrite(pinRelay, LOW);  // turn the Relay off
  }
}

Have I done it correctly?

No

Read what I wrote. Declare val as an unsigned long. Do not introduce another variable, ie var, especially one that you don’t subsequently use

No, "val" is still of type "int". You have declared a new variable "var" of type "unsigned long" which is not used for anything.

Ok, thanks,

I've only got a book for beginners and it doesn't cover this!

Is this more like it?

Cheers

/* sketch 1
  Introduce additional delay to thermostat.

*/
int pinBeforesensor = 7; //pin to connect input from sensor BEFORE thermostat
int pinAftersensor = 8; //pin to connect input from sensor AFTER thermostat
int pinPot = A0 ;    // pin to connect the potentiometer
int pinRelay = 2; //pin to connect the Relay
int stateBeforesensor = 1; // initial value of BEFORE sensor
int stateAftersensor = 1; // initial value of AFTER sensor
unsigned long val = 0; // initial value of pot

void setup() {
  pinMode(pinBeforesensor, INPUT_PULLUP); //set switch 1 pin as INPUT
  pinMode(pinAftersensor, INPUT_PULLUP); //set switch 2 pin as INPUT
  pinMode(pinRelay, OUTPUT); //set the LED pin as OUTPUT
}
void loop()
{
  delay(1000);
  val = (analogRead(pinPot) * 600UL);    // read the value from the pot x 600
  int stateBeforesensor = digitalRead(pinBeforesensor); //read the state of BEFORE sensor
  int stateAftersensor = digitalRead(pinAftersensor); //read the state of AFTER sensor

  if ((stateBeforesensor == LOW) && (stateAftersensor == HIGH))
  {
    digitalWrite(pinRelay, HIGH);  // turn the Relay on
    delay(val);
    digitalWrite(pinRelay, LOW);
    while (digitalRead(pinAftersensor) == HIGH)
      delay(5);

  }  else
  {
    digitalWrite(pinRelay, LOW);  // turn the Relay off
  }
}

Yes, that's it! :slight_smile:

That looks better but I still don't understand parts of your code.

Please post a schematic of your circuit. A photo of a pencil and paper drawing is good enough. I am afraid that a picture of your project tells us little or nothing about what is connected to what, especially when most of the wires are blue

Excellent…getting there!

Cheers

Basic Schematic attached

Thanks for the schematic

I presume that I am missing something but why do you need a before and after sensor ? If you were to detect when the output became off then you could start a timing period based on millis() at that time and not allow the solenoid to turn on the supply again until the required period had ended.

Note that there is a difference between the output becoming off and it being off. You need to detect the change of state between on and off as illustrated by the StateChangeDetection example in the IDE

Hi and thanks.

All is working.

It's gone into a piece of equipment for my work and sensing the state before and after the thermostat is necessary.

I've come across millis() as an alternative to delay() but for me and this application delay() works ok, perhaps I'll read up your tutorial on millis() and include it a future projects.

Thanks for help