0

I've coded an analog comparator to work on Nano, but the same code does not work on a brand new Pro Mini 3.3v 8MHz. The serial monitor just outputs the triggered interrupt continuously. Lines orange and yellow have the potentiometer.

enter image description here

enter image description here

The code is:

#define INDICATOR_PIN 8

volatile boolean triggered;
volatile boolean rising;
volatile byte acsr_value;

ISR (ANALOG_COMP_vect) {
  if (triggered) return;           // For testing purposes, ignore the interrupt if we haven't reported the last one yet.
                                   // This also effects some crude debouncing.
  cli();
  acsr_value = ACSR;
  rising = (acsr_value & bit(ACO)) > 0;
  triggered = true;
  sei();
}

void setup() {
  Serial.begin (115200);
  Serial.println ("Started.");

  pinMode(INDICATOR_PIN, OUTPUT);
  digitalWrite(INDICATOR_PIN, LOW);

  ADCSRB = ADCSRB & ! bit(ACME);   // disable Analog Comparator Multiplexer Enable
  ACSR =  bit(ACI)                 // clear Analog Comparator Interrupt Flag
        | bit(ACIE)                // set Analog Comparator Interrupt Enable
        | bit(ACIS1) | bit(ACIS0)  // select rising edge: ACIS1/ACIS0 Analog Comparator Interrupt Mode Select
        ;
}

void loop() {
  if (triggered) show_what_happened();
}

// just give an indication via Serial and LED that we handled an interrupt...
void show_what_happened() {
  digitalWrite(INDICATOR_PIN, HIGH);

  Serial.print ("Triggered! ACSR=");
  Serial.print (acsr_value, BIN);
  if (rising)
    Serial.println (" RISING EDGE (as expected)");
  else
    Serial.println (" FALLING EDGE .. wtf, didn't ask for this, but every so often one slips thru. Maybe a sync issue reading ACSR.");

  triggered = false;

  delay(100);
  digitalWrite(INDICATOR_PIN, LOW);
}

Why does it work well on Nano? I've read all the related sections in the datasheet and tested several ACSR configurations.

I'm seeing the voltage levels changes on a DSO, and it works as expected in the Nano.

I'd like to add that when loading the code to the board, it either prints nothing, or prints continuously RISING FALLING edge. Sometimes, when a connect the serial monitor, it just stops flashing the led. It is definitely an erratic behavior.

What am I doing wrong?

  • Could you tell us the exact board models/ microcontrollers? – Eduardo Trápani Mar 30 at 2:17
  • Yes. It's an Arduino Pro Mini 3.3v 8Mhz board. Atmega328p. – Morty Mar 30 at 3:10
  • Ok, just in case (ISR names and such). Are you using this circuit? Because it sets AIN1 to a reference of 2.5V. Just thnking out loud, but internally in a 3.3v board, that might be too high. – Eduardo Trápani Mar 30 at 3:15
  • I edited the question with the schematics. I assure you that at first, the level of AIN0 is fewer that AIN1, and then when I move the potentiometer, the level surpass AIN1, moment when I expect it to trigger the interruption, but this does not happen. I'm starting to think that there must be some kind of limitation in the Arduino Pro Mini board. – Morty Mar 30 at 6:07
  • There's no limitation - the signals go straight from the header pins to the TQFP pins on both boards. The only difference is AREF - 5V on the Nano, 3.3V on the Promini you are using. What level signal are you sending in, and comparing against? – CrossRoads Mar 30 at 13:21

Your Answer

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

Browse other questions tagged or ask your own question.