Run tutorial not working

Hello everyone,
I'm having trouble making the run tutorial, I found here, work with my program. My program worked before I added the run portion. Can someone please see if you can tell where I went wrong?

// Next I will replace each LED with a motor or other function
//adjusting the delays accordingly
int run;
int LED = 11;
int leds[8] = {2, 3, 4, 5, 6, 7, 8, 9};    //To control the 8 LEDS--for test only
int ranNum;
int ranDel;
int buttonPin;



void setup() {
  run = 0;
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);    // Setup 8 LEDs as Outputs
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(LED, OUTPUT);    //shows the switch is pushed
  buttonPin = 12;
  pinMode(buttonPin, INPUT_PULLUP);


  randomSeed(analogRead(A0));     // unused (floating) analog pin.
  
}
void loop() {
 //code you always run here; you can leave this section blank if you want the entire program to stop and start, or add code here if you want it to always run

   //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
   if(digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
   {
      if(run == 0)
      {
          run = 255;
      }
      else
      {
          run = 0;
      }
   }

   if(run > 0)
   {
      //code you only run if button was pressed, stops running when button pressed again, so forth...

   }
}
void runPgm() {                 //start program again

  ranNum = random(2, 10);      //Generate random number between 8 and 12
  ranDel = random(100, 200);          // random delay time

  {
    int randChoose = random(8);
    switch (randChoose)
    {
      case 0 :  digitalWrite(2, HIGH);
        delay(4000);
        digitalWrite(2, LOW);
        delay(4000);
        runPgm();
      case 1 :  digitalWrite(3, HIGH);
        delay(4000);
        digitalWrite(3, LOW);
        delay(4000);
        runPgm();
      case 2 :  digitalWrite(4, HIGH);
        delay(4000);
        digitalWrite(4, LOW);
        delay(4000);
        runPgm();
      case 3 :  digitalWrite(5, HIGH);
        delay(4000);
        digitalWrite(5, LOW);
        delay(4000);
        runPgm();
      case 4 :  digitalWrite(6, HIGH);
        delay(4000);
        digitalWrite(6, LOW);
        delay(4000);
        runPgm;
      case 5 :  digitalWrite(7, HIGH);
        delay(4000);
        digitalWrite(7, LOW);
        delay(4000);
        runPgm();
      case 6 :  digitalWrite(8, HIGH);
        delay(4000);
        digitalWrite(8, LOW);
        delay(4000);
        runPgm();
      case 7 :  digitalWrite(9, HIGH);
        delay(4000);
        digitalWrite(9, LOW);
        delay(4000);
        runPgm();

    }
  }
}

What are you trying to do in loop()? You are not calling anything.

void loop() {
 //code you always run here; you can leave this section blank if you want the entire program to stop and start, or add code here if you want it to always run

   //check button press here and if it is pressed then toggle run variable between 0 and 255; REQUIRED!
   if(digitalRead(buttonPin) == LOW) //funcitons based off of button pulling input pin LOW
   {
      if(run == 0)
      {
          run = 255;
      }
      else
      {
          run = 0;
      }
   }

   if(run > 0)
   {
      //code you only run if button was pressed, stops running when button pressed again, so forth...

   }
}

Also in your runPgm() function (which you never call) you have recursive calls with no way to end the recursion. You will run out of memory and the Arduino will crash.

You never call runPgm and it’s a bad idea to have runPgm call itself.

Use the loop as intended... read tutorials

void setup() {
  run = 0;

At this point, “run” is already zero.

J-M-L:
You never call runPgm and it’s a bad idea to have runPgm call itself.

Use the loop as intended... read tutorials

J-M-L

My case program used break after each case. When I added more to the loop I had to change it to something else

My case program used break after each case. When I added more to the loop I had to change it to something else

I have absolutely no idea what any of that means.

For you, recursion is BAD - don’t do it.

I’m having trouble making the run tutorial, I found here,

You found what, where?

Okay, maybe this would be an easier way to go. The attached program does what I want it to. What would be the best way code it to turn it off and on with a button?

// Next I will replace each LED with a motor or other function
//adjusting the delays accordingly

int LED = 11;
int leds[8] = {2, 3, 4, 5, 6, 7, 8, 9};    //To control the 8 LEDS--for test only
int ranNum;
int ranDel;


void setup() {

  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);    // Setup 8 LEDs as Outputs
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
 
  randomSeed(analogRead(A0));     // unused (floating) analog pin.
  
}
void loop() {                 
  ranNum = random(2, 10);      //Generate random number between 8 and 12
  ranDel = random(100, 200);          // random delay time

  {
    int randChoose = random(8);
    switch (randChoose)
    {
      case 0 :  digitalWrite(2, HIGH);
        delay(4000);
        digitalWrite(2, LOW);
        delay(4000);
        break;
      case 1 :  digitalWrite(3, HIGH);
        delay(4000);
        digitalWrite(3, LOW);
        delay(4000);
        break;
      case 2 :  digitalWrite(4, HIGH);
        delay(4000);
        digitalWrite(4, LOW);
        delay(4000);
        break;
      case 3 :  digitalWrite(5, HIGH);
        delay(4000);
        digitalWrite(5, LOW);
        delay(4000);
        break;
      case 4 :  digitalWrite(6, HIGH);
        delay(4000);
        digitalWrite(6, LOW);
        delay(4000);
        break;
      case 5 :  digitalWrite(7, HIGH);
        delay(4000);
        digitalWrite(7, LOW);
        delay(4000);
        break;
      case 6 :  digitalWrite(8, HIGH);
        delay(4000);
        digitalWrite(8, LOW);
        delay(4000);
        break;
      case 7 :  digitalWrite(9, HIGH);
        delay(4000);
        digitalWrite(9, LOW);
        delay(4000);
        break;

    }
  }
}

It depends on how responsive you want it to be.
You could simply read the button at the top of loop() and make the rest of loop conditional, but that would be very unresponsive once those delays kick-in.

If you need to be responsive, get rid of the delays.

Polliwog:
What would be the best way code it to turn it off and on with a button?

Button action is momentary or push on/push off?

dougp:
Button action is momentary or push on/push off?

Yes, I know

Thanks everyone.