0

I am taking reading from temperature sensor and from that data. I have to control 3 LEDs. Lets consider LED1, LED2, LED3.

If temperature range between 0-20
   I have to HIGH LED2.
   else if temperature range between 20-30 
I have to HIGH LED1; HIGH LED3; LOW LED1; 

my sketch is something like this:-

while(1)
{
if(temp>=0&&temp<=20)
{
digitalWrite(2,HIGH);
}
else if (temp>=20&&temp<=30)
{
digitalWrite(1,HIGH);
digitalWrite(3,HIGH);
digitalWrite(1,LOW);
}
}

The problem which I am facing is LED1 is continuously ON (which should be OFF), when temperature is between 20-30 because of continuously running loop so, how can I stop loop, once the statement under second condition has executed till the second condition has made false.

  • Don't understand your "I have to HIGH LED1; HIGH LED3; LOW LED1;" ? What do you want for LED1 ? – DataFiddler Sep 24 at 16:52
  • I want to first ON LED1 then ON LED2 then OFF LED1 – anupam verma Sep 24 at 16:55
  • when the condition2 is satisfied and stop the loop till the condition 2 is made false – anupam verma Sep 24 at 16:57
  • If the temperature is exactly 20, which LEDs should be on/off? Shouldn't LED2 be in this statement somewhere "I have to HIGH LED1; HIGH LED3; LOW LED1;". – VE7JRO Sep 24 at 17:16
  • yeah sorry brother have to mention LED3 at the place of 2 ... as according to you – anupam verma Sep 24 at 17:23
2

Your code doesn't make sense. You have a while (1) statement, which will run forever. Inside the body of that statement, you never change the value of temp.

An Arduino sketch's loop() function runs continuously. You should not have a while (1) loop inside your loop function. You should have a loop function something like this:

void loop() {
  temp = //read a temp value somehow 
  if(temp>=0&&temp<=20)
  {
    digitalWrite(2,HIGH);
  }
  else if (temp>=20&&temp<=30)
  {
    digitalWrite(1,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(1,LOW);
  }
}   

With that loop function you would fetch a new value of temp on each pass through the loop, then decide what LEDs to light.

  • I have edited the code as u said but I am facing the problem i have to stop LED1 till the condition 2 is made false ...but it is continuously ON – anupam verma Sep 24 at 16:35
  • I have to stop loop till the condition 2 has made false along with continuously taking reading of temperature – anupam verma Sep 24 at 16:38
  • 1
    This is much better than the original question's sketch, but still very questionable: 2 will never be LOW, once it was set HIGH; 3 will never become LOW, once it was set HIGH; 1 is considerably dimmed, if temp is between 20 and 30. (dimming depends on the time it takes to read temp) – DataFiddler Sep 24 at 17:00
  • @DataFiddler The OPs logic is incomplete, and I didn't attempt to address those issues. I was just trying to explain how to write code that gets called continuously. – Duncan C Sep 24 at 18:48
  • 1
    "I have to stop loop..." No. Full stop. You should not write a sketch that freezes the loop until some condition occurs. That is a very bad idea. You need to come up with a different approach. – Duncan C Sep 24 at 18:49
2

You don't want to stop the loop! You want it to run fast and often.

Eventually you want it to not change the current state of a LED, but in your case, I rather do not see enough defined states of LEDs.

I want to first ON LED1 then ON LED2 then OFF LED1"

if then means an amount of time, remember that loop runs fast and does not take time by itself. You will have to notice the starting point in time and compare that with the current time and your desired intervals. (And you do not mention what should happen to LED2 after the second then )

const unsigned int LED1_DURATION=500;
const unsigned int LED3_DURATION=1000;
bool c2 = false;  // true while condition2 is met
unsigned long start2;  // time when condition2 starts 
void loop() {
  int temp = analogRead(A0); //read a temp value somehow 
  if(temp<=20)
  {
    digitlaWrite(1, LOW);
    digitalWrite(2,HIGH);
    digitlaWrite(3, LOW);
    c2 = false;
  }
  else if (temp >20 && temp <= 30)
  {
     digitalWrite(2, LOW);

     if (c2 == false) { c2 = true; start2=millis(); } // detect start time 

     if ((millis() - start2 < LED1_DURATION) ||
         (millis() - start2 > LED3_DURATION)   )
          digitalWrite(1,HIGH);
     else digitalWrite(1, LOW); 

     if ((millis() - start2 > LED1_DURATION) &&
         (millis() - start2 < LED3_DURATION)    )
          digitalWrite(3,HIGH);
     else digitalWrite(3,LOW);
  }
  else
  {
    digitlaWrite(1, LOW);
    digitalWrite(2, LOW);
    digitlaWrite(3, LOW);
    c2 = false;
  }
} 

This code contains a couple of guesses about states you did not mention, and a slightly different behavior (sorry), but I hope you get an idea...

  • LED2 has to be off if the condition1 is not correct... – anupam verma Sep 24 at 17:26
  • let me explain again if the temperature is between 20-30 condition2 will be executed as LED1 HIGH; LED3 HIGH; LED1 LOW . after execution i have to stop loop till the condition 2 is made false i.e temperature exceeds 30 or decrease below 20. while stoping the loop we also have to continuously taking reading from temperature sensor – anupam verma Sep 24 at 17:31
  • sorry i have a mistake at the place of LED2 it should be LED3 – anupam verma Sep 24 at 17:35
  • "while stoping the loop we also have to continuously taking reading from temperature sensor" So don't call it stoping You're simply in the part 'Condition2' but the time has exceeded both then times and your LEDs have a desired state. – DataFiddler Sep 24 at 17:38
  • 1
    Don't try to copy/paste the code DataFiddler posted and complain when it gives compile errors It's intended to be a guide, not the exact code for your problem. Study the approach they're recommending and then apply that approach to your problem. The idea is that you don't "stop the loop", you use a combination of flags to keep track of state, and timers to track when events occur. You then write code that checks the current value of millis() on each pass through the loop, waiting for the desired amount of time to pass. – Duncan C Sep 25 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.