Help: Blinking multiple LEDs whilst using Potentiometer to Sequence them

Hi,

I have a potentiometer that basically turns the lights on/off in a sequence. I'd like to add in some code to also have them blink whilst the potentiometer is turning them on/off. But I'm running around in circles do to the idea of "multiple" states and when to declare a state for what led.

//Define pin inputs
const byte LED1 = 23,
           LED2 = 22,
           LED3 = 21,
           LED4 = 20,
           potentiometer = A0;

//define a variable for the potentiometer value
int potentiometervalue;           

void setup()
{
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
}

void loop()
{

  //potentiometervalue is send to the analogue input of the potentiometer
  potentiometervalue = analogRead(potentiometer);
  digitalWrite(LED1, potentiometervalue > 100);
  digitalWrite(LED2, potentiometervalue > 400); 
  digitalWrite(LED3, potentiometervalue > 800);
  digitalWrite(LED4, potentiometervalue > 1000);
}

^ my code. I'm already declaring the states of the individual leds using the values I get by changing the potentiometer. I'd like to also have them blink whilst this is happening. I'm using a teensy 3.5 by the way.

Anyway ideas?

UPDATE:

//Define pin inputs
const byte LED1 = 23,
           LED2 = 22,
           LED3 = 21,
           LED4 = 20,
           potentiometer = A0;

//define a variable for the potentiometer value
int potentiometervalue;   

int LED1state = LOW;
int LED2state = LOW;
int LED3state = LOW;
int LED4state = LOW;

long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillis4 = 0;

long interval = 10000;
long interval2 = 9000;
long interval3 = 8000;
long interval4 = 7000;

void setup()
{
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
}

void loop()
{

  //potentiometervalue is send to the analogue input of the potentiometer
  potentiometervalue = analogRead(potentiometer);
  digitalWrite(LED1, potentiometervalue > 100);
  digitalWrite(LED2, potentiometervalue > 400); 
  digitalWrite(LED3, potentiometervalue > 800);
  digitalWrite(LED4, potentiometervalue > 1000);

  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis1 > random(200, interval)) {
    // save the last time you blinked the LED 
    previousMillis1 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (LED1state == LOW)
      LED1state = HIGH;
    else
      LED1state = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED1, LED1state);
  }

  unsigned long currentMillis2 = millis();
  if(currentMillis2 - previousMillis2 > random(200, interval2)) {
    // save the last time you blinked the LED 
    previousMillis2 = currentMillis2;   

    // if the LED is off turn it on and vice-versa:
    if (LED2state == LOW)
      LED2state = HIGH;
    else
      LED2state = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED2, LED2state);
  }

  unsigned long currentMillis3 = millis();
  if(currentMillis3 - previousMillis3 > random(200, interval3)) {
    // save the last time you blinked the LED 
    previousMillis3 = currentMillis3;   

    // if the LED is off turn it on and vice-versa:
    if (LED3state == LOW)
      LED3state = HIGH;
    else
      LED3state = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED3, LED3state);
  }

  unsigned long currentMillis4 = millis();
  if(currentMillis4 - previousMillis4 > random(200, interval4)) {
    // save the last time you blinked the LED 
    previousMillis4 = currentMillis4;   

    // if the LED is off turn it on and vice-versa:
    if (LED4state == LOW)
      LED4state = HIGH;
    else
      LED4state = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(LED4, LED4state);
  }
}

The LEDs only blink when I turn the Potentiometer to low - basically off. However I can see that they still have a small bit of power and they blink sequentially. Which is good.

So I think my problem is to do with my initial states (for example "int LED1state = LOW;") and some overwrite happening with the value given by the potentiometer (for example "digitalWrite(LED4, potentiometervalue > 1000);").

I'm a pretty crap programmer, so any hand holding will be greatly appreciated.

long previousMillis1 = 0;
long previousMillis2 = 0;
long previousMillis3 = 0;
long previousMillis4 = 0;

Unless time can go backwards in your universe, these should be unsigned variables.

You have variables called LEDNstate, where N is 1 to 4. You do not ALWAYS write the value in LEDNstate to pin LEDN. Until you ALWAYS use LEDNstate in all calls to digitalWrite() that affect pin LEDN, you will continue to not know what state any pin is actually in.

You COULD assign the state variables values:

   LED1state = potentiometervalue >  100;
   LED2state = potentiometervalue >  400;
   LED3state = potentiometervalue >  800;
   LED4state = potentiometervalue > 1000;

and then write those state values to the appropriate pins.