How to complete this programme

Hi!
I’m new, so I’ll be sorry for any mistake I’ll do.

I’m working on a lamp that has to turn on when there’s no sun light (I used a LDR).
But after an hour the lamp begin hot and I need to turn off the lamp because it’s made of paper.
So I used a Thermoresistor.
I did the sketch, but I don’t know how to turn off the bulb for 10 minutes if the temperature arrives at 50 degrees. I only know how to turn off the bulb at 50 degrees, but as soon as arrived at 49° it will be on. And after a moment off…all so.
I want to let it go till a low temperature when it arrives to 50°.
This is the sketch I’d like to correct:

int LDRPin = 0; //LDR's pin
int TempPin = 4; //Thermoresistor's pin
int interruttore = 3; //relay's pin


int base; 
int soglia = 100;  //max light
int soglia1 = 50;  //max heat


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

  pinMode (LDRPin, INPUT); 
  pinMode (interruttore, OUTPUT); 
  base = analogRead(LDRPin); 
}
//float volts()
//{
//  float raw = analogRead(ImPin); 
//  float percent = raw/1023.0;
//  float volts = percent*5.0;
//  return volts;
//}
float tempC()
{
  float raw = analogRead (TempPin);
  float percent = raw/1023.0;
  float volts = percent*5.0;
  return 100.0*(volts-0.5);
}
void loop() {
  int v = analogRead(LDRPin); 
  Serial.print("TEMPERATURE");
  Serial.print(tempC());
  Serial.print(" \260C; ");
  Serial.println();
  if ((base - v) < soglia && (tempC() < soglia1)) {   
    digitalWrite(interruttore, HIGH); 
  } else { 
    digitalWrite(interruttore, LOW);
  }
}

Can you help me? Thanks a lot.
Riccardo

Welcome to the Forum. Please read the two posts by Nick Gammon at the top of this Forum for guidelines on posting here, especially the use of code tags when listing source code here.

Why not test to see how much time it takes for the bulb temperature to drop to some desired level and activate a timer when the bulb is turned off? Otherwise, simply keep monitoring the temp sensor until the desire temp is reached and then kick the bulb back on.

An obvious question is - will the lamp being on trigger the LDR? I assume you have it set up so it doesn't.

So the lamp is in three states

1 - it's light outside
2 - it's dark outside, and the lamp is on
3 - its dark outside, and the lamp is off because we want it to cool down.

The rules become:

1 - if the current state is "it's light outside", then
1a - if the LDR indicates that it's still light outside, do nothing
1b - otherwise, turn the lamp on and set the current state to "dark, lamp on".

2 - if the current state is "it's dark outside, lamp is on", then
2a - if the LDR indicates it's light outside, turn the lamp off and set the current state to "light outside"
2b - if the thermistor indicates the lamp is getting hot, turn the lamp off, make a note of the current time, change state to "cooling"
2c - otherwise, do nothing and leave the lamp on.

3 - if the currrent state is "lamp is cooling"
3a - if the LDR indicates it's light outside, turn the lamp off and set the current state to "light outside"
3b - if ten minues has elapsed since the lamp was turned off, turn it on again and set current state to "lamp os on"
3c - otherwise, stay cooling

What happens if the light outside fluctuates? A car goes by at night, clouds move in front of the sun just as it is dawn/dusk.

What happens if 10 minutes isn't quite enough to cool the lamp?

pichard0203:
I'm new, so I'll be sorry for any mistake I'll do.

Welcome.

I'm not so new and I make mistakes all the time. Don't let it worry you.

...R

PaulMurrayCbr:
An obvious question is - will the lamp being on trigger the LDR? I assume you have it set up so it doesn't.

So the lamp is in three states

1 - it's light outside
2 - it's dark outside, and the lamp is on
3 - its dark outside, and the lamp is off because we want it to cool down.

The rules become:

1 - if the current state is "it's light outside", then
1a - if the LDR indicates that it's still light outside, do nothing
1b - otherwise, turn the lamp on and set the current state to "dark, lamp on".

2 - if the current state is "it's dark outside, lamp is on", then
2a - if the LDR indicates it's light outside, turn the lamp off and set the current state to "light outside"
2b - if the thermistor indicates the lamp is getting hot, turn the lamp off, make a note of the current time, change state to "cooling"
2c - otherwise, do nothing and leave the lamp on.

3 - if the currrent state is "lamp is cooling"
3a - if the LDR indicates it's light outside, turn the lamp off and set the current state to "light outside"
3b - if ten minues has elapsed since the lamp was turned off, turn it on again and set current state to "lamp os on"
3c - otherwise, stay cooling

