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();
}

