I’m having trouble with a project using both the analog comparator and timer1. I hope someone can give me some advice on what’s going wrong.
Hardware: Arduino Uno R3
What I’m trying to do: I want to turn on a fan when a temperature rises above a certain level. When the temperature drops the fan stops after a certain time.
How I’m doing it: I have a thermopile sensor on pin 6 (outputs voltage between 0 and 5 volts) and a reference voltage on pin 7 (about 2 volts). The analog comparator trips an interrupt when the thermopile voltage is greater than the reference voltage. The fan then starts up (set pin 4 to HIGH which is connected to a transistor acting as a switch) and a timer variable (timecounter) is set based on the value of pin A1 (this is a voltage divider with a potentiometer to make the time user configurable).
Timer1 is supposed to trip every second. If the timer variable reaches zero the fan is turned off (pin 4 LOW) otherwise it decrements the timer variable.
The reason for doing it this way is so that the fan remains on while the temperature is high because the analog comparator keeps tripping and resetting the timer variable.
The problem: The analog comparator interrupt routine works fine but the timer interrupt isn’t. The fan starts but it never switches off.
Ideas on what is going wrong: It looks a bit like the timer interrupt isn’t tripping. I’ve read a lot of tutorials on timer interrupts and it looks to me I’m doing it right (but obviously I can’t be).
Another idea I had was it might be something to do with variable scope for the timer variable (timercounter).
Or could it be the two different kinds of interrupts interfering with each other?
After spending a lot of time on Google I’m still stumped. Any ideas appreciated thanks!
Kerry.
Here is my code:
// Thermopile Activated Fan
// when analog pin rises higher than threshold interrupt to start fan and timer
// every time it passes threshold reset time
// timer interrupt to turn of fan
// Analog comparator
// If AIN0 rises above AIN1 voltage, trigger interrupt
const int THERMOPILE = 6; // AIN0 pin is D6
const int REFPIN = 7; // AIN1 pin is reference voltage
const int FAN = 4; //fan pin
const int TIMESET = A1; // potentiometer for run timer
const float MAXTIME = 120; // maximum fan run time in seconds
volatile int timecounter = 0; // countdown in seconds until fan stops
void setup() {
noInterrupts(); // disable all interrupts
// analog comparator interrupt
ACSR =
(0<<ACD) | // Analog Comparator: Enabled
(0<<ACBG) | // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
(0<<ACO) | // Analog Comparator Output: Off
(1<<ACI) | // Analog Comparator Interrupt Flag: Clear Pending Interrupt
(1<<ACIE) | // Analog Comparator Interrupt: Enabled
(0<<ACIC) | // Analog Comparator Input Capture: Disabled
(1<<ACIS1) | (1<ACIS0); // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge
// timer interrupt (1 per second)
TCCR1A = 0;
TCCR1B = 0;
OCR1A = 15624;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
interrupts(); // enable all interrupts
pinMode(FAN, OUTPUT);
}
void loop() {
}
// analog comparator interrupt function
ISR(ANALOG_COMP_vect) {
noInterrupts();
digitalWrite(FAN, HIGH); // turn on the fan
timecounter = analogRead(TIMESET)/1024 * MAXTIME; // set num of seconds for fan to run based on timer potentiometer
interrupts();
}
// Timer interrupt trips once per second and counts down timer
ISR(Timer1_COMPA_vect) {
noInterrupts();
if(timecounter <= 0) {
digitalWrite(FAN,LOW); // turn off fan
} else {
timecounter--;
}
interrupts();
}