What happens if the light outside fluctuates? A car goes by at night, clouds move in front of the sun just as it is dawn/dusk.

What happens if 10 minutes isn't quite enough to cool the lamp?

I'll find the right time to have cold the lamp.
maybe 20,30,40 minutes, I'll see.
Only have the sketch, I tried to add a "delay" in the "if" function" to turn off the bulb for a determinate time, but it didn't worked correctly.
Thank you all.

Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button.

aarg:
Welcome to the Forum. Please read the two posts at the top of this Forum by Nick Gammon on guidelines for posting here, especially the use of code tags ("</>") when posting source code files. Also, before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button.

Is it now ok?
Can you help me with the sketch? I need it because I’m working in my school for Erasmus European Project.
Thank you

look at this code and see if you understand it. If you do not understand how it works or why its doing something then post back what part you do not understand. Your teacher is going to ask questions so you had better understand how it works. I only tested the ldr part to make sure the debounce worked. It may be programmed backwards but the theory works. The temp sensor looks like it should work but im not going to bother testing as its your project.

int LDRPin = A0; //LDR's pin
int TempPin = A4; //Thermoresistor's pin
int lightRelay = 3; //relay's pin

int minLight = 100;  //max light
int maxHeat = 50;  //max heat
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 600000; //1000 millis * 60seconds * 10 minutes
unsigned long lightDelay = 3000; // 3 seconds
byte timer1Run = 0;
byte requestLight = 0;
byte overTemp = 0;


void setup() {
  Serial.begin (9600);
  pinMode (LDRPin, INPUT);
  pinMode (lightRelay, OUTPUT);

}


void loop() {
  int lightLevel = analogRead(LDRPin);
  Serial.println(lightLevel);

  unsigned long currentMillis = millis();

  if ( lightLevel > minLight) {
    previousMillis3 = currentMillis;
  } else {
    previousMillis2 = currentMillis;
  }
  if (currentMillis - previousMillis2 > lightDelay) {
    requestLight = 1;
  }

  if (currentMillis - previousMillis3 > lightDelay) {
    requestLight = 0;
  }

  if (tempC() > maxHeat && timer1Run == 0)  {
    overTemp = 1;
    timer1Run = 1;
    previousMillis1 = currentMillis;
  }
  if ((currentMillis - previousMillis1 > interval) && (timer1Run == 1)) {
    overTemp = 0;
    timer1Run = 0;
  }
  if ( requestLight == 1 && overTemp == 0) {
    digitalWrite(lightRelay, HIGH);
  } else {
    digitalWrite(lightRelay, LOW);
  }

}


float tempC()
{
  float raw = analogRead (TempPin);
  float percent = raw / 1023.0;
  float volts = percent * 5.0;
  return 100.0 * (volts - 0.5);
}

p.s I had to change some words and the flow of the code as I struggle to program something that I find hard to read im sure you can use edit, find word and change in the ide. The code seems odd as most people would say "when bulb is hot turn off, when bulb is cool turn on after xxx seconds" , rather than when hot turn off and wait 10 mins)

I have a question. What is going on with the light readings? In setup, you take a "base" light measurement. In loop(), you compare the current light reading with that. That means the threshold changes depending on how bright it is when the sketch is started. It just seems unusual.

gpop1:
look at this code and see if you understand it. If you do not understand how it works or why its doing something then post back what part you do not understand. Your teacher is going to ask questions so you had better understand how it works. I only tested the ldr part to make sure the debounce worked. It may be programmed backwards but the theory works. The temp sensor looks like it should work but im not going to bother testing as its your project.

int LDRPin = A0; //LDR's pin

int TempPin = A4; //Thermoresistor's pin
int lightRelay = 3; //relay's pin

int minLight = 100;  //max light
int maxHeat = 50;  //max heat
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 600000; //1000 millis * 60seconds * 10 minutes
unsigned long lightDelay = 3000; // 3 seconds
byte timer1Run = 0;
byte requestLight = 0;
byte overTemp = 0;

void setup() {
 Serial.begin (9600);
 pinMode (LDRPin, INPUT);
 pinMode (lightRelay, OUTPUT);

}

