Skip to content

Pro Micro, AIN0/D7 OK but can't relate any other pin with the analog comparator (lib 1.2.4, Arduino IDE 1.8.9) #12

@JazzTp

Description

@JazzTp

analogComp library ver 1.2.4
Arduino IDE ver 1.8.9

I can't have the library work on a Pro Micro which is apparently identical to this one (except it is blue/green): https://www.sparkfun.com/products/12640

The pinout scheme is among the links in the Documents tab:
https://cdn.sparkfun.com/datasheets/Dev/Arduino/Boards/ProMicro16MHzv1.pdf

I can trigger the interrupt through pin AIN0, D7, but I haven't managed to get any reaction from any other pin with any combination of 0 / 3.3 / 5 Volt.

I've been going through web pages like the following ones

but I haven't been able to figure out a successful tweak yet.

Basically, I have been using the sketch coming with the lib except trying the setOn method on different pins and using pin 17 for the builtin LED.

#define __ENABLE_SERIAL__ANALOG_COMP__
#define __CHECK_COMPILE_TIME_FLAGS__
// #define __TRY_THE_ATMEL_STUDIO_EXAMPLE__
/*
This is a simple sketch to demonstrate the use of analogComp, a
library to manage the analog comparator included in a wide 
variety of Atmel microcontrollers

This sketch enables an interrupt to be raised when on the analog input 0
(pin AIN0) there is a voltage greater than the voltage on the
analog input 1 (pin AIN1). To test the sketch, build the following
circuit:
- connect pin AIN1 to pin 3V3
- connect pin AIN0 to GND using a pull-down resistor (10Kohm or greater)
- connect pin AIN0 to pin 5V to activate the interrupt


More info on the analog comparator can be found reading the datasheet.

Please read the README file to know how to use this library.

Written by Leonardo Miliani <leonardo AT leonardomiliani DOT com>
	
This code and the analogComp library are free software; you can redistribute 
and/or modify them under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3.0 of the License,
or (at your option) any later version.

This work is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

*/

// #ifndef ARDUINO_AVR_PROMICRO
//include the library
#include "analogComp.h"
// #endif


//global variables

#ifdef __ENABLE_SERIAL__ANALOG_COMP__
long count, columnsCount;
#endif

#ifdef ARDUINO_AVR_PROMICRO
#define BUILTIN_LED_PIN 17 //set the output LED
#else
#define BUILTIN_LED_PIN 13 //set the output LED
#endif

volatile boolean enableLed = false; //used to check if the interrupt has raised



//let's set up the hardware
void setup() {
    pinMode(BUILTIN_LED_PIN, OUTPUT); //LED pin as output
#ifdef ARDUINO_AVR_PROMICRO

#ifdef __TRY_THE_ATMEL_STUDIO_EXAMPLE__
    // https://www.electronicwings.com/avr-atmega/atmega1632-analog-comparator
    // DDRC |= 0x80;    /* Make pin 7 of PORTC as output */
    ADCSRA &= (1<<ADEN);  /* Disable ADC */
    ADMUX = 0x00;   /* Select ADC0 as a -ve pin of comparator */
    SFIOR |= (1<<ACME); /* Enable analog comparator */
    // ^ error 'SFIOR' was not declared in this scope. That example is for Atmel Studio.
#else
    analogComparator.setOn(7, A10); //we instruct the lib to use voltages on the pins
#endif

#else
    analogComparator.setOn(AIN0, AIN1); //we instruct the lib to use voltages on the pins
#endif

    analogComparator.enableInterrupt(changeStatus, CHANGE); //we set the interrupt and when it has to be raised

    count = 0;
    columnsCount = 0;
}

//main loop
void loop() {
    if (enableLed) { //let's check if the analog comparator has raised the interrupt
        //yes, so we do a little blink of the LED
        digitalWrite(BUILTIN_LED_PIN, HIGH);
#ifdef __ENABLE_SERIAL__ANALOG_COMP__
        Serial.println("*** IRQ TRIGGERED ***");
#endif
        delay(200);
        digitalWrite(BUILTIN_LED_PIN, LOW);
        enableLed = false;
    }
#ifdef __ENABLE_SERIAL__ANALOG_COMP__
    else {
      if (++count == 10000) {
        count = 0;
        Serial.print("-");
        if (++columnsCount == 80) {
          columnsCount = 0;
          Serial.println("");

#ifdef __CHECK_COMPILE_TIME_FLAGS__
#ifdef __AVR_ATmega32U4__
          Serial.println("__AVR_ATmega32U4__ is defined");
#endif
#ifdef ATMEGAxU
          Serial.println("ATMEGAxU is defined (the analogComp lib does it, if __AVR_ATmega32U4__ is defined");
          Serial.println("Notice that this info could be printed out at compile time with compiler directives.");
          Serial.println("");
          Serial.print("AIN0 == "); // result 0
          Serial.print(AIN0);
          Serial.print("  -  AIN1 == "); // result 255
          Serial.println(AIN1);
          Serial.println("");
#endif
#endif
        }
      }
    }
#endif
}

//interrupt to be raised by the analog comparator
void changeStatus() {
    enableLed = true; //let's inform the main loop that the condition has been reached by the analog comparator
}

I'd be happy to test any suggested changes to the library (possibly subordinated to #ifdef ARDUINO_AVR_PROMICRO ... #endif if not of general validity).


Marginally, I get this warning (IDE 1.8.9):

/home/lemon/MANINST/arduino-1.8.9/portable/sketchbook/libraries/analogComp/analogComp.cpp: In member function 'uint8_t analogComp::setOn(uint8_t, uint8_t)':
/home/lemon/MANINST/arduino-1.8.9/portable/sketchbook/libraries/analogComp/analogComp.cpp:100:19: warning: comparison is always true due to limited range of data type [-Wtype-limits]
     if ((tempAIN1 >= 0) && (tempAIN1 < NUM_ANALOG_INPUTS)) { //set the AC Multiplexed Input using an analog input pin
                   ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions