2
void buttonPressed()
{
  if(ledOn)
  {
    ledOn = false;
    digitalWrite(13,LOW);
  }else
  {
    ledOn = true;
    digitalWrite(13,HIGH);
  }

  vout=analogRead(sensor);
  vout=(vout*500)/1023;
  tempc=vout; // Storing value in Degree Celsius
  tempf=(vout*1.8)+32; // Converting to Fahrenheit 
  lcd.setCursor(0,0);
  lcd.print("ROOM TEMPERATURE");

  lcd.setCursor(0,1);
  lcd.print("C = ");
  lcd.print(tempc);

  lcd.print("  F = ");
  lcd.print(tempf);
  Serial.print("f ");
  Serial.print(tempf);
}

here is the code of interrupt and i want to monitor the sensor value for about 5second.

4

You don't need interrupts for any of that. Your main loop just needs to test the switch, eg.

void loop ()
  {
  if (digitalRead (button) == HIGH)  // assuming LOW means pressed
     return;

  // read thermocouple here

  // display results

  delay (5000);

  }

I want to print the value of the thermistor on lcd using interrupts and i want the value to stay about 5 secs

You are misusing interrupts in what you are attempting. See my page about interrupts. You simply should not be attempting to print or delay inside an interrupt routine. The most you should do is detect that the switch is pressed:

volatile bool switchIsPressed = false;

// Interrupt Service Routine (ISR)
void buttonPressed()
  {
  switchIsPressed = true;
  }  // end of buttonPressed ISR

Now in your main loop you detect that the switch is pressed and take your reading. If you want to do something else "remember" the time you showed the value, like this:

unsigned long whenTemperatureDisplayed = 0;

void loop ()
  {
  if (switchIsPressed && whenTemperatureDisplayed == 0)
    {
    // take reading ...
    // display reading ...
    whenTemperatureDisplayed = millis ();  // when we displayed the reading
    switchIsPressed = false;               // ready for next time
    }  // end of if switch was pressed

  if (whenTemperatureDisplayed != 0 &&
      millis () - whenTemperatureDisplayed >= 5000) // 5 seconds are up
    {
    // stop displaying the reading ...
    whenTemperatureDisplayed = 0;
    }  // end of if time is up

  // do other stuff here

  }  // end of loop
  • I am making a temperature monitoring device Thermocouple is the main sensor and I am using thermistor for room temperature sensing. I want to print the value of the thermistor on lcd using interrupts and i want the value to stay about 5 secs. the code is working but i am unable to make a delay of about 5sec while staying on the interrupt – Peouse Dutta Apr 11 '18 at 22:47
  • 1
    the code is working but i am unable to make a delay of about 5sec - it's working but it isn't, huh? The "not working" part is the part you have to completely rewrite. It isn't almost working. You are not doing it in a way that will work. See amended answer. – Nick Gammon Apr 11 '18 at 23:42
  • My amended answer does not do any delays, and thus you will not have any trouble doing other things, as well as displaying the temperature for 5 seconds. – Nick Gammon Apr 11 '18 at 23:44
3

It's bad practice to use a delay within a timer, and 5 seconds is a huge amount of time. Also print statements and possibly lcd print statements I would not advice.

What is better, is to calculate the value, store it in a global variable (vout?). Than set a boolean that a new value has been calculated.

Your main program checks the boolean and does all the print/lcd code.

If you think you might miss values, than create a global array where you add values in your interrupt, and let the main program process the array.

  • I am making a temperature monitoring device Thermocouple is the main sensor and I am using thermistor for room temperature sensing. I want to print the value of the thermistor on lcd using interrupts and i want the value to stay about 5 secs. the code is working but i am unable to make a delay of about 5sec while staying on the interrupt. – Peouse Dutta Apr 11 '18 at 22:38
  • You can use a delay of 5 sec in your main, or what's even better is to use millis(). Check the blink Arduino program for an example. – Michel Keijzers Apr 11 '18 at 22:39
  • delaying 5 sec in my main would cause a delay in the monitored value. I want the sensed temperature to be accurate with time and as well as the interrupt to stay about 5sec. – Peouse Dutta Apr 11 '18 at 22:49
  • 2
    If you wait 5 sec in interrupt, your main is interrupted for 5 sec and will not do anything, until the interrupt ends. Interrupt would not help you the way you think it would. Use waiting without delay in main and do your measuring/displaying just there. – gilhad Apr 11 '18 at 23:02
  • 3
    @PeouseDutta delaying 5 sec in my main would cause a delay in the monitored value - delaying 5 seconds in the interrupt will also cause a delay in the monitored value, so you haven't achieved anything by delaying there. If you succeed in delaying inside the interrupt no other code will execute during that delay. – Nick Gammon Apr 11 '18 at 23:46
2

Strictly speaking, yes you CAN generate 5 second delay in interrupt. But you really, really should NOT do it.

And if you are going to do it anyway, it could not be easily done with delay(), as Arduino supports only one level of interrupts and delay depends on interrupts enabled. (which can also be cheated away, but it should not be).

The right thing is use interrupt for fast detection of key pressed and manage other parts in loop(), see https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay how to wait in main, while doing other work.

Also using lcd and Serial inside interrupt can bring a lot of problems, especially if you are using it also in loop() (as the key can be pressed anytime and interrupt can well -interrupt- such commands in the loop and then make your output all wrong and possibly crash your program on the way too.)


Read http://gammon.com.au/interrupts if you want to know more about interrupts and why your idea is not the best possible way.

Question: Can I hit really hard my thumb with heavy hammer? Answer: Yes, you can, but you really should not do it. There are much better ways for using thumbs and hammers.

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.