Pages: [1]   Go Down
Author Topic: Can I output a value from loop function in an interrupt  (Read 646 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm new to using interrupts and am trying to figure out how to print the current value of a variable at a set time using an interrupt.  It's a simple oven program, and I want to output a measurement every 5 seconds.  I want it to be precise so I figured I'd use an interrupt using Timer1.

The problem is that I get the not so surprising error message of the temperature variable "not declared in this scope" since it's declared in the loop rather than the interrupt. 

Is there a way for me to print the current value of the temperature variable in the loop and output it in an interrupt?

Thanks for your help
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hi, I'm new to using interrupts and am trying to figure out how to print the current value of a variable at a set time using an interrupt.
You can't. Serial output does not belong in an interrupt handler. It matters not whether the interrupt is an external interrupt or a timer based interrupt.

Quote
It's a simple oven program
Which probably does not need interrupts at all. What is generating the interrupts of interest, and why is polling not sufficient?

Quote
and I want to output a measurement every 5 seconds.
Every 5 seconds is an eternity as far as the Arduino is concerned, unless you introduce a time warp (aka delay()).

Quote
The problem is that I get the not so surprising error message of the temperature variable "not declared in this scope" since it's declared in the loop rather than the interrupt. 
Whether it is surprising or not depends on where you defined the variable. A global variable is always available. A local variable is limited in scope. It isn't clear from your code (hint, hint) where the variable is defined (other than in the wrong scope).

Quote
Is there a way for me to print the current value of the temperature variable in the loop
Of course.

Quote
and output it in an interrupt?
Buzzwords like output should be avoided in any technical discussion. There is some specific way of outputting the value that you have in mind. Don't be shy. Just blurt out what that method is.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the prompt response.  And I'm new to this so any advice RE "buzzwords" and any other conventions are helpful.

So I will provide a little more clarity on what I'm doing/trying to do.

I have 2 Arduino controlled electric ovens, one is 8x2x1.5 feet.  Another 18x1.75x1.5.  I built them to cure parts for an aircraft manufacturer that I work with.  I'm curing a molded part, and the molds are of varying mass/volume.  I need to send via serial port a temperature measurement at precise intervals in order to better understand how mass/shape of the mold is affecting rate of heat transfer to the part among several other temperature measurements.  One thermocouple is on the part, the other is measuring the ambient temperature inside the oven.

Here's the very simple code.  I'm using a thermocouple library from Adafruit

Code:
#include "Adafruit_MAX31855.h"

int heatPin1 = 9;
int heatPin2 = 8;
int targetTemp = 350;
int thermoDO = A2;
int thermoCS_1 = A3;
int thermoCS_2 = A4;
int thermoCLK = A5;

Adafruit_MAX31855 thermocouple_1(thermoCLK, thermoCS_1, thermoDO);
Adafruit_MAX31855 thermocouple_2(thermoCLK, thermoCS_2, thermoDO);
 
void setup() {
  Serial.begin(9600);
  pinMode (heatPin1, OUTPUT);
  pinMode (heatPin2, OUTPUT);
  Serial.println("MAX31855 test");
  delay(500);
}

void loop() {
   Serial.print("T1 = ");
   Serial.println(thermocouple_1.readFarenheit());
   
   Serial.print("T2 = ");
   Serial.println(thermocouple_2.readFarenheit());
   
      if ((thermocouple_1.readFarenheit()) < targetTemp - 1)
   {
     digitalWrite (heatPin1, HIGH);
     delay (50);
     digitalWrite (heatPin2, HIGH);
   }
   else
   {
     digitalWrite (heatPin1, LOW);
     digitalWrite (heatPin2, LOW);
   }
   
   delay(200);
}


At this point knowing the precise moment of when the heating element is turned on isn't really important, the controller is keeping the oven +/-2 degrees F which is fine for this application.  I just need the two thermocouples to be reporting at exactly (or as close to exactly) .2 second intervals. 

Thanks again for the help.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have 2 Arduino controlled electric ovens, one is 8x2x1.5 feet.  Another 18x1.75x1.5.  I built them to cure parts for an aircraft manufacturer that I work with.
Cool. How are these ovens powered? How fast does the temperature change? I'm only familiar with my home oven and the autoclaves we use at work to cook composite parts. Neither of them have significant changes in measured temperature in a 5 second interval.

Quote
I need to send via serial port a temperature measurement at precise intervals in order to better understand how mass/shape of the mold is affecting rate of heat transfer to the part among several other temperature measurements.
How precise? Using millis() to determine if it is time to check/record/send the temperature would get you millisecond resolution. If that's not good enough, try micros() for 250 times better resolution. The micros() function is good for +/- 5 microseconds.

Quote
I just need the two thermocouples to be reporting at exactly (or as close to exactly) .2 second intervals. 
So, millis() will be more than adequate. No interrupts needed.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, this looks perfect.  Paul are you a Boeing man?  Autoclaves + Seattle...

I made the ovens using 1750 watt electric heating coils that are made for small glass kilns.  The 18 footer has 2, so it's running 3500 watts.  They're actually much more efficient than I thought they'd be. Temperature increases slightly less than 1 degree F per second initially and only takes about 8 minutes to come up to the 350 degree curing temperature.  I either run them off of 2 standard 120v-15Amp circuits or as I did through hurricane Sandy, a little 6000 watt generator. 

Our building had no power for 10 days but we kept on cooking parts.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One thing we're doing is that we have a few different models for our mold and we're trying to figure out which performs best.  They have different fin configurations that add rigidity of the mold but also increase the surface area of the mold to speed up the rate the mold comes up to temp.  We also want to keep them as simple as possible.  So we're testing several different molds to see out how they perform.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok, this looks perfect.  Paul are you a Boeing man?  Autoclaves + Seattle...
For the last 33 years, yep.

Quote
Temperature increases slightly less than 1 degree F per second initially and only takes about 8 minutes to come up to the 350 degree curing temperature.
So, nothing really exotic like 300 degree swings in 20 microseconds. Simply monitoring the temperature, and outputting every 5000 milliseconds sounds reasonable, and easy.

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At our refinery we had lots of temperature control loops on very large vessels and columns, some with many thousands of gallons of product boiling off for distillation and because the process time lag is so slow if was among the hardest P&ID control loops to get tuned correctly. Make a tuning change and have to wait a long time to see if it was an improvement or not.

Lefty
Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 146
Posts: 5520
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is absolutely NOT what interrupts are for....

What you need to know is when five seconds have passed:

Code:
unsigned int milliCount=0;
void loop()
{
  delay(1);
  if (++milliCount==5000) {
    // Five seconds have passed
    milliCount = 0;
    ...
  }
}

OTOH the Arduino clock isn't terribly accurate. If this is is really critical you might need to add an external clock module.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
unsigned int milliCount=0;
void loop()
{
  delay(1);
  if (++milliCount==5000) {
    // Five seconds have passed
    milliCount = 0;
    ...
  }
}
There's that delay() that the whole discussion has been about avoiding. There is no guarantee that the rest of the loop() function will execute in 0 time (in fact, it's guaranteed NOT to), so your method of determining when 5 seconds has passed is guaranteed to fail.

It is stuff like this that gives the Arduino's ability to maintain time a bad reputation.
Logged

Pages: [1]   Go Up
Jump to: