Relay control with Buttons

Need advice/help on controlling relays with buttons for a stage lighting effect. So far I am able to turn on the 3 sequences with the 3 buttons (want to eventually have 12 sequences). What I need help with is making the sequence loop until I press another button .... and maybe a little guidance on "cleaning up" the code to make it more efficient/streamlined .... I have no previous C++ programming experience only the things I've picked up on the net and this forum so any and all help/criticism welcome.

link to code:

Ah, DropBox isn't available to some of us who might, just might, mind you, be goofing, err killing time while at work.

And chance you could put that sucker between a pair of code tags?

// Light Chaser v1 
// set pin numbers:
const int buttonPin[3] = {19, 20, 21,};     // the number of the pushbutton pin
const int ledPin[4] = {0, 1, 2, 3};      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status

 void setup() {
   pinMode(19, INPUT);      // set the switch pin to be an input
   pinMode(20, INPUT);
   pinMode(21, INPUT);
   pinMode(0, OUTPUT);    // Sets the output pins
   pinMode(1, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);  
   digitalWrite(19, HIGH);
   digitalWrite(20, HIGH);
   digitalWrite(21, HIGH);
 }


 void loop() {
   
   if (digitalRead(19) == LOW) {
     // if the switch is closed:
        digitalWrite(0, HIGH);
        delay(200);
        digitalWrite(1, HIGH);
        delay(200);
        digitalWrite(2, HIGH);
        delay(200);
        digitalWrite(3, HIGH);
        delay(200);
        digitalWrite(0, LOW);
        delay(200);
        digitalWrite(1, LOW);
        delay(200);
        digitalWrite(2, LOW);
        delay(200);
        digitalWrite(3, LOW);
        delay(200);
        digitalWrite(3, HIGH);
        delay(200);
        digitalWrite(2, HIGH);
        delay(200);
        digitalWrite(1, HIGH);
        delay(200);
        digitalWrite(0, HIGH);
        delay(200);
        digitalWrite(3, LOW);
        delay(200);
        digitalWrite(2, LOW);
        delay(200);
        digitalWrite(1, LOW);
        delay(200);
        digitalWrite(0, LOW);
        delay(200);  
   }
   if (digitalRead(20) == LOW) {
     // if the switch is closed:
        digitalWrite(0, HIGH);
        delay(200);
        digitalWrite(0, LOW);
        delay(200);
        digitalWrite(1, HIGH);
        delay(200);
        digitalWrite(1, LOW);
        delay(200);
        digitalWrite(2, HIGH);
        delay(200);
        digitalWrite(2, LOW);
        delay(200);
        digitalWrite(3, HIGH);
        delay(200);
        digitalWrite(3, LOW);
        delay(200);
        digitalWrite(0, HIGH);
        delay(200);
        digitalWrite(0, LOW);
        delay(200);
        digitalWrite(1, HIGH);
        delay(200);
        digitalWrite(1, LOW);
        delay(200);
        digitalWrite(2, HIGH);
        delay(200);
        digitalWrite(2, LOW);
        delay(200);
        digitalWrite(3, HIGH);
        delay(200);
        digitalWrite(3, LOW);
        delay(200);  
   }
   if (digitalRead(21) == LOW) {
     // if the switch is closed:
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        delay(200);
        digitalWrite(0, LOW);
        digitalWrite(1, LOW);
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        delay(50);
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        delay(150);
        digitalWrite(0, LOW);
        digitalWrite(1, LOW);
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        delay(50);
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        delay(150);
        digitalWrite(0, LOW);
        digitalWrite(1, LOW);
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        delay(50);
        digitalWrite(0, HIGH);
        digitalWrite(1, HIGH);
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        delay(150);
        digitalWrite(0, LOW);
        digitalWrite(1, LOW);
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        delay(50);  
   }
 }

During delay(), your Arduino just sits there doing nothing. Look at the Blink Without Delay example in the IDE to see how to overcome this.

Unfortunately had to go do something called WORK .... anyway, came up with this after reading for the last 3-4 hours.
I'm now having trouble with latching buttons, as each sequence will run continuously if I hold down the button, which can work if I can just implement the latch to work properly. examples on the net refer to only one button.... tried several different ways but can't get them to work properly.

the code below is what I came up with without the latching buttons. PLEASE HELP!

