Want to delay an output after input is made, Part 2

I was advised to start a new thread as I will be posting new code. Original thread here: http://forum.arduino.cc/index.php?topic=190567.0

I wanted to delay a relay after a sensor was triggered. With the help of the group I was able to clean up the code and make this work. I’m sure it a bit messy but I’m getting the desired results. Any suggestions are appreciated but please be aware I have no programming background and am learning by just looking at the code. I have only applied the delay to output 1 as I’m sure I’ll get some feedback to streamline things XD

/*
This program has 3 Inputs and 8 Outputs. 
Inputs can be IR sensors: 1 wire to 5V, 1 to Ground and 1 to Input.
 */

const int ledPin = 13; // LED is connected to pin 13
const int pir1 = 2;  // Input 1 is on Pin 2
const int pir2 = 3;  // Input 2 is on Pin 3
const int pir3 = 4;  // Input 3 is on Pin 4
const int relay1 = 5;   // output to relay 1 pin 5
const int relay2 = 6;   // output to relay 2 pin 6
const int relay3 = 7;   // output to relay 3 pin 7
const int relay4 = 8;   // output to relay 4 pin 8
const int relay5 = 9;   // output to relay 5 pin 9
const int relay6 = 10;   // output to relay 6 pin 10
const int relay7 = 11;   // output to relay 7 pin 11
const int relay8 = 12;   // output to relay 8 pin 12
long previousMillis1 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long previousMillis2 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long previousMillis3 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long previousMillis4 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long previousMillis5 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long previousMillis6 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long previousMillis7 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long previousMillis8 = 0; // this value is constantly changed to match current time as program runs, it does not count up itself
long TriggerDelay1 = 5000; // delay of Output 1 (pin 5) untill next allowable trigger 
long TriggerDelay2 = 5000; // delay of Output 2 (pin 6) untill next allowable trigger
long TriggerDelay3 = 5000; // delay of Output 3 (pin 7) untill next allowable trigger
long TriggerDelay4 = 5000; // delay of Output 4 (pin 8) untill next allowable trigger
long TriggerDelay5 = 5000; // delay of Output 5 (pin 9) untill next allowable trigger
long TriggerDelay6 = 5000; // delay of Output 6 (pin 10) untill next allowable trigger
long TriggerDelay7 = 5000; // delay of Output 7 (pin 11) untill next allowable trigger
long TriggerDelay8 = 5000; // delay of Output 8 (pin 12) untill next allowable trigger
long RelayOnTime1 = 3000; // how long you want Output 1 to stay on, must subtract relay delay time
long RelayOnTime2 = 3000; // how long you want Output 2 to stay on, must subtract relay delay time
long RelayOnTime3 = 3000; // how long you want Output 3 to stay on, must subtract relay delay time
long RelayOnTime4 = 3000; // how long you want Output 4 to stay on, must subtract relay delay time
long RelayOnTime5 = 3000; // how long you want Output 5 to stay on, must subtract relay delay time
long RelayOnTime6 = 3500; // how long you want Output 6 to stay on, must subtract relay delay time
long RelayOnTime7 = 4000; // how long you want Output 7 to stay on, must subtract relay delay time
long RelayOnTime8 = 4500; // how long you want Output 8 to stay on, must subtract relay delay time
long RelayDelayTime1 = 1000; // relay delay after sensor trigger
long RelayDelayTime2 = 1000; // relay delay after sensor trigger
long RelayDelayTime3 = 1000; // relay delay after sensor trigger
long RelayDelayTime4 = 1000; // relay delay after sensor trigger
long RelayDelayTime5 = 1000; // relay delay after sensor trigger
long RelayDelayTime6 = 1000; // relay delay after sensor trigger
long RelayDelayTime7 = 1000; // relay delay after sensor trigger
long RelayDelayTime8 = 1000; // relay delay after sensor trigger

void setup() 
{
  delay(10000);  // Must be longer than longest prop duration, makes sure all relays are off when board is powered on
  pinMode(pir1, INPUT); // Input 1 = pin 2
  pinMode(pir2, INPUT); // Input 2 = pin 3
  pinMode(pir3, INPUT); // Input 3 = pin 4
  pinMode(relay1, OUTPUT);  // Output 1 = pin 5
  pinMode(relay2, OUTPUT);  // Output 2 = pin 6
  pinMode(relay3, OUTPUT);  // Output 3 = pin 7
  pinMode(relay4, OUTPUT);  // Output 4 = pin 8
  pinMode(relay5, OUTPUT);  // Output 5 = pin 9
  pinMode(relay6, OUTPUT);  // Output 6 = pin 10
  pinMode(relay7, OUTPUT);  // Output 7 = pin 11
  pinMode(relay8, OUTPUT);  // Output 8 = pin 12
}


