Blink w/o delay 250410

Hello Arduino forum,

Have uploaded three sketches.
Blink_wo_delay_two_intervals_240828_ino
AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_230705.ino
and
AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino

The first sketch is just a test of the blink w/o delay function
to show the method.

In the second sketch, AllenDirectionality...ino, the heartbeat blink
is done by changing the state to the opposite of its present state.
So the interval off is the same as the interval on.

Would like to make the off different from the interval on. So I substituted
the blink without delay approach in the functions runledPin9();
and runledPin10(); in
AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino

LedPin9 is functioning as expected but LedPin10 is not.

Have scoured the code for hours. This is fallacy in checking your own work.

What am I missing?

Thanks.

Allen Pitts

Blink_wo_delay_two_intervals_240828_ino

/*
Sketch to test Seeed SAMSD12 using
blink w/o delay in three separate functions
WITH SEPARATE on AND off intervals
Tested positive 240828
*/

// constants won't change. Used here to set a pin number :
const int ledPin4 = 4;  // the number of the LED pin
const int ledPin6 = 6;  // the number of the LED pin
const int ledPin10 = 10;  // the number of the LED pin


// Variables will change :
int ledState4 = LOW;
int ledStateOff4 = LOW;
int ledState6 = LOW;
int ledStateOff6 = LOW;
int ledState10 = LOW;
int ledStateOff10 = LOW;


unsigned long previousMillisOn4 = 0;
unsigned long previousMillisOff4 = 0;
unsigned long previousMillisOn6 = 0;
unsigned long previousMillisOff6 = 0;
unsigned long previousMillisOn10 = 0;
unsigned long previousMillisOff10 = 0;

// constants won't change :
const long intervalOn4 = 1000;
const long intervalOff4 = 500;
const unsigned long intervals4[] {intervalOff4, intervalOn4};
const long intervalOn6 = 1500;
const long intervalOff6 = 750;
const unsigned long intervals6[] {intervalOff6, intervalOn6};
const long intervalOn10 = 2000;
const long intervalOff10 = 1000;
const unsigned long intervals10[] {intervalOff10, intervalOn10};

void setup() {
  // set the digital pin as output:
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin10, OUTPUT);
}

void loop() {
  runLED4();
  runLED6();
  runLED10();  
}

void runLED4() {
  unsigned long currentMillisOn4 = millis();

  if (currentMillisOn4 - previousMillisOn4 >= intervals4[ledState4])
  {
    // save the last time you blinked the LED
    previousMillisOn4 = currentMillisOn4;

    // if the LED is off turn it on and vice-versa:
    if (ledState4 == LOW) {
      ledState4 = HIGH;
    } else {
      ledState4 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin4, ledState4);

  }
}

void runLED6() {
  unsigned long currentMillisOn6 = millis();

  if (currentMillisOn6 - previousMillisOn6 >= intervals6[ledState6])
  {
    // save the last time you blinked the LED
    previousMillisOn6 = currentMillisOn6;

    // if the LED is off turn it on and vice-versa:
    if (ledState6 == LOW) {
      ledState6 = HIGH;
    } else {
      ledState6 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin6, ledState6);

  }
}

void runLED10() {
  unsigned long currentMillisOn10 = millis();

  if (currentMillisOn10 - previousMillisOn10 >= intervals10[ledState10])
  {
    // save the last time you blinked the LED
    previousMillisOn10 = currentMillisOn10;

    // if the LED is off turn it on and vice-versa:
    if (ledState10 == LOW) {
      ledState10 = HIGH;
    } else {
      ledState10 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin10, ledState10);
  }
}

AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_230705.ino

// AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_230705.ino
// Four LEDs, 2 operated by PIRs and two as indicator blinkers
// tested normal yymmdd
// Layout truth table:
// Staircase_PCB_Uno_Layout_w_direct_2_LEDS_230705.jpg

bool ascending = true;

int led4 = 5; //LED pin number
int led5 = 6; //LED pin number
int ledD2 = 9; //LED pin number
int ledD1 = 10; //LED pin number