// attemp2
const int buttonPin[6] = {16, 17, 18, 19, 20, 21,};
int buttonState = 0;
int timer1 = 200;           // The higher the number, the slower the timing.
int timer2 = 300;
int timer3 = 150;
int timer4 = 100;
int ledPins1[] = {0, 1, 2, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount1 = 4;           // the number of pins (i.e. the length of the array)
int ledPins2[] = {0, 3, 0, 3, 1, 2, 1, 2 };       // an array of pin numbers to which LEDs are attached
int pinCount2 = 8;
int ledPins3[] = {0, 1, 2, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount3 = 4;
int ledPins4[] = {0, 1, 0, 2, 1, 2, 1, 3, 2, 3 };       // an array of pin numbers to which LEDs are attached
int pinCount4 = 10;

void setup() {
   pinMode(16, INPUT);      // set the switch pin to be an input
   pinMode(17, INPUT);
   pinMode(18, INPUT);
   pinMode(19, INPUT);      // set the switch pin to be an input
   pinMode(20, INPUT);
   pinMode(21, INPUT);
   digitalWrite(16, HIGH);
   digitalWrite(17, HIGH);
   digitalWrite(18, HIGH);
   digitalWrite(19, HIGH);
   digitalWrite(20, HIGH);
   digitalWrite(21, HIGH);
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount1; thisPin++)  {
    pinMode(ledPins1[thisPin], OUTPUT);
    }
  for (int thisPin = 0; thisPin < pinCount2; thisPin++)  {
    pinMode(ledPins2[thisPin], OUTPUT);    
  }
  for (int thisPin = 0; thisPin < pinCount3; thisPin++)  {
    pinMode(ledPins3[thisPin], OUTPUT);
}
  for (int thisPin = 0; thisPin < pinCount4; thisPin++)  {
    pinMode(ledPins4[thisPin], OUTPUT);    
  }
}
void loop() {
  if (digitalRead(16) == LOW) {
  for (int thisPin = 0; thisPin < pinCount1; thisPin++) { 
    // turn the pin on:
    digitalWrite(ledPins1[thisPin], HIGH);   
    delay(timer1);                  
    // turn the pin off:
    digitalWrite(ledPins1[thisPin], LOW);
  }
  
}
  if (digitalRead(17) == LOW) {
  for (int thisPin = pinCount2 - 1; thisPin >= 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins2[thisPin], HIGH);
    delay(timer1);
    // turn the pin off:
    digitalWrite(ledPins2[thisPin], LOW);
  }
}
  if (digitalRead(18) == LOW) {
   for (int thisPin = 0; thisPin < pinCount4; thisPin++) {
    digitalWrite(ledPins4[thisPin], HIGH);
    delay(timer1);
    digitalWrite(ledPins4[thisPin], LOW);
  }
}
  if (digitalRead(19) == LOW) {
  for (int thisPin = 0; thisPin < pinCount3; thisPin++) { 
    // turn the pin on:
    digitalWrite(ledPins3[thisPin], HIGH);   
    delay(timer3);                  
    // turn the pin off:
    digitalWrite(ledPins3[thisPin], LOW);    
  }
  for (int thisPin = pinCount3 - 1; thisPin >= 0; thisPin--) { 
    // turn the pin on:
    digitalWrite(ledPins3[thisPin], HIGH);   
    delay(timer3);                  
    // turn the pin off:
    digitalWrite(ledPins3[thisPin], LOW);    
  }
 }
}

I'm willing to abandon this if there's somebody out there with knowledge on getting DMX RECIEVE to work on a teensy 2 board.

ROG3R:
Unfortunately had to go do something called WORK

I vaguely remember doing something called that, before I retired :smiley:

I'm now having trouble with latching buttons, as each sequence will run continuously if I hold down the button, which can work if I can just implement the latch to work properly. examples on the net refer to only one button.... tried several different ways but can't get them to work properly.
the code below is what I came up with without the latching buttons. PLEASE HELP!

You only need to set each pin as an output once. ledPins3[] holds all four LED pins (0 to 3) so just use:

   for (int thisPin = 0; thisPin < pinCount3; thisPin++)  {
    pinMode(ledPins3[thisPin], OUTPUT);
}

to set them. There's no need for the other three for loops.
To overcome your buttons not latching, look up 'Finite State Machine' or else use latching switches. DIP switches might be what you need.