Hello, I am trying to use the analog comparator on an Arduino Uno to trigger an interrupt when I hit a voltage of > 3.3 volts on a rising edge. I am able to trigger on a rising edge but it seems to ignore my AIN1 voltage level and will always interrupt even when I have 2.1v on my AIN0 input. I have to use the analog comparator due to my timing of my system. My code is as followed:
#include <avr/interrupt.h>
#define DELAYTIME 500
bool pinStatus = false;
double lastTime = 0;
volatile bool analogTrigger = false;
void setup() {
// put your setup code here, to run once:
pinMode(A0, OUTPUT);
pinMode(3, INPUT);
//Set up the interrupts for the analog Comparator
ADCSRB = 0; // (Disable) ACME: Analog Comparator Multiplexer Enable
ACSR = bit (ACIE) // (Clear) Analog Comparator Interrupt Flag
| bit (ACIE) // Analog Comparator Interrupt Enable
| bit (ACIS1) // ACIS1, ACIS0: Analog Comparator Interrupt Mode Select (trigger on falling edge)
| bit (ACIS0);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int value = digitalRead(3);
if (value == HIGH && (millis() - lastTime) > DELAYTIME)
{
if (pinStatus)
{
digitalWrite(A0, HIGH);
pinStatus = false;
}
else {
digitalWrite(A0, LOW);
pinStatus = true;
}
lastTime = millis();
}
if (analogTrigger) {
Serial.println("High");
analogTrigger = false;
}
}
//the ISR that handles the analog compare interrupt
ISR(ANALOG_COMP_vect)
{
analogTrigger = true;
}
Does anyone have any ideas what is going on?
I am using a voltage divider on pin A0 to give myself a 2.1V (Using it as a digital pin) signal to test the system right now. Also, I am using pin 3 as a push button to toggle my 2.1V input. Some of my code for the comparator I found from another post and I am using it temporarily so I can get it working then I plan on fixing that section up.
Thank you!