int PIRA1 = A1; //PIR pin P5 on ...Layout_w_direct_230507.jpg
int PIRA2 = A2; //PIR pin P4 on ...Layout_w_direct_230507.jpg
int PIRA3 = A3; //PIR pin P3 on ...Layout_w_direct_230507.jpg

bool led4On = false; //Status of led4, set to off by default
long millisToTurnOffLed4 = 0; // This variable gets set to the time when led1 should turn off.
bool led5On = false; //Status of led5, set to off by default
long millisToTurnOffLed5 = 0; // This variable gets set to the time when led1 should turn off.
bool ledD2On = false; //Status of led1, set to off by default
long millisToTurnOffLedD2 = 0; // This variable gets set to the time when led1 should turn off.
bool ledD1On = false; //Status of led1, set to off by default
long millisToTurnOffLedD1 = 0; // This variable gets set to the time when led1 should turn off.

void setup() {
 
  pinMode(led4, OUTPUT); //setup led4 pin as output
  pinMode(PIRA1, INPUT); //setup PIRA1 as input

  pinMode(led5, OUTPUT); //setup led5 pin as output
  pinMode(PIRA2, INPUT); //setup PIRA0 as input

  pinMode(PIRA3, INPUT); //setup PIR12 as input

  pinMode(ledD2, OUTPUT); //setup led1 pin as output
  pinMode(ledD1, OUTPUT); //setup led1 pin as output

}

void runLed4() {

  digitalWrite(led4, led4On); //sets LED 4 to whichever state variable led4on says to.
  bool valA3 = digitalRead(PIRA2); //reads PIRA5's state

  if (valA3 == HIGH) { //This block is triggered if PIRA1 is activated
      led4On = true; //turns led1 on
    millisToTurnOffLed4 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed4) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led4On = false;
  }
}

void runLed5() {

  digitalWrite(led5, led5On); //sets LED5 to whichever state variable led2on says to.
  bool valA3 = digitalRead(PIRA3); //reads PIRA5's state

  if (valA3 == HIGH) { //This block is triggered if PIRA1 is activated
    led5On = true; //turns led5 on
    millisToTurnOffLed5 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed5) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led5On = false;
  }
}


void runLedD2() {

  digitalWrite(ledD2, ledD2On); //sets LED D2 to whichever state variable ledD2on says to.

  if (millis() >= millisToTurnOffLedD2) { 
    ledD2On = !ledD2On; //switches the state of LedD2On. if false, it switches to true and vice versa.
    millisToTurnOffLedD2 = millis() + 500; //sets timer. The LED should blink on and off every 1.5 seconds
  }

}

void runLedD1() {

  digitalWrite(ledD1, ledD1On); //sets LED D2 to whichever state variable ledD2on says to.

  if (millis() >= millisToTurnOffLedD1) { 
    ledD1On = !ledD1On; //switches the state of LedD2On. if false, it switches to true and vice versa.
    millisToTurnOffLedD1 = millis() + 1000; //sets timer. The LED should blink on and off every 1.5 seconds
  }

}



void runLed4Descending() {

  digitalWrite(led4, led4On); //sets LED 4 to whichever state variable led4on says to.
  bool valA1 = digitalRead(PIRA1); //reads PIRA5's state

  if (valA1 == HIGH) { //This block is triggered if PIRA1 is activated
    led4On = true; //turns led1 on
    millisToTurnOffLed4 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed4) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led4On = false;
  }
}

void runLed5Descending() {

  digitalWrite(led5, led5On); //sets LED5 to whichever state variable led2on says to.
  bool valA1 = digitalRead(PIRA2); //reads PIRA5's state

  if (valA1 == HIGH) { //This block is triggered if PIRA1 is activated
     led5On = true; //turns led5 on
    millisToTurnOffLed5 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed5) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led5On = false;
  }
}


