Turning on LED from multiple pot values

Hi everyone,

first time posting, sorry if I'm not posting in the right place.

I'm trying to trigger an LED from a potentiometer, the values going from 0 to 500 to 300. I need the LED to light up at 300.

my code works fine as this is a simple circuit and code. My only issue is I don't have enough knowledge to what to put down for the LED to light up at 300 after 0 to 500.

Initialize a flag, reached500, to false. When value reaches 500 set reached500 to true. This will 'remember' that the 500 level has been met.

When reached500 == true and value == 300 do something.

1 Like

please post your code using </>

are you saying you only want the LED on after the pot has been at the value 0, then 500 and finally 300?

if so, consider

enum { Off = HIGH, On = LOW };
enum { ST_NONE, ST_0, ST_500 };

void
func ()
{
    static int state = ST_NONE;

    switch (analogRead (potPin))  {
    case 0:
        if (ST_NONE == state)
            state = ST_0;
        break;

    case 300:
        if (ST_500 == state)
            digitalWrite (ledPin, On);

        break;

    case 500:
        if (ST_0 == state)
            state = ST_500;
        break;

    default:
        digitalWrite (ledPin, Off);
        break;
    }
}

1 Like

If your sketch is not doing anything else, you can use blocking code:

const byte PotPin = A0;

void setup()
{
  digitalWrite(LED_BUILTIN, LOW);
  pinMode(LED_BUILTIN, OUTPUT);

  while (analogRead(PotPin) != 0) ;
  while (analogRead(PotPin) != 500) ;
  while (analogRead(PotPin) != 300) ;
  digitalWrite(LED_BUILTIN, HIGH);
}

void loop() {}
1 Like

Thank you all for the responses, I will try these possible solutions. To further clarify. I wanted the LED to turn on after a sequence of 0,500,300 have been shown.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.