el wire steps

Can someone help me here?
Is there a way of "shortening" this code?, I can't get my head around arrays yet. This works the way I want, but I would like to use an Optical sensitive Resistance Sensor Module to only allow this all to happen at dusk. Also at some point, I would like to have sensors wireless, but for first working project, this will do for now. I have a rough block diagram, if it helps to understand what I am doing here.

// Stairs , original code by Daz3nt 2013
// modified 2015 by Skooter
// added 9th channel and inverter switched on pin A0
const int irup = 11;     // IR Beam at the bottom of the stairs
const int irdown = 12;     // IR Beam at the top of the stairs
const int partybutton = 13;     // Party button and 10k pulldown res to grnd pin 13
int ch1 = 2;
int ch2 = 3;
int ch3 = 4;
int ch4 = 5;
int ch5 = 6;
int ch6 = 7;
int ch7 = 8;
int ch8 = 9;
int ch9 = 10; // extra channel wired to 2 channel relay board (sub to ssr)
int ch10 = A0; // power to inverter wired to 2 channel relay
// int irup = 11; taken out  from original , as they are already declared
// int irdown = 12;
// int partybutton = 13;



// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  pinMode(ch1, OUTPUT);        // sets the digital pin as output
  pinMode(ch2, OUTPUT);      // sets the digital pin as output
  pinMode(ch3, OUTPUT);        // sets the digital pin as output
  pinMode(ch4, OUTPUT);      // sets the digital pin as output
  pinMode(ch5, OUTPUT);        // sets the digital pin as output
  pinMode(ch6, OUTPUT);      // sets the digital pin as output
  pinMode(ch7, OUTPUT);        // sets the digital pin as output
  pinMode(ch8, OUTPUT);      // sets the digital pin as output
  pinMode(ch9, OUTPUT);        // sets the digital pin as output
  pinMode(ch10, OUTPUT);   // sets the digital pin as output (inverter)
  pinMode(irup, INPUT);       // sets the digital pin as input
  pinMode(irdown, INPUT);    // sets the digital pin as input
  pinMode(partybutton, INPUT); // sets the digital pin as input
}