void loop() {

  if (digitalRead(PIRA2)) { // sets ascending to true  if bottom sensor is tripped
    ascending = true;
  }
  if (digitalRead(PIRA1)) {
    ascending = false;
  }

  if (ascending) {
    runLed4();
    runLed5();
  
  }
  else {
    runLed4Descending();
    runLed5Descending();
    }

  runLedD2();
  runLedD1();

}


AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino

// AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_230705.ino
// Four LEDs, 2 operated by PIRs and two as indicator blinkers
// tested normal yymmdd
// Layout truth table:
// Staircase_PCB_Uno_Layout_w_direct_2_LEDS_230705.jpg

// constants won't change. Used here to set a pin number :
const int ledPin9 = 9;  // the number of the LED pin
const int ledPin10 = 10;  // the number of the LED pin



// Variables will change :
int ledState9 = LOW;
int ledStateOff9 = LOW;
int ledState10 = LOW;
int ledStateOff10 = LOW;


unsigned long previousMillisOn9 = 0;
unsigned long previousMillisOff9 = 0;
unsigned long previousMillisOn10 = 0;
unsigned long previousMillisOff10 = 0;

// constants won't change :
const long intervalOn9= 1000;
const long intervalOff9 = 500;
const unsigned long intervals9[] {intervalOff9, intervalOn9};
const long intervalOn10 = 2000;
const long intervalOff10 = 1000;
const unsigned long intervals10[] {intervalOff10, intervalOn10};

bool ascending = true;

int led4 = 5; //LED pin number
int led5 = 6; //LED pin number
//int ledPin9 = 9; //LED pin number
//int ledPin10 = 10; //LED pin number

int PIRA1 = A1; //PIR pin P5 on ...Layout_w_direct_230507.jpg
int PIRA2 = A2; //PIR pin P4 on ...Layout_w_direct_230507.jpg
int PIRA3 = A3; //PIR pin P3 on ...Layout_w_direct_230507.jpg

bool led4On = false; //Status of led4, set to off by default
long millisToTurnOffLed4 = 0; // This variable gets set to the time when led1 should turn off.
bool led5On = false; //Status of led5, set to off by default
long millisToTurnOffLed5 = 0; // This variable gets set to the time when led1 should turn off.
bool ledD2On = false; //Status of led1, set to off by default
long millisToTurnOffLedD2 = 0; // This variable gets set to the time when led1 should turn off.
bool ledD1On = false; //Status of led1, set to off by default
long millisToTurnOffLedD1 = 0; // This variable gets set to the time when led1 should turn off.

void setup() {
 
  pinMode(led4, OUTPUT); //setup led4 pin as output
  pinMode(PIRA1, INPUT); //setup PIRA1 as input

  pinMode(led5, OUTPUT); //setup led5 pin as output
  pinMode(PIRA2, INPUT); //setup PIRA0 as input

  pinMode(PIRA3, INPUT); //setup PIR12 as input

  pinMode(ledPin9, OUTPUT); //setup led1 pin as output
  pinMode(ledPin10, OUTPUT); //setup led1 pin as output

}

void runLed4() {

  digitalWrite(led4, led4On); //sets LED 4 to whichever state variable led4on says to.
  bool valA3 = digitalRead(PIRA2); //reads PIRA5's state

  if (valA3 == HIGH) { //This block is triggered if PIRA1 is activated
      led4On = true; //turns led1 on
    millisToTurnOffLed4 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed4) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led4On = false;
  }
}

void runLed5() {

  digitalWrite(led5, led5On); //sets LED5 to whichever state variable led2on says to.
  bool valA3 = digitalRead(PIRA3); //reads PIRA5's state

  if (valA3 == HIGH) { //This block is triggered if PIRA1 is activated
    led5On = true; //turns led5 on
    millisToTurnOffLed5 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed5) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led5On = false;
  }
}


void  runledPin9() {

    unsigned long currentMillisOn9= millis();

  if (currentMillisOn9 - previousMillisOn9 >= intervals9[ledState9])
  {
    // save the last time you blinked the LED
    previousMillisOn9 = currentMillisOn9;

    // if the LED is off turn it on and vice-versa:
    if (ledState9 == LOW) {
      ledState9 = HIGH;
    } else {
      ledState9 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin9, ledState9);

  }
}


