# 2 Potentiometers controlling a single LED

**URL:** <https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779>\
**Category:** Programming\
**Created:** [August 18, 2021, 11:22am UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779 "2021-08-18T11:22:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon26912280](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon26912280](https://forum.arduino.cc/u/anon26912280)\
**Post date:** [August 18, 2021, 11:22am UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/1 "2021-08-18T11:22:28Z")

</div>

This code is designed to continuously blink an LED with Pot1 controlling timeOn and Pot2 controlling timeOff. When the button is pushed, the sequence starts and loops, however once running, the delays in the loop prevent the button from toggling off and keeping the LED low.

I know to make this work I need to use millis() so the code can loop freely without interfering with the button, but I'm having great difficulty controlling the single LED's on and off time with 2 Pots using millis().

Any and all help is greatly appreciated! Here's the reference code. It works only every few button pushes...

```cpp
int led = 2;
int Button = 3;
int pot1 = A4;
int pot2 = A5;
boolean toggle = false;

void setup() {
pinMode(led, OUTPUT);
pinMode(Button, INPUT_PULLUP);
}

void loop() {
int B = digitalRead(Button);        
int timeOn = analogRead(pot1);
timeOn = map(timeOn, 0, 1023, 50, 500);
int timeOff = analogRead(pot2);
timeOff = map(timeOff, 0, 1023, 50, 500);  

if (B == LOW){toggle = !toggle; delay(200);}
if (toggle == true){
  digitalWrite(led, HIGH); 
  delay(timeOn); 
  digitalWrite(led, LOW); 
  delay(timeOff);}
if (toggle == false){digitalWrite(led, LOW);}
}

```

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [August 18, 2021, 11:24am UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/2 "2021-08-18T11:24:31Z")

</div>

Try [here](https://forum.arduino.cc/t/1-l-e-d-2-potentiometers/896486/2)

---

<div class="post-metadata">

**Author:** ![anon26912280](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon26912280](https://forum.arduino.cc/u/anon26912280)\
**Post date:** [August 18, 2021, 11:50am UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/3 "2021-08-18T11:50:23Z")

</div>

I tried posting there but the replies seemed to fall into some sour discussion about my intentions for the project.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [August 18, 2021, 12:14pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/4 "2021-08-18T12:14:37Z")

</div>

Hello  
I think the IDE example named BlinkWithOutDelay will help.  
[https://www.arduino.cc/en/pmwiki.php?n=Tutorial/BlinkWithoutDelay#:~:text=Blink%20Without%20Delay.%20Sometimes%20you%20need%20to%20do,pass%2C%20your%20program%20will%20miss%20the%20button%20press](https://www.arduino.cc/en/pmwiki.php?n=Tutorial/BlinkWithoutDelay#:~:text=Blink%20Without%20Delay.%20Sometimes%20you%20need%20to%20do,pass%2C%20your%20program%20will%20miss%20the%20button%20press).

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [August 18, 2021, 2:59pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/5 "2021-08-18T14:59:06Z")

</div>

> [@anon26912280](#):
>
> I know to make this work I need to use millis() so the code can loop freely without interfering with the button, but I'm having great difficulty controlling the single LED's on and off time with 2 Pots using millis().

Start with File-\>Examples-\>02.Digital-\>BlinkWithoutDelay

When the LED switches from ON to OFF, take that opportunity to change the 'interval':

```arduino
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
      interval = map(analogRead(pot1), 0, 1023, 50, 500);
    } else {
      ledState = LOW;
      interval = map(analogRead(pot2), 0, 1023, 50, 500);
    }

```

Of course, if you do that without changing anything else you will get an error: "assignment of read-only variable 'interval'". That means "Take the 'const' keyword off of the declaration of 'interval' if you want to be able to change it!"

---

<div class="post-metadata">

**Author:** ![anon26912280](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon26912280](https://forum.arduino.cc/u/anon26912280)\
**Post date:** [August 18, 2021, 3:03pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/6 "2021-08-18T15:03:51Z")

</div>

From all of these suggestions (and more from another thread) I've cracked it.  
Toggling an LED with ON & OFF interval potentiometers.

```cpp
int timeOn = A3;
int timeOff = A7;
int Button = 4;
int led = 6;
boolean toggle = false;
unsigned long timeLedChanged = millis();
unsigned long period;
boolean ledOn = false;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
  digitalWrite(led, LOW);
}

void loop() {
int B = digitalRead(Button);        
int onVal = analogRead(timeOn);
onVal = map(onVal, 0, 1023, 50, 500);
int offVal = analogRead(timeOff);
offVal = map(offVal, 0, 1023, 50, 500);  
 
if (B == LOW){toggle = !toggle; delay(200);}  

if (toggle== true){ 
  if (millis() - timeLedChanged >= period){
    ledOn = !ledOn;
    timeLedChanged = millis(); 
    digitalWrite(led, ledOn); 
      if (ledOn){
      period = (onVal);}
      else{
      period = (offVal);}
     }  
     }
if (toggle == false){
ledOn = false; 
digitalWrite(led, LOW);}    

}

```

Thanks for all the help everyone!

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [August 17, 2021, 2:57pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/8 "2021-08-17T14:57:57Z")

</div>

If I understand the "intent" of your code correctly the button controls whether the LED is ON (and blinking based on potentiometers) or whether the LED is OFF all of the time. If you want the button to be responsive you will have to remove the delays and use millis().

---

<div class="post-metadata">

**Author:** ![anon26912280](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon26912280](https://forum.arduino.cc/u/anon26912280)\
**Post date:** [August 17, 2021, 3:04pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/9 "2021-08-17T15:04:31Z")

</div>

That's the correct function yes. Each example of millis() I've seen uses variables based on the time between changes. I think my issue is storing 2 lots of data from the pots and having them act upon the LED simultaniously, which I find easiest with delays.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [August 17, 2021, 3:04pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/10 "2021-08-17T15:04:48Z")

</div>

The following tutorials should help you out:

[State Change Detection](https://forum.arduino.cc/t/state-change-detection-in-the-21st-century/680899)

[Using millis() for timing](https://forum.arduino.cc/t/using-millis-for-timing-a-beginners-guide/483573)

---

<div class="post-metadata">

**Author:** ![anon26912280](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon26912280](https://forum.arduino.cc/u/anon26912280)\
**Post date:** [August 17, 2021, 3:05pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/11 "2021-08-17T15:05:59Z")

</div>

That's a fantastic link, thankyou ToddL1962!

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [August 17, 2021, 3:06pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/12 "2021-08-17T15:06:11Z")

</div>

> [@anon26912280](#):
>
> I think my issue is storing 2 lots of data from the pots and having them act upon the LED simultaniously, which I find easiest with delays.

You can use a little state machine which will make the code very easy to manage.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [August 17, 2021, 3:13pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/13 "2021-08-17T15:13:13Z")

</div>

@anon26912280  
If you want that the LED should bink only once when button is closed and released, then your sketch requires following modification that involes the resetting of the variable **state** after one pass. (Current sketch of Post-1 will continuously blink LED and it does when button is closed and released only for once.)

```cpp
int LED = 2;
int Button = 4;
int timeOn = A3;
int timeOff = A4;

boolean state = false;

void setup() 
{
 pinMode(LED, OUTPUT);
 pinMode(Button, INPUT_PULLUP);
}

void loop() 
{
 int B = digitalRead(Button);
 int onVal = analogRead(timeOn);
 onVal = map(onVal, 0, 1023, 50, 500);
 int offVal = analogRead(timeOff);
 offVal = map(offVal, 0, 1023, 50, 500);

 if (B == LOW) 
 {
   state = ! state; //Button closed change state
   delay(200);
 }
 if (state == true) 
 {
   digitalWrite(LED, HIGH);
   delay(onVal);
   digitalWrite(LED, LOW);
   delay(offVal);
   state = false;
 }
 /*
 if (state == false) 
 {
   digitalWrite(LED, LOW);
 }*/
}

```

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [August 17, 2021, 3:16pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/14 "2021-08-17T15:16:47Z")

</div>

Unless OP wants it to continuously blink until the button turns it off. That was the assumption of OP intent I made.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [August 17, 2021, 3:18pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/15 "2021-08-17T15:18:14Z")

</div>

As @anon26912280 has not mentioned his intention clearly, any reasonable assumption is justified. However, the bahavior of the sketch of Post-1 as described in Post-7 has been found after uploading it into Arduino MEGA.

---

<div class="post-metadata">

**Author:** ![anon26912280](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon26912280](https://forum.arduino.cc/u/anon26912280)\
**Post date:** [August 17, 2021, 3:33pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/16 "2021-08-17T15:33:27Z")

</div>

Sorry for the confusion folks, the code intends to be...

1. button push to activate.
2. continuous blink, with pot 1 controlling led HIGH time and pot 2 controlling led LOW time.
3. button push to stop and keep the led LOW.

I understand millis to a degree, however it's the merging of these 2 parameters acting onto a single led using millis which has me a bit stumped.

GolamMostafa the issue with using delays is that the continuous running of the led sequence is interfering with my ability to use the button with a debounce time applied.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [August 17, 2021, 4:24pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/17 "2021-08-17T16:24:31Z")

</div>

Here is a start. I did the button processing and created a skeleton for the state machine and timing.

```cpp
const int LED = 2;
const int Button = 4;
const int timeOn = A3;
const int timeOff = A4;

enum StateEnum
{
  STATE_IDLE,
  STATE_LEDON,
  STATE_LEDOFF
};

StateEnum state = STATE_IDLE;

void setup() 
{
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
}

void loop() 
{
  static int lastButtonState = digitalRead(Button);
  static unsigned long prevMillis;
  
  int buttonState = digitalRead(Button);
  unsigned long onVal = analogRead(timeOn);
  onVal = map(onVal, 0, 1023, 50, 500);
  unsigned long offVal = analogRead(timeOff);
  offVal = map(offVal, 0, 1023, 50, 500);
  unsigned long currMillis = millis();

  if (buttonState != lastButtonState)
  {
    delay(50);
    if (buttonState == LOW)
    {
      if (state == STATE_IDLE)
      {
        // set up to start flashing
        prevMillis = millis();
        digitalWrite(LED, HIGH);
        state = STATE_LEDON;
      }
      else
      {
        // Turn off LED go to idle state
        digitalWrite(LED, LOW);
        state = STATE_IDLE;
      }
    }
    lastButtonState = buttonState;
  }

  switch (state)
  {
    case STATE_IDLE:
      break;

    case STATE_LEDON:
      // check to see if on time has expired. If so then turn LED off and transition to STATE_LEDOFF
      break;

    case STATE_LEDOFF:
      // check to see if off time has expired. If so then turn LED on and transition to STATE_LEDON
      break;
  }
}

```

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [August 17, 2021, 4:41pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/18 "2021-08-17T16:41:47Z")

</div>

@anon26912280  
**Implementation using millis():**

```cpp
int LED = 2;
int Button = 4;
int timeOn = A3;
int timeOff = A4;
int B;

boolean state = false;

void setup()
{
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
}

void loop()
{
  B = digitalRead(Button);
  int onVal = analogRead(timeOn);
  onVal = map(onVal, 0, 1023, 50, 500);
  int offVal = analogRead(timeOff);
  offVal = map(offVal, 0, 1023, 50, 500);

  if (B == LOW)
  {
    state = ! state; //Button closed change state //true
    delay(200);
  }

  if (state == true)
  {
    digitalWrite(LED, HIGH);
    timeDelay(onVal);
    digitalWrite(LED, LOW);
    timeDelay(offVal);
  }
}

void timeDelay(unsigned long t)//500 000
{
  unsigned long prMillis = millis();
  while(millis() - prMillis <= t)
  {
    if(digitalRead(Button) == LOW)
    {
      digitalWrite(LED, LOW);
      state = !state;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 17, 2021, 5:13pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/19 "2021-08-17T17:13:35Z")

</div>

> [@GolamMostafa](#):
>
> has not mentioned his intention clearly, any reasonable assumption is justified

However, leads to an infinite number of reasonable assumptions... otherwise known as a long, drawn out thread.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [August 17, 2021, 5:30pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/20 "2021-08-17T17:30:22Z")

</div>

> [@anon57585045](#):
>
> However, leads to an **infinite number** of reasonable assumptions...

![reasonAssump](https://europe1.discourse-cdn.com/arduino/original/4X/4/b/7/4b7867c2a3986f00fd1891f478777135d4f933c3.png)

---

<div class="post-metadata">

**Author:** ![anon26912280](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@anon26912280](https://forum.arduino.cc/u/anon26912280)\
**Post date:** [August 18, 2021, 3:07pm UTC](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779/21 "2021-08-18T15:07:00Z")

</div>

From all of these suggestions (and more from another thread) I've cracked it.  
Toggling an LED with ON & OFF interval potentiometers.

```cpp
int timeOn = A3;
int timeOff = A7;
int Button = 4;
int led = 6;
boolean toggle = false;
unsigned long timeLedChanged = millis();
unsigned long period;
boolean ledOn = false;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
  digitalWrite(led, LOW);
}

void loop() {
int B = digitalRead(Button);        
int onVal = analogRead(timeOn);
onVal = map(onVal, 0, 1023, 50, 500);
int offVal = analogRead(timeOff);
offVal = map(offVal, 0, 1023, 50, 500);  
 
if (B == LOW){toggle = !toggle; delay(200);}  

if (toggle== true){ 
  if (millis() - timeLedChanged >= period){
    ledOn = !ledOn;
    timeLedChanged = millis(); 
    digitalWrite(led, ledOn); 
      if (ledOn){
      period = (onVal);}
      else{
      period = (offVal);}
     }  
     }
if (toggle == false){
ledOn = false; 
digitalWrite(led, LOW);}    

}

```

Thanks for all the help everyone!

[Next page](https://forum.arduino.cc/t/2-potentiometers-controlling-a-single-led/896779.md?page=2)