void loop() {
 int lightLevel = analogRead(LDRPin);
 Serial.println(lightLevel);

unsigned long currentMillis = millis();

if ( lightLevel > minLight) {
   previousMillis3 = currentMillis;
 } else {
   previousMillis2 = currentMillis;
 }
 if (currentMillis - previousMillis2 > lightDelay) {
   requestLight = 1;
 }

if (currentMillis - previousMillis3 > lightDelay) {
   requestLight = 0;
 }

if (tempC() > maxHeat && timer1Run == 0)  {
   overTemp = 1;
   timer1Run = 1;
   previousMillis1 = currentMillis;
 }
 if ((currentMillis - previousMillis1 > interval) && (timer1Run == 1)) {
   overTemp = 0;
   timer1Run = 0;
 }
 if ( requestLight == 1 && overTemp == 0) {
   digitalWrite(lightRelay, HIGH);
 } else {
   digitalWrite(lightRelay, LOW);
 }

}

float tempC()
{
 float raw = analogRead (TempPin);
 float percent = raw / 1023.0;
 float volts = percent * 5.0;
 return 100.0 * (volts - 0.5);
}




p.s I had to change some words and the flow of the code as I struggle to program something that I find hard to read im sure you can use edit, find word and change in the ide. The code seems odd as most people would say "when bulb is hot turn off, when bulb is cool turn on after xxx seconds" , rather than when hot turn off and wait 10 mins)

Thank you so much!
I've to clarify that I'm still at the beginnings of programming.
Now it works as well as I'd like.
Just a thing: I would like to see on the Serial Monitor the dates of the Thermoresistor, instead of the LDR's ones. How can I modify it?

I presume you mean data rather than dates.

add a serial print command to print tempC() instead of light level.

gpop1:
I presume you mean data rather than dates.

add a serial print command to print tempC() instead of light level.

Thank you!

gpop1:
look at this code and see if you understand it. If you do not understand how it works or why its doing something then post back what part you do not understand. Your teacher is going to ask questions so you had better understand how it works. I only tested the ldr part to make sure the debounce worked. It may be programmed backwards but the theory works. The temp sensor looks like it should work but im not going to bother testing as its your project.

int LDRPin = A0; //LDR's pin

int TempPin = A4; //Thermoresistor’s pin
int lightRelay = 3; //relay’s pin

int minLight = 100;  //max light
int maxHeat = 50;  //max heat
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 600000; //1000 millis * 60seconds * 10 minutes
unsigned long lightDelay = 3000; // 3 seconds
byte timer1Run = 0;
byte requestLight = 0;
byte overTemp = 0;

void setup() {
 Serial.begin (9600);
 pinMode (LDRPin, INPUT);
 pinMode (lightRelay, OUTPUT);

}

void loop() {
 int lightLevel = analogRead(LDRPin);
 Serial.println(lightLevel);

unsigned long currentMillis = millis();

if ( lightLevel > minLight) {
   previousMillis3 = currentMillis;
 } else {
   previousMillis2 = currentMillis;
 }
 if (currentMillis - previousMillis2 > lightDelay) {
   requestLight = 1;
 }

if (currentMillis - previousMillis3 > lightDelay) {
   requestLight = 0;
 }

if (tempC() > maxHeat && timer1Run == 0)  {
   overTemp = 1;
   timer1Run = 1;
   previousMillis1 = currentMillis;
 }
 if ((currentMillis - previousMillis1 > interval) && (timer1Run == 1)) {
   overTemp = 0;
   timer1Run = 0;
 }
 if ( requestLight == 1 && overTemp == 0) {
   digitalWrite(lightRelay, HIGH);
 } else {
   digitalWrite(lightRelay, LOW);
 }

}

float tempC()
{
 float raw = analogRead (TempPin);
 float percent = raw / 1023.0;
 float volts = percent * 5.0;
 return 100.0 * (volts - 0.5);
}




p.s I had to change some words and the flow of the code as I struggle to program something that I find hard to read im sure you can use edit, find word and change in the ide. The code seems odd as most people would say "when bulb is hot turn off, when bulb is cool turn on after xxx seconds" , rather than when hot turn off and wait 10 mins)

I’ve just tried it.
It works well with a led, but using a Relay it doesn’t work.
The relay is always on and also the lamp.
If temperature is too high, it doesn’t turn off, and also when there’s light outside.
Why it works well with the led and using a relay doesn’t?

write a small test code for the relay and you may find that the relay is on when output is low. (depends on the type of relay you bought)

Hi,

I think its time for;

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Picture of your project?

If LED works but relay doesn't.....

Tom.... :slight_smile:

TomGeorge:
Hi,

I think its time for;

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Picture of your project?

If LED works but relay doesn't.....

Tom.... :slight_smile:

Thanks Tom and Gpop1!

I post a copy of my circuit in the last message quoting gpop1.
The name is: Erasmus fritzing corretto.jpg

something didn't post correctly. Did you try making a simple sketch to test the relay

you have a few posts to delete as they are being repeated