void runledPin10() {
  unsigned long currentMillisOn10 = millis();

  if (currentMillisOn10 - previousMillisOn10 >= intervals10[ledState10])
  {
    // save the last time you blinked the LED
    previousMillisOn10 = currentMillisOn10;

    // if the LED is off turn it on and vice-versa:
    if (ledState10 == LOW) {
      ledState10 = HIGH;
    } else {
      ledState10 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin10, ledState10);

  }
}


void runLed4Descending() {

  digitalWrite(led4, led4On); //sets LED 4 to whichever state variable led4on says to.
  bool valA1 = digitalRead(PIRA1); //reads PIRA5's state

  if (valA1 == HIGH) { //This block is triggered if PIRA1 is activated
    led4On = true; //turns led1 on
    millisToTurnOffLed4 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed4) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led4On = false;
  }
}

void runLed5Descending() {

  digitalWrite(led5, led5On); //sets LED5 to whichever state variable led2on says to.
  bool valA1 = digitalRead(PIRA2); //reads PIRA5's state

  if (valA1 == HIGH) { //This block is triggered if PIRA1 is activated
     led5On = true; //turns led5 on
    millisToTurnOffLed5 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed5) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led5On = false;
  }
}


void loop() {

  if (digitalRead(PIRA2)) { // sets ascending to true  if bottom sensor is tripped
    ascending = true;
  }
  if (digitalRead(PIRA1)) {
    ascending = false;
  }

  if (ascending) {
    runLed4();
    runLed5();
  
  }
  else {
    runLed4Descending();
    runLed5Descending();
    }

  runledPin9();
  runledPin10();

}


You could be using a lot less code, consider that void loop() when not blocked runs dozens of times per millisecond so...

if the runLED function took a pin # arg, a single function could run many leds one at a time and still switch them all ON/OFF in the same milli().

You don't need currentMilli variables, just use mill().
You don't need to define const variables to make const arrays, just define the arrays with the numbers.

I have things to do for a while but I'll be back and if you haven't gotten to it, I'll take a shot at shortening your first code.

