analogRead(pin) as part of an Interrupt Service Routine

alex94364832
Posts: 1
Joined: Fri Dec 03, 2021 3:25 am

analogRead(pin) as part of an Interrupt Service Routine

Postby alex94364832 » Fri Dec 03, 2021 4:01 am

Hello, I have a question about if it is possible to trigger an interrupt and subsequently do an analogRead on that pin?
I have 6 buttons attached with a number of resistors to analog pin 36 of the ESP32 so that I can attach multiple buttons on the same analog pin. (Like in this example: https://www.youtube.com/watch?v=Y23vMfynUJ0)

The output is nothing, except for the button that has no resistor value (the first one) in the array. However, it only prints two 1s in the serial output.

I think the problem is that "attachInterrupt(digitalPinToInterrupt(BTNS), isrbuttons, CHANGE" is expecting a change. Going from fully High to fully Low or the other way around. I was hoping it would trigger when there was any change. However, this wouldn't explain why it only prints two values in the serial output.

I would highly appreciate any suggestions on achieving something similar to what I'm trying to do below!
Alex

The code I am running now looks like this:

Code: Select all

#define BTNS 36

volatile bool iButtons = false;

void IRAM_ATTR isrbuttons()
{
    iButtons = true;
}

int8_t FuncButtonAnalogueRead()
{
    iButtons = false;
    int16_t JButRead = analogRead(BTNS);
    if (JButRead > 3500 < 4500)
    {
        return 1;
    }
    if (JButRead > 1600 < 3500)
    {
        return 2;
    }    
    if (JButRead > 1000 < 1600)
    {
        return 3;
    }
    if (JButRead > 700 < 1000)
    {
        return 4;
    }
    if (JButRead > 600 < 700)
    {
        return 5;
    }
    if (JButRead > 450 < 600)
    {
        return 6;
    }
}

void setup()
{
    Serial.begin(115200);
    
    pinMode(BTNS, INPUT);
    attachInterrupt(digitalPinToInterrupt(BTNS), isrbuttons, CHANGE);
}

void loop()
{
    if (iButtons)
    {
        int8_t ENButton = FuncButtonAnalogueRead();
        Serial.println(ENButton);
    }
}

rodmcm
Posts: 65
Joined: Sat Sep 02, 2017 3:31 am

Re: analogRead(pin) as part of an Interrupt Service Routine

Postby rodmcm » Tue Dec 07, 2021 3:13 am

Yes you are correct, the button is defined as an input but you are using it as an analog input, it will not change state with the resistor network attached

Why not just sample the input as an analog and then invoke FuncButtonAnalogRead( int16-t AnalogIn)


If you have too much code in the loop ( in you final program) then set up a time trigger to read

Who is online

Users browsing this forum: No registered users and 62 guests