void loop() {
  // read the state of the ir sensor value:
  buttonState = digitalRead(irup);

  if (buttonState == 1) {
    // turn channels on:
    digitalWrite(ch10, HIGH);
    digitalWrite(ch1, HIGH);
    delay(300);
    digitalWrite(ch2, HIGH);
    delay(300);
    digitalWrite(ch3, HIGH);
    delay(300);
    digitalWrite(ch4, HIGH);
    delay(300);
    digitalWrite(ch5, HIGH);
    delay(300);
    digitalWrite(ch6, HIGH);
    delay(300);
    digitalWrite(ch7, HIGH);
    delay(300);
    digitalWrite(ch8, HIGH);
    delay(300);
    digitalWrite(ch9, HIGH);
    delay(300);
    // digitalWrite(ch10, HIGH); moved to line 51
    // delay(5000);
    digitalWrite(ch1, LOW);
    delay(1200);
    digitalWrite(ch2, LOW);
    delay(1100);
    digitalWrite(ch3, LOW);
    delay(1000);
    digitalWrite(ch4, LOW);
    delay(900);
    digitalWrite(ch5, LOW);
    delay(800);
    digitalWrite(ch6, LOW);
    delay(700);
    digitalWrite(ch7, LOW);
    delay(600);
    digitalWrite(ch8, LOW);
    delay(500);
    digitalWrite(ch9, LOW);
    // delay(400);
    // digitalWrite(ch10, LOW);

  }

  // read the state of the irdown value:
  buttonState = digitalRead(irdown);

  if (buttonState == 1) {
    // turn ch on:

    digitalWrite(ch10, HIGH); // keep on always
    // delay(300);
    digitalWrite(ch9, HIGH);
    delay(300);
    digitalWrite(ch8, HIGH);
    delay(300);
    digitalWrite(ch7, HIGH);
    delay(300);
    digitalWrite(ch6, HIGH);
    delay(300);
    digitalWrite(ch5, HIGH);
    delay(300);
    digitalWrite(ch4, HIGH);
    delay(300);
    digitalWrite(ch3, HIGH);
    delay(300);
    digitalWrite(ch2, HIGH);
    delay(300);
    digitalWrite(ch1, HIGH);
    delay(5000);
    // digitalWrite(ch10, LOW);
    // delay(1000);
    digitalWrite(ch9, LOW);
    delay(900);
    digitalWrite(ch8, LOW);
    delay(800);
    digitalWrite(ch7, LOW);
    delay(700);
    digitalWrite(ch6, LOW);
    delay(600);
    digitalWrite(ch5, LOW);
    delay(500);
    digitalWrite(ch4, LOW);
    delay(400);
    digitalWrite(ch3, LOW);
    delay(300);
    digitalWrite(ch2, LOW);
    delay(250);
    digitalWrite(ch1, LOW);
  }

  // read the state of the partybutton value:
  buttonState = digitalRead(partybutton);

  if (buttonState == 1) {
    // turn channel on:
    digitalWrite(ch10, HIGH);
    // delay(100);
    // digitalWrite(ch10, LOW);
    //delay(1);

    digitalWrite(ch9, HIGH);
    delay(200); // changed from 100 to 200
    digitalWrite(ch9, LOW); // original delays were too fast for el wire
    delay(50); // changed from 1 to 50
    digitalWrite(ch8, HIGH);
    delay(200);
    digitalWrite(ch8, LOW);
    delay(50);
    digitalWrite(ch7, HIGH);
    delay(200);
    digitalWrite(ch7, LOW);
    delay(50);
    digitalWrite(ch6, HIGH);
    delay(200);
    digitalWrite(ch6, LOW);
    delay(50);
    digitalWrite(ch5, HIGH);
    delay(200);
    digitalWrite(ch5, LOW);
    delay(50);
    digitalWrite(ch4, HIGH);
    delay(200);
    digitalWrite(ch4, LOW);
    delay(50);
    digitalWrite(ch3, HIGH);
    delay(200);
    digitalWrite(ch3, LOW);
    delay(50);
    digitalWrite(ch2, HIGH);
    delay(200);
    digitalWrite(ch2, LOW);
    delay(50);
    digitalWrite(ch1, HIGH);
    delay(200);
    digitalWrite(ch1, LOW);
    delay(50);
    digitalWrite(ch2, HIGH);
    delay(200);
    digitalWrite(ch2, LOW);
    delay(50);
    digitalWrite(ch3, HIGH);
    delay(200);
    digitalWrite(ch3, LOW);
    delay(50);
    digitalWrite(ch4, HIGH);
    delay(200);
    digitalWrite(ch4, LOW);
    delay(50);
    digitalWrite(ch5, HIGH);
    delay(200);
    digitalWrite(ch5, LOW);
    delay(50);
    digitalWrite(ch6, HIGH);
    delay(200);
    digitalWrite(ch6, LOW);
    delay(50);
    digitalWrite(ch7, HIGH);
    delay(200);
    digitalWrite(ch7, LOW);
    delay(50);
    digitalWrite(ch8, HIGH);
    delay(200);
    digitalWrite(ch8, LOW);
    delay(50);
    digitalWrite(ch9, HIGH);
    delay(200);
    digitalWrite(ch9, LOW);
    // delay(1);
    // digitalWrite(ch10, HIGH);
    // delay(100);
    // digitalWrite(ch10, LOW);
    delay(400);
    digitalWrite(ch1, HIGH);
    digitalWrite(ch2, HIGH);
    digitalWrite(ch3, HIGH);
    digitalWrite(ch4, HIGH);
    digitalWrite(ch5, HIGH);
    digitalWrite(ch6, HIGH);
    digitalWrite(ch7, HIGH);
    digitalWrite(ch8, HIGH);
    digitalWrite(ch9, HIGH);
    // digitalWrite(ch10, HIGH);  now it gets tricky alternating using 10 instead of 12
    //  delay(1000);
    digitalWrite(ch1, LOW);
    delay(500);
    // digitalWrite(ch10, LOW); //was 12, each channel decrease by 1
    // delay(500);
    digitalWrite(ch3, LOW);
    delay(500);
    digitalWrite(ch9, LOW);
    delay(500);
    digitalWrite(ch5, LOW);
    delay(500);
    digitalWrite(ch7, LOW);
    delay(500);
    digitalWrite(ch6, LOW);
    delay(500);
    digitalWrite(ch5, LOW);
    delay(500);
    digitalWrite(ch8, LOW);
    delay(500);
    digitalWrite(ch4, LOW);
    delay(500);
    digitalWrite(ch2, LOW);

  }

  // read the state of the irdown value:
  buttonState = digitalRead(irdown);

  if (buttonState == 0) {
    // turn channel off:

    digitalWrite(ch10, LOW);
    digitalWrite(ch9, LOW);
    digitalWrite(ch8, LOW);
    digitalWrite(ch7, LOW);
    digitalWrite(ch6, LOW);
    digitalWrite(ch5, LOW);
    digitalWrite(ch4, LOW);
    digitalWrite(ch3, LOW);
    digitalWrite(ch2, LOW);
    digitalWrite(ch1, LOW);

  }

  // read the state of the partybutton value:
  buttonState = digitalRead(partybutton);

  if (buttonState == 0) {
    // turn channels off:
    digitalWrite(ch10, LOW); 
    digitalWrite(ch9, LOW);
    digitalWrite(ch8, LOW);
    digitalWrite(ch7, LOW);
    digitalWrite(ch6, LOW);
    digitalWrite(ch5, LOW);
    digitalWrite(ch4, LOW);
    digitalWrite(ch3, LOW);
    digitalWrite(ch2, LOW);
    digitalWrite(ch1, LOW);

  }

  // read the state of the pushbutton value:
  buttonState = digitalRead(irup);

  if (buttonState == 0) {
    // turn channel off:
    digitalWrite(ch10, LOW); 
    digitalWrite(ch9, LOW);
    digitalWrite(ch8, LOW);
    digitalWrite(ch7, LOW);
    digitalWrite(ch6, LOW);
    digitalWrite(ch5, LOW);
    digitalWrite(ch4, LOW);
    digitalWrite(ch3, LOW);
    digitalWrite(ch2, LOW);
    digitalWrite(ch1, LOW);
  }
  else {
    // turn channel off:
    digitalWrite(ch1, LOW);
    digitalWrite(ch2, LOW);
    digitalWrite(ch3, LOW);
    digitalWrite(ch4, LOW);
    digitalWrite(ch5, LOW);
    digitalWrite(ch6, LOW);
    digitalWrite(ch7, LOW);
    digitalWrite(ch8, LOW);
    digitalWrite(ch9, LOW);
    digitalWrite(ch10, LOW); 

  }
}