BTW, do you know that your IDE has a BWD example and that it contains minor errors that won't show up for over 24 days? I think you might since your code doesn't have those basic fouls... but you did start with SEC (someone else's code).

Hang in there! It's a real good sign that you do! If you don't get bugs, you're not pushing!

What does that mean? Is ledPin10 not blinking at all, or is it doing something else?

I quickly wired up a couple of LEDs on pin 9 and 10 of an R3 and they're both blinking with your sketch.

const unsigned long intervals10[] {intervalOff10, intervalOn10};

It always make me blink when I see this form of initialization. I know in my head that the = is now optional, but it still feels wrong; decades of habit are hard to shake.

@AllenPitts you have been on the forum for long enough to know that such statements are unhelpful.

We cannot see through your eyes to know what is happening with your LEDs. We cannot see into your mind to understand what you were expecting to happen.

And @AllenPitts can't see anything through his eyes, either, at least not the wealth of information about what is really flowing on in this code that a bit of the old serial printing would make manifest.

For example: what are the sensors reporting? Print the value in immediately you read it. Maybe the unit on that LED is not performing to specifications.

Like that.

a7

Compiles, not tested, example of shorter code:

/*
  blink w/o delay in three separate functions
  WITH SEPARATE on AND off intervals
*/

const byte leds = 3;
byte  ledIndex;
const byte ledPin[ ] = { 4, 6, 10 };  // the number of the LED pin
byte ledState[ ] = { 0, 0, 0 };

unsigned long previousMillis[ leds ];
const long intervalOn4 = 1000;
const long intervalOff4 = 500;
const unsigned long interval[ leds ][ 2 ] = { 500, 1000, 750, 1500, 1000, 2000 }; // OFF, ON

void setup( )
{
  for ( ledIndex = 0;  ledIndex < leds;  ledIndex++ )
  {
    pinMode( ledPin[ ledIndex ], OUTPUT );
  }
  ledIndex = 0;
}

void runLED( byte led ) 
{
  if (  millis() - previousMillis[ led ] >= interval[ led ][ ledState[ led ] ] )
  {
    previousMillis[ led ] += interval[ led ][ ledState[ led ] ];

    if (ledState[ led ] == 0 ) 
    {
      ledState[ led ] = 1;
    } 
    else 
    {
      ledState[ led ] = 0;
    }
    digitalWrite(  ledPin[ led ], ledState[ led ]  );
  }
}

void loop( ) 
{
  runLED(  ledIndex++  );
  if (  ledIndex == leds - 1  )
  {
    ledIndex = 0;
  }
}

@AllenPitts - I built your project and aside from not having any idea what it should do, I certainly see LED 9 and LED 10 blinking.

The PIR sensors are a mystery. I used pushbuttons instead, and fixed the logic so that pressing the button is the same as activated the corresponding PIR.

See it here, slightly modified and with the kind of printing I have suggested:

// AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_230705.ino
// Four LEDs, 2 operated by PIRs and two as indicator blinkers
// tested normal yymmdd
// Layout truth table:
// Staircase_PCB_Uno_Layout_w_direct_2_LEDS_230705.jpg

// constants won't change. Used here to set a pin number :
const int ledPin9 = 9;  // the number of the LED pin
const int ledPin10 = 10;  // the number of the LED pin



// Variables will change :
int ledState9 = LOW;
int ledStateOff9 = LOW;
int ledState10 = LOW;
int ledStateOff10 = LOW;


unsigned long previousMillisOn9 = 0;
unsigned long previousMillisOff9 = 0;
unsigned long previousMillisOn10 = 0;
unsigned long previousMillisOff10 = 0;

// constants won't change :
const long intervalOn9= 1000;
const long intervalOff9 = 500;
const unsigned long intervals9[] {intervalOff9, intervalOn9};
const long intervalOn10 = 2000;
const long intervalOff10 = 1000;
const unsigned long intervals10[] {intervalOff10, intervalOn10};

bool ascending = true;

int led4 = 5; //LED pin number
int led5 = 6; //LED pin number
//int ledPin9 = 9; //LED pin number
//int ledPin10 = 10; //LED pin number

int PIRA1 = A1; //PIR pin P5 on ...Layout_w_direct_230507.jpg
int PIRA2 = A2; //PIR pin P4 on ...Layout_w_direct_230507.jpg
int PIRA3 = A3; //PIR pin P3 on ...Layout_w_direct_230507.jpg

bool led4On = false; //Status of led4, set to off by default
long millisToTurnOffLed4 = 0; // This variable gets set to the time when led1 should turn off.
bool led5On = false; //Status of led5, set to off by default
long millisToTurnOffLed5 = 0; // This variable gets set to the time when led1 should turn off.
bool ledD2On = false; //Status of led1, set to off by default
long millisToTurnOffLedD2 = 0; // This variable gets set to the time when led1 should turn off.
bool ledD1On = false; //Status of led1, set to off by default
long millisToTurnOffLedD1 = 0; // This variable gets set to the time when led1 should turn off.

void setup() {
  Serial.begin(115200);
  Serial.println("\nLED 9?\n");

  pinMode(PIRA2, INPUT_PULLUP); //setup PIRA0 as input
  pinMode(PIRA1, INPUT_PULLUP); //setup PIRA1 as input
  pinMode(PIRA3, INPUT_PULLUP); //setup PIR12 as input

  pinMode(led4, OUTPUT); //setup led4 pin as output
  pinMode(led5, OUTPUT); //setup led5 pin as output
  pinMode(ledPin9, OUTPUT); //setup led1 pin as output
  pinMode(ledPin10, OUTPUT); //setup led1 pin as output
}

# define PIRACTIVE LOW   // i am using pushbuttons wired pulled up

void runLed4() {

  digitalWrite(led4, led4On); //sets LED 4 to whichever state variable led4on says to.
  bool valA3 = digitalRead(PIRA2) == PIRACTIVE; //reads PIRA5's state

  if (valA3) { //This block is triggered if PIRA1 is activated
      led4On = true; //turns led1 on
    millisToTurnOffLed4 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed4) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led4On = false;
  }
}

