NO DELAY

I apologize for another delay without delay question but after searching, I can still not find some definitive answers. I am working on a project that requires LEDs to stay on for a certain amount of time without using the delay() function. I have already incorporated the BlinkWithoutDelay program and it still does not do what I need. Here is the code that I am working on. After

case 1:
        if(Program_State == 1)
        {
        for(int i=0; i < MAX; i++)
        {
          digitalWrite(LEDs[i], LOW);
        }
          digitalWrite(LEDs[random(2)], HIGH);
          break;
        }

I want the LED that is randomly chosen to stay on for a certain amount of milliseconds without using the delay() function. Then the program will move on to the next set of LEDs. Right now, the program switches LEDs really fast and I don’t want that.

int LED0 = 2;
int LED1 = 3;
int BUTTON1 = 0;
int LED8 = 11;
int LED9 = 12;
int BUTTON2 = 1;
const int LED2 = 4;
const int LED3 = 5;
int PIEZO = 8;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 9;
const int LED7 = 10;
const int MAX = 6;
int LEDs[MAX] = {LED2, LED3, LED4, LED5, LED6, LED7};
int State = LOW;
long previous = 0;
long interval = 100;
int Program_State = 0;

void setup()
{
  for(int i=0; i < MAX; i++)
  {
    pinMode(LEDs[i], OUTPUT);
  }
  pinMode(LED0, OUTPUT);
  pinMode(LED1, OUTPUT);
  pinMode(BUTTON1, INPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(BUTTON2, INPUT);
}

void loop()
{
  if(digitalRead(BUTTON1) == HIGH)
  {
    digitalWrite(LED0, HIGH), digitalWrite(LED1, LOW);
  }
  else
  {
    digitalWrite(LED1, HIGH), digitalWrite(LED0, LOW);
  }
  if(digitalRead(BUTTON2) == HIGH)
  {
    digitalWrite(LED8, HIGH), digitalWrite(LED9, LOW);
  }
  else
  {
    digitalWrite(LED9, HIGH), digitalWrite(LED8, LOW);
  }

  unsigned long current = millis();
  if(current - previous >= interval)
  {
    previous = current;
    Program_State = Program_State + 1;
      if(Program_State == 7)
      {
        Program_State = 1;
      }
      switch(Program_State)
      {
        case 1:
        if(Program_State == 1)
        {
        for(int i=0; i < MAX; i++)
        {
          digitalWrite(LEDs[i], LOW);
        }
          digitalWrite(LEDs[random(2)], HIGH);
          break;
        }
         case 2:
         if(Program_State == 2)
         {
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(2,4)], HIGH);
           break;
         }
         case 3:
         if(Program_State == 3)
         {
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(4,6)], HIGH);
          break;
         }
         case 4:
         if(Program_State == 4)
         {
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(4,6)], HIGH);
          break;
         }
         case 5:
         if(Program_State == 5)
         {
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(2,4)], HIGH);
           break;
         }
         case 6:
         if(Program_State == 6)
         {
         for(int i=0; i < MAX; i++)
         {
           digitalWrite(LEDs[i], LOW);
         }
           digitalWrite(LEDs[random(2,4)], HIGH);
           break;
         }         
    }
  }
}

What is this doing?

    digitalWrite(LED0, HIGH), digitalWrite(LED1, LOW);

Do you mean:

 digitalWrite(LED0, HIGH);
 digitalWrite(LED1, LOW);

(Note the semicolon).


 switch(Program_State)
      {
        case 1:
        if(Program_State == 1)
...

Surely Program_State will be 1 here? Why test it again? Ditto for the other places.

The code I have does the same thing as your suggestion just in one line. It is for a button that I am controlling. For more information on my program, I have a post on the Project Guidance thread called My Arduino Game. I am new so I don't know how to give you a direct link to it and I apologize.
I had previously mispelled case like Case and it didn't work. I just haven't deleted the unnecessary checks which I am glad you told me they were unnecessary because I would have had no idea.

Maybe post the simplified code?

Meanwhile I am just confused. How many LEDs do you have?

int LED0 = 2;
int LED1 = 3;
int BUTTON1 = 0;
int LED8 = 11;
int LED9 = 12;
int BUTTON2 = 1;
const int LED2 = 4;
const int LED3 = 5;
int PIEZO = 8;
const int LED4 = 6;
const int LED5 = 7;
const int LED6 = 9;
const int LED7 = 10;
const int MAX = 6;
int LEDs[MAX] = {
  LED2, LED3, LED4, LED5, LED6, LED7};
int State = LOW;
long previous = 0;
long interval = 100;
int Program_State = 0;

How are the buttons wired? Normally open or closed? With a pull-up resistor or something?

Half of your loop function seems to be testing two switches and turning a couple of LEDs on and off, and the rest turns them all off, and then one randomly on. What are you really expecting to happen?

The code I have does the same thing as your suggestion just in one line.

No, it doesn't. There is a big difference between a comma and a semicolon.

and it still does not do what I need.

You'll need to describe what it does do, what you need, and how the two differ.

Right now, the program switches LEDs really fast and I don't want that.

Perhaps that is because you have defined interval to be 1/10th of a second.

MathematicsFanatic:
For more information on my program, I have a post on the Project Guidance thread called My Arduino Game.

Oh, I see. Here:

Please, please don't start two threads asking questions about the same piece of code. It just fragments the answers and confuses everyone. I note in the other thread (now that I read it) that the issue of the commas has already come up.

My mistake. I thought I did have it on 1000 milliseconds. I am very sorry for the confusion.