void loop()
{
  int sensor_1 = digitalRead(pir1);
  int sensor_2 = digitalRead(pir2);
  int sensor_3 = digitalRead(pir3);
  unsigned long CurrentMillisCount = millis();
  int relayState1 = digitalRead(relay1);
  int relayState2 = digitalRead(relay2);
  int relayState3 = digitalRead(relay3);
  int relayState4 = digitalRead(relay4);
  int relayState5 = digitalRead(relay5);
  int relayState6 = digitalRead(relay6);
  int relayState7 = digitalRead(relay7);
  int relayState8 = digitalRead(relay8);

  if ((relayState1 == LOW) && (CurrentMillisCount - previousMillis1 > RelayOnTime1))
  {
    digitalWrite(relay1, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState1 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis1 > TriggerDelay1))
  {
    previousMillis1 = CurrentMillisCount;
  }
  if ((relayState1 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis1 > RelayDelayTime1))
  {
        digitalWrite(relay1, LOW); // activate device that requires LOW input
  }
  if ((relayState2 == LOW) && (CurrentMillisCount - previousMillis1 > RelayOnTime2))
  {
    digitalWrite(relay2, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState2 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis2 > TriggerDelay2))
  {
    previousMillis2 = CurrentMillisCount;
    digitalWrite(relay2, LOW); // activate device that requires LOW input
  }
   if ((relayState3 == LOW) && (CurrentMillisCount - previousMillis3 > RelayOnTime3))
  {
    digitalWrite(relay3, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState3 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis3 > TriggerDelay3))
  {
    previousMillis3 = CurrentMillisCount;
    digitalWrite(relay3, LOW); // activate device that requires LOW input
  }
   if ((relayState4 == LOW) && (CurrentMillisCount - previousMillis4 > RelayOnTime4))
  {
    digitalWrite(relay4, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState4 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis4 > TriggerDelay4))
  {
    previousMillis4 = CurrentMillisCount;
    digitalWrite(relay4, LOW); // activate device that requires LOW input
  }
   if ((relayState5 == LOW) && (CurrentMillisCount - previousMillis5 > RelayOnTime5))
  {
    digitalWrite(relay5, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState5 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis5 > TriggerDelay5))
  {
    previousMillis5 = CurrentMillisCount;
    digitalWrite(relay5, LOW); // activate device that requires LOW input
  }
   if ((relayState6 == LOW) && (CurrentMillisCount - previousMillis6 > RelayOnTime6))
  {
    digitalWrite(relay6, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState6 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis6 > TriggerDelay6))
  {
    previousMillis6 = CurrentMillisCount;
    digitalWrite(relay6, LOW); // activate device that requires LOW input
  }
   if ((relayState7 == LOW) && (CurrentMillisCount - previousMillis7 > RelayOnTime7))
  {
    digitalWrite(relay7, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState7 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis7 > TriggerDelay7))
  {
    previousMillis7 = CurrentMillisCount;
    digitalWrite(relay7, LOW); // activate device that requires LOW input
  }
   if ((relayState8 == LOW) && (CurrentMillisCount - previousMillis8 > RelayOnTime8))
  {
    digitalWrite(relay8, HIGH); // de-activate device that requires LOW input
  }

  if ((relayState8 == HIGH) && (sensor_1 == HIGH) && (CurrentMillisCount - previousMillis8 > TriggerDelay8))
  {
    previousMillis8 = CurrentMillisCount;
    digitalWrite(relay8, LOW); // activate device that requires LOW input
  }

  }
[\code]

http://forum.arduino.cc/index.php?topic=190567.msg1408869#msg1408869

  pinMode(relay1, OUTPUT);  // Output 1 = pin 5
  int relayState1 = digitalRead(relay1);

Why are you reading from output pins? Make relayState a global array and remember what you wrote to the relay pins.

AWOL:
http://forum.arduino.cc/index.php?topic=190567.msg1408869#msg1408869

I understand that arrays are the way to go, I’m just trying to crawl here before I run :slight_smile: As simple as it is, I have at least 4 hours into getting my delay to work and getting rid of some of the redundant issues in the code. The upside is that I’m actually starting to understand what is going on XD

PaulS:   pinMode(relay1, OUTPUT);  // Output 1 = pin 5   int relayState1 = digitalRead(relay1);

Why are you reading from output pins? Make relayState a global array and remember what you wrote to the relay pins.

I have no idea, will start reading up on arrays.