void runLed5() {

  digitalWrite(led5, led5On); //sets LED5 to whichever state variable led2on says to.
  bool valA3 = digitalRead(PIRA3) == PIRACTIVE; //reads PIRA5's state

  if (valA3) { //This block is triggered if PIRA1 is activated
    led5On = true; //turns led5 on
    millisToTurnOffLed5 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed5) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led5On = false;
  }
}


void  runledPin9() {

    unsigned long currentMillisOn9= millis();

  if (currentMillisOn9 - previousMillisOn9 >= intervals9[ledState9])
  {
    // save the last time you blinked the LED
    previousMillisOn9 = currentMillisOn9;

    // if the LED is off turn it on and vice-versa:
    if (ledState9 == LOW) {
      Serial.println("            LED 9 goes ON");
      ledState9 = HIGH;
    } else {
      Serial.println("            LED 9 goes OFF");
      ledState9 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin9, ledState9);
  }
}


void runledPin10() {
  unsigned long currentMillisOn10 = millis();

  if (currentMillisOn10 - previousMillisOn10 >= intervals10[ledState10])
  {
    // save the last time you blinked the LED
    previousMillisOn10 = currentMillisOn10;

    // if the LED is off turn it on and vice-versa:
    if (ledState10 == LOW) {
      Serial.println("   LED 10 goes ON");
      ledState10 = HIGH;
    } else {
      Serial.println("   LED 10 goes OFF");
      ledState10 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin10, ledState10);

  }
}


void runLed4Descending() {

  digitalWrite(led4, led4On); //sets LED 4 to whichever state variable led4on says to.
  bool valA1 = digitalRead(PIRA1) == PIRACTIVE; //reads PIRA5's state

  if (valA1) { //This block is triggered if PIRA1 is activated
    led4On = true; //turns led1 on
    millisToTurnOffLed4 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed4) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led4On = false;
  }
}

void runLed5Descending() {

  digitalWrite(led5, led5On); //sets LED5 to whichever state variable led2on says to.
  bool valA1 = digitalRead(PIRA2) == PIRACTIVE; //reads PIRA5's state

  if (valA1) { //This block is triggered if PIRA1 is activated
     led5On = true; //turns led5 on
    millisToTurnOffLed5 = millis() + 3000; //sets our stopwatch. This variable is set to the current time + 1.5 seconds. (millis() + 1500)
  }

  if (millis() >= millisToTurnOffLed5) { //continually checks to see if our current time ( millis() ) is equal to our "stopwatch", if so, we turn the led1 off;
    led5On = false;
  }
}


void loop() {

  if (digitalRead(PIRA2) == PIRACTIVE) { // sets ascending to true  if bottom sensor is tripped
    ascending = true;
  }
  if (digitalRead(PIRA1) == PIRACTIVE) {
    ascending = false;
  }

  if (ascending) {
    runLed4();
    runLed5();
  
  }
  else {
    runLed4Descending();
    runLed5Descending();
    }

  runledPin9();
  runledPin10();

}

I was thinking you probably made a copy/paste error as you grew your code. You may have heard it said that when you are making new variables by appending a letter or digit to the name of an exist variable, you should think about using arrays.

And you may have heard it said that when you are growing code by copy/edit/pasting, you should unleash the true power of functions and write a function that exploits the arrays you've used so that one function can blink any blinking LED you configure.

And one day, you will realize that all most of those comments are gratuitous. Anyone can read the code, there is no need to say again what lines of code do, like

  a = b + c;      // add b and c and store in a

It's just noise, and when the comments aren't even right they are real distraction.

HTH

a7

Hello GoForSmoke, van_der_decken, alto777, and the Arduino forum,

