Turning on and off LED for a certain duration and temperature sensing

Hi all, I am currently doing a project that require me to turn on LED for 5 hours and then turn it off for 8 hours and repeat. I have written a sample code with a short duration for it but it does not seem to work. The problem I faced is that the LED does not turn on and off. The intention of the writing this way is that when the temperature sensing code is been integrated , and when the temperature coding is been called, the LED will remain at its current state with the "count" still counting but I dont know whether is it possible or is there another way of doing this. Please advise or give any suggestions. Many thanks

char count = 0;
char count1 = 0;
char count2 = 0;

void setup() 
{
  pinMode(1, OUTPUT);
  digitalWrite (1, HIGH);
}

void loop() 
{
 count++;
 delay (1); // delay 1mili
 if (count ==  60);// 60mili
 {
   count1++ ;
 }

  if (count1 ==  10);// 600mili
 {
   count2++ ;
 }
 if (count2 == 10)// 6000 mili
 {
 digitalWrite(1,LOW);
}
else 
digitalWrite (1, HIGH);

}

for example:

temperature sensing if count2 ==10; turn on LED else off LED

Hi Johnnytan1234, thanks for using code tags. +1 Karma.

What kind of Arduino are you using? For Uno and many other kind of Arduino, you should not use pin 1 for your led, because this pin is used for sketch upload and serial monitor. Change to use another pin such as pin 2.

I cannot understand, from your description, how measuring the temperature affects the led.

Hi thanks for replying, I am using arduino UNO. I will be doing a small growing medium for growing plants, the LED will turn on for 5 hours and off for 8 hours continuously (timing subjected to change later on) and while the temperature sensor is measuring the ambient temperature the LED will remain at its current states before the sensor code is executed. meaning if the LED is at ON state before the temperature sensor code is been executed it will remain on till temperature sensor code is finished executing. Below is the code that I have modified from my previous one but I am not sure whether this code will work as I intend it to be when I integrate it with the temperature sensor coding

#define LED1 3
#define LED2 2
char count =0;
char count1 =0;
char count2 =0;

void setup() 
{
 pinMode(3, OUTPUT);
 pinMode(2, OUTPUT);// LED to simulate temperature sensor 
 digitalWrite(LED1, HIGH);
}

void loop()
{
delay(500); // pause for 500milisecond
count++;
count2++;
if (count % 5 == 0)  
{ 
 digitalWrite (LED1,LOW); 
 count = 0;
}

if (count2 % 10 == 0)  
{ 
 digitalWrite (LED1,HIGH); 
 count2 = 0;
}
if (count % 5 == !0 || count2 % 10 == !0)
{
 digitalWrite (LED2,HIGH);
 delay(500);
 digitalWrite (LED2,LOW);
}
}

Not much clearer, I'm afraid. What model of temperature sensor are you using? How long do you expect a temperature reading to take? Why are you concerned that the LEDs will change during the temperature measurement?

Before you post your code again, please perform Tools-->Auto Format.

Your code is difficult to follow. All those counters. It's there a good reason you are coding it this way?

More usual ways to achieve this would be to use the millis() function for timing, or use an RTC module, if the timing needs to remain accurate over many weeks or months.

So sorry if i did not make it clearer but I am planning to read temperature every 1 hour or so. I am concerned that when the temperature is been read, the LED will turn off as I know that arduino only executed 1 function at a time and I wanted to make sure that the LED state will not be affect when the temperature is been read but I am not sure how to do it or is it possible so I am thinking that using "count" and adding it into the temperature function will prevent it.

Johnnytan1234: I am concerned that when the temperature is been read, the LED will turn off as I know that arduino only executed 1 function at a time and I wanted to make sure that the LED state will not be affect when the temperature is been read...

millis() function Demonstration code for several things at the same time

Doing multiple things at once

The Arduino will only do one 1 thing at a time, but it does it so fast that it looks like things are done at the same time. Once the LED is on, it stays that way until a line of code says to change it...unless you're using a blocking function like delay(). If you use delay(), then the Arduino essentially does nothing else until the delay is over.

Johnnytan1234: So sorry if i did not make it clearer but I am planning to read temperature every 1 hour or so....

Something like this could be used to read the temperature every hour. You can also adapt it to turn the LED on for 5 hours and off for 8 hours.

int tempPin = A0;
int tempHour = 1;        // variable to store number of hours to read temperature
unsigned long tempRead;  // used to store when temperature last read

void setup() 
{
  // setup pins
  tempRead = millis();  // start timer to read temperature

}

void loop() 
{                                                       //  (unsigned long)1000ms/sec*60sec/min*60min/hr*1hr will give time in milliseconds 
  if (millis() - tempRead >= (1000UL*60*60*tempHour))   //  if the internal millis() timer reaches the time to read the temperature
  {
    readTemp();           // make a function to read and save temperature or whatever you want to do with it
    tempRead = millis();  // reset tempRead to wait for another hour
  }
}

void readTemp()
{
  int temp = analogRead(tempPin);  // read temperature sensor on whatever pin you have it connected to
  // whatever else you want to do with it
}

Johnnytan1234: I am concerned that when the temperature is been read, the LED will turn off as I know that arduino only executed 1 function at a time

Ah, I wondered if that was what you are concerned about. No, the led will stay on until turned off again. Turning the led on (or off) requires a function. Keeping the led on (or off) does not require any function, the led remains in the same state. So there is no danger of the led turning off while the temp reading is being made. The opposite problem is more possible to be a concern: the led cannot be switched off until the temp reading is completed. But as the temp reading will probably take a few tens of milliseconds at most, depending on the type of sensor, so the led would turn off late, but by a time that is too short for us humans to perceive.

Thanks for all your reply is it really helpful. I will try and see if there is anymore issue for me to ask you guys