stairs_EL_wire_10_channel_skooter.ino (8.02 KB)

With so many delays, you may have problems reading inputs. During a delay, nothing is done. So if someone sets off the IR during the delay, it may be missed.

Learn to do things using millis().

Weedpharma

Is there a particular reason you want to turn all the lights on with these delays ? Some visual effect ? Or power supply issue ? Why not turn them all on at once ?

You could write something like this

int ch_pins = { 3,4,5,6,7,8,9,10,11,12 }  ;  //  ten output channels
for ( int i=0 ; i<10 ; i++ )
{
     digitalWrite( ch_pin[i], HIGH ) ;
     delay(300) ;
}

which would be more compact code than what you currently have.

an Optical sensitive Resistance Sensor Module

I don't know what this is, but if it is just a fancy name for a light-sensitive resistor, you need to be reading and analog value from it, not a digital read.

However if your "module" includes some circuit to give you a digital on/off output, then the digital read would be appropriate.

michinyon:
Is there a particular reason you want to turn all the lights on with these delays ? Some visual effect ? Or power supply issue ? Why not turn them all on at once ?

You could write something like this

int ch_pins = { 3,4,5,6,7,8,9,10,11,12 }  ;  //  ten output channels

for ( int i=0 ; i<10 ; i++ )
{
    digitalWrite( ch_pin[i], HIGH ) ;
    delay(300) ;
}




which would be more compact code than what you currently have.

michinyon:
Is there a particular reason you want to turn all the lights on with these delays ? Some visual effect ? Or power supply issue ? Why not turn them all on at once ?

You could write something like this

int ch_pins = { 3,4,5,6,7,8,9,10,11,12 }  ;  //  ten output channels

for ( int i=0 ; i<10 ; i++ )
{
    digitalWrite( ch_pin[i], HIGH ) ;
    delay(300) ;
}




which would be more compact code than what you currently have.

Thank you for the reply and suggestion, I want the lights stage on , so it appears they are anticipating your steps up/down the garden path. Sort of like in the movie "Avatar"

weedpharma:
With so many delays, you may have problems reading inputs. During a delay, nothing is done. So if someone sets off the IR during the delay, it may be missed.

Learn to do things using millis().

Weedpharma

Thank you for the suggestion, I will give it a try , this works as is, but if it can be improved upon, I am all ears.

michinyon:
I don't know what this is, but if it is just a fancy name for a light-sensitive resistor, you need to be reading and analog value from it, not a digital read.

However if your "module" includes some circuit to give you a digital on/off output, then the digital read would be appropriate.

it has both analog and digital output capabilities, I am trying to figure out where to insert this particular module onto the uno board, and where I should start the code

sorry cant attach what I want, and can't seem to delete this post