Ok, there was not enough context given in Post #1 April 10.
Thats on me.

Post #1 April 10 should have clarified

  1. AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_230705.ino
    operates as expected. That is, ledPin9 (D2 on schematic)
    and ledPin10 (D1 on schematic) oscillate as spec'd in
    the sketch.

  2. But in AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_230705 both
    ledPin9 (D2 on schematic) both and ledPin10 (D1 on schematic)
    are on or off for equal amounts of time because the functions
    void runLedD1() {
    and
    void runLedD2() {
    simply do a comparison
    ledD2On = !ledD2On;
    and if the LED is on it turns the LED off and conversely.

  3. What was intended was to use the blink w/o delay approach in
    AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino
    so the on and off inervals are unequal. For instance,
    in the reposted
    AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino
    LED10 is on for a quarter second then off for three seconds.

AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino
is now working with asymentrical on and off intervals.

I am a bit red faced to admit the reason why ledPin10
did not turn on when ...250410.ino
was loaded was because ledPin10 had a loose solder.
Because, by coincidence, ledPin10 oscillated as expected
when ...230705.ino was loaded and failed to come on
when ...250410.ino was loaded it was conjectured that
the defect was in the sketch.

alto777 is right. Post #1 April 10 assumen many facts not in
evidence because the focus was on why ledPin10 failed
to ocillate. Here is the bigger picture that should
have been painted in Post #1 April 10.

The project is called the Staircase System. It operates
in two modes: ascending and descending. When the PIR at the
bottom of the staircase is triggered the lower LED is
lit and the ascending the
void runLed5Ascending() {
function runs which waits for the middle PIR to be
triggered which lights the upper LED.

The descending mode is triggered by the PIR at the
top of the staircase. When the upper PIR is
triggered, which lights the upper LED and the descending function,
void runLed4Descending() {
runs. runLed4Descending then waits for the middle PIR to be
triggered which lights the lower LED.

Thanks for feedback.

Allen Pitts
,
PS GoForSmoke thanks for tips on using arrays.
I hate to admit it but the code, written by a contractor,
is way over my head.
In fact, I am struggling to get the PIRs and LEDs to operate
as designed.
It was specifically requested that a more verbose, arrayless
sketch be written. If the sketch without arrays is
beyond my ability to analyze and understand code,
imagine how far out to sea I would be id the added
complexities of code is introduced.

Presently LED4 stays on all the time (should go off
when not triggered for a time interval) and blinks at the
rate set at
const long intervalOn10 = 250; //red
const long intervalOff10 = 3000
and the LED connected to Atduino pin 10
is not doing anything.

LED5 comes on when triggered and goes off
after the spec' thirty seconds but blinks
at the same rate as LED5. That is when
LED5 comes on it blnks at the rate
that D1 is supposed to.

So somehow i have got the wires crossed
between D1 and LED4 and LED5 and not
sure how to troubleshoot the problem.
If you think you could help me out
I would start a new topic at the Arduino forum.

It is my experience that big code with mainly repeated sections is harder to get through and get right but....

Your mileage may vary.

If you never tackled arrays;

1 dimensional arrays are like numbered rows of boxes that you can drop values into and take them out by array name and element number.
All the boxes have the same name but a different number.
It's like the floors of a tall building, same address/different floors.

2 dimensional arrays have rows and columns, 2 numbers.
In the tall building, each floor has different apartments.

Timing the leds; 1 dimension is which led and the other is how long off and on for that led. The array is # of leds by 2 times for each... organized to be together at [ led # ][ Off or On Time ]. And you can sequence the leds from first to last or last to first or any other way yinz (Pittsburgh word for y'all) want.

The same name with numbered elements aspect of arrays makes writing one function to do a thing to all of the elements possible and eliminates name per element, keep those all straight coding. It is worth spending time messing with arrays to get used to them because in the long run you will save a great deal more time than the learning took.

The thing about state and blinking is that if it is ON, you turn it OFF and if it is OFF you turn it ON.
But I'm not sure that you want repeated blinking so much as turn the led ON for a while then turn it OFF until it gets triggered again... what is called a 1-shot. For that you only need one time interval per led, a 1D array that relates time to led number.

If you take your time planning the details and order out before writing the code, it gets easier.

Hello @GoForSmoke and the Arduino forum,

Shout out to GoforSmoke for the excellent discourse on arrays.
A feint glimpse of the difference between one dimensional and two dimensional arrays is perceived and is much appreciated.

Unfortunately, this writer is about 120 IQ.

The many elements of the code in
AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino
are barely within my grasp as they exist. In fact, successful code for the
Staircase System was written using arrays. The Project
Manager (Allen Pitts) went back to the Coding Contractor
and suffered a multi-hour charge for redrafting the sketch
in a more loquacious version which lists each input and output variable
and the variables used to store values, individually.

Adding the complexities of arrays would remove an understanding
of the sketch further from my grip.

It is realized that this offends the excellent efficiencies afforded
by using arrays.

Individual listing of variables counters the clever use of pithy code
to do powerful operations with fewer characters.

But alas, what good is the clever code nine months from when it is successfully compiled and a change, however minor, is required and cannot be installed
because the complexities of the sketch cannot be fathomed.

It is with gratitude that GoFor Smoke's superior explanation and edification
that the shortened sketch is perused. And with dismay that it is known that the limitations of my cerebral abilities impede a fuller comprehension.

Despite my relatively diminished capacity it is hoped a solution to the
combination of the schematic
Staircase_Schematic_3_PIRs_2_LEDs_250412.jpg
and the sketch
AllenDirectionality_3PIR_2LEDS_WTwoBlinkers_250410.ino
can be attained.

Several approaches to a solution have been considered:

  1. A series of DMM tests at key junctions such as the power and output
    of the PIRS, the inputs and outputs of the Uno, and the source to ground
    voltage at Q5 and Q4.
  2. A circuit simulator like WOKWI
    https://wokwi.com/
  3. Building a new PCB with a plan for testing subsections of the circuit
    as they are added.

Do any of these avenues appear to be productive?

Without expectation of an ability to provide adequate requital
I remain,
Sir,
Your humble servant

Allen Pitts

Ok you wrote a lot of detailed descriptions.
But I do net have yet the overview.

Would you mind to write a pretty short summary in normal words = no code of what the LEDs are supposed to do?

OK read more what was posted above

I recommend that you use SELF-explaining names for everything.
As long as you use names like "led4", "PIRA1" etc. your brain has to do translation-work what that exactly means.

ah led4 is the LED at the bottom of the stairs, PIRA1 is the PIR at the top of the stairs etc.

There are 3 PIR-sensors
one PIR-Sensir at the bottom of the stairs let's name it the bottomPIR
one PIR-Sensor in the middle of the stairs let's name it the middlePIR
one PIR-Sensor at the top of the stairs let's name it the topPIR

and there are 3 LEDs
bottomLED
middleLED
topLED

So you have two sequences:

ascending:

  • if bottomPIR is triggered switch on bottomLED
  • if bottomLED is switched on check if middlePIR is triggered
  • if middlePIR is triggered switch on topLED

descending:

  • if topPIR is triggered switch on topLED
  • if topLED is switched on check if middlePIR is triggered
  • if middlePIR is triggered switch on bottomLED

there is some additional timing involved

expand my description with the timings in normal words (no code yet!)

There is another language-element that is best suited for such sequences.
Once you have expanded my description I can write down the code for it.

If it is explained based on your description it will be much easier to understand than a non-related example

I would stick with simple.

If a PIR first triggers, light the matching light for at least X seconds. If it keeps triggering, extend the lit period.

Using 1D arrays for pin # and start time, the whole sketch might take 20 or so lines.
When help is needed, short sketches get more helpers and quicker solutions.

We have ways to deal with the problems the OP doesn't seem to know about, like commenting code with full explanations from the planning up but I only started writing code for pay in 1980 and what do I know?
If I can save someone time, I'll try. Often it takes many hours to save... years.