gpop1:
write a small test code for the relay and you may find that the relay is on when output is low. (depends on the type of relay you bought)

I think relay is ok, because using different sketch it works well.

I don't think you understand what I am asking

some relays on separate boards have a optical isolator (other components on the board with the relay) that means the relay doesn't have to be on when the output is on. The relay may be off when the output is on and on when the output off

test run this code and see what happens (notes in code)

int LDRPin = A0; //LDR's pin
int TempPin = A4; //Thermoresistor's pin
int lightRelay = 3; //relay's pin

int minLight = 100;  //max light
int maxHeat = 50;  //max heat
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 600000; //1000 millis * 60seconds * 10 minutes
unsigned long lightDelay = 3000; // 3 seconds
byte timer1Run = 0;
byte requestLight = 0;
byte overTemp = 0;


void setup() {
  Serial.begin (9600);
  pinMode (LDRPin, INPUT);
  digitalWrite(lightRelay, HIGH);//added so relay is off when starting
  pinMode (lightRelay, OUTPUT);

}


void loop() {
  int lightLevel = analogRead(LDRPin);
  Serial.println(lightLevel);

  unsigned long currentMillis = millis();

  if ( lightLevel > minLight) {
    previousMillis3 = currentMillis;
  } else {
    previousMillis2 = currentMillis;
  }
  if (currentMillis - previousMillis2 > lightDelay) {
    requestLight = 1;
  }

  if (currentMillis - previousMillis3 > lightDelay) {
    requestLight = 0;
  }

  if (tempC() > maxHeat && timer1Run == 0)  {
    overTemp = 1;
    timer1Run = 1;
    previousMillis1 = currentMillis;
  }
  if ((currentMillis - previousMillis1 > interval) && (timer1Run == 1)) {
    overTemp = 0;
    timer1Run = 0;
  }
  if ( requestLight == 1 && overTemp == 0) {
    digitalWrite(lightRelay, LOW);//changed to LOW as relay may be low on
  } else {
    digitalWrite(lightRelay, HIGH);//changed to HIGH as relay is high off
  }

}


float tempC()
{
  float raw = analogRead (TempPin);
  float percent = raw / 1023.0;
  float volts = percent * 5.0;
  return 100.0 * (volts - 0.5);
}

gpop1:
I don't think you understand what I am asking

some relays on separate boards have a optical isolator (other components on the board with the relay) that means the relay doesn't have to be on when the output is on. The relay may be off when the output is on and on when the output off

test run this code and see what happens (notes in code)

int LDRPin = A0; //LDR's pin

int TempPin = A4; //Thermoresistor's pin
int lightRelay = 3; //relay's pin

int minLight = 100;  //max light
int maxHeat = 50;  //max heat
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
unsigned long interval = 600000; //1000 millis * 60seconds * 10 minutes
unsigned long lightDelay = 3000; // 3 seconds
byte timer1Run = 0;
byte requestLight = 0;
byte overTemp = 0;

void setup() {
  Serial.begin (9600);
  pinMode (LDRPin, INPUT);
  digitalWrite(lightRelay, HIGH);//added so relay is off when starting
  pinMode (lightRelay, OUTPUT);

}

void loop() {
  int lightLevel = analogRead(LDRPin);
  Serial.println(lightLevel);

unsigned long currentMillis = millis();

if ( lightLevel > minLight) {
    previousMillis3 = currentMillis;
  } else {
    previousMillis2 = currentMillis;
  }
  if (currentMillis - previousMillis2 > lightDelay) {
    requestLight = 1;
  }

if (currentMillis - previousMillis3 > lightDelay) {
    requestLight = 0;
  }

if (tempC() > maxHeat && timer1Run == 0)  {
    overTemp = 1;
    timer1Run = 1;
    previousMillis1 = currentMillis;
  }
  if ((currentMillis - previousMillis1 > interval) && (timer1Run == 1)) {
    overTemp = 0;
    timer1Run = 0;
  }
  if ( requestLight == 1 && overTemp == 0) {
    digitalWrite(lightRelay, LOW);//changed to LOW as relay may be low on
  } else {
    digitalWrite(lightRelay, HIGH);//changed to HIGH as relay is high off
  }

}

float tempC()
{
  float raw = analogRead (TempPin);
  float percent = raw / 1023.0;
  float volts = percent * 5.0;
  return 100.0 * (volts - 0.5);
}

Ah, ok. I didn't understand :wink:
I don't know if this sketch will resolve this problem, because the relay was alway on.
I'll try it in next days and I'll keep you informed.
Thank you grop1! You're fantastic