Interrupt using pot value?

Hello!

I would like to use a 10k potentiometer to trigger an interrupt as i have found an issue with my current code.

I have a loop that uses the potentiometer to determine the length of the of a delay which is used at the end of the loop. The issue is that some of the pot values give quite a large delay so if you turn the potentiometer you must wait the length of which ever delay it is currently on before the value will change. I've tried moving this outside of the loop but then then the delay will no longer change.

I'd appreciate any help :slight_smile:

Greetings,

What is the name of your board?

It's the classic "I cornered myself with delay, now I need interrupts".

No, you don't need interrupts, just switch your delay timing to a millis based timing.

2 Likes

the code can be reduced in size by using sprintf() to format a complete c-string with all values to overwrite what it on the display (as well as could be used with Serial.print())

you could conditionally check millis() for an expire period of time before remeasuring or updating the display while outside the condition monitor the pot for changes beyond some delta

here's a quick simulation demonstrating


struct LCD {
    void setCursor (int x, int y)  { };
    void print (const char *s)     { Serial.println (s); };
} lcd;

#define pinPot  A0

void setup ()
{
    Serial.begin (9600);
}

unsigned long msecLst;
unsigned long cycletime;

int           potLst = 0;

void loop ()
{
    unsigned long  msec = millis ();
    const char    *s;

    int potread = analogRead (pinPot);

    if (5 < abs(potread - potLst))  {
        potLst = potread;

        if (potread <= 204) {
            cycletime = 10;
            s         = " 1min";
        }
        else if (potread <= 408) {
            cycletime = 286;
            s         = "30min";
        }
        else if (potread <= 612) {
            cycletime = 573;
            s         = " 1 hr";
        }
        else if (potread <= 816) {
            cycletime = 3438;
            s         = " 6 hr";
        }
        else {
            cycletime = 6875;
             s         = "12 hr";
        }

        lcd.setCursor (11,1);
        lcd.print     (s);

        cycletime *= 10;

        msecLst = msec;
    }

    if ((msec - msecLst) > cycletime)  {
        msecLst = msec;

        Serial.println ("do somthing");
    }
}

Hi, i'm using an Arduino UNO R3

I will give that a go thank you :slight_smile:

What kind of issue?

Don’t forget you can insert print statements to help you debug your code,
After you read the pot value , print it to the serial monitor to see if the value is what you expect