If statement range logic problem

I'm using an LDR here in this script. My script outputs a value to the variable lockedValue once in my script and then maximum and minimum values are calculated based on the lockedValue. I also have a third variable that updates with the LDR called updatedValue and I'm creating a range where if the updatedValue goes above or below the max and min, the if statement triggers. Right now, the values are all printing correctly to console and the min and max values are correct in relative to the lockedValue but the if statement doesn't trigger as it should be. Here's the range code minus the trigger for lockedValue.

int lockedValue = 0; //this value triggers from a sensor once
int updatedValue = 0; //this value is constantly pulled from sensor
int Minimum = 0;
int Maximum = 0;

void loop() { 
  Minimum = lockedValue - 10; //minimum value is calculated 
  Maximum = lockedValue + 10; //maximum value is calculated

  Serial.println("====================");
  Serial.println(lockedValue);
  Serial.println(updatedValue );
  Serial.println(Minimum);
  Serial.println(Maximum);

  if(updatedValue >= Minimum || updatedValue <= Maximum) { 
    Serial.println("Trigger");
  }
}

Am I doing this correctly? Or is this wrong?

Where does updatedValue get updated?

12Stepper:
Where does updatedValue get updated?

it gets pulled from the LDR every second

int ldrSensor = A0;

void loop() {
    updatedValue = analogRead(ldrSensor); //LRD Sensor data 
}

It would make life a lot simpler all round if you just posted the whole sketch...

(See #6 here)

12Stepper:
It would make life a lot simpler all round if you just posted the whole sketch...

(See #6 here)

Right

//these values are for LRD sensor
int ldrSensor = A0;
int ldrValue = 0; 

//these values are for PIR sensor
int pirSensor = 2;
int pirState = LOW;
int pirValue = 0;

//these values are for RFID sensor
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
boolean RFIDState = false; //stores RFID state from cardModule

//these values are for timer
boolean activeState = false; //active state
unsigned long triggerTime; //time that PIR sensor was triggered
unsigned long activeTime; //time for script to be active
unsigned long ULTime; //current time

//these values are for security code
int ldrcStart = 0;
int ldrcMinimum = 0;
int ldrcMaximum = 0;
boolean SecurityTrigger = false;

void setup() {
  pinMode(pirSensor, INPUT); //PIR sensor pin activate
  SPI.begin(); //RFID begin
  mfrc522.PCD_Init(); //RFID library activation

  pinMode(8, OUTPUT); //LED active state
  
  Serial.begin(9600);
}

void loop() {
  ldrModule();
  pirModule();
  cardModule();
  
  //code for timer
  if(activeState == true) { //trigger code when pirModule detect motion
    ULTime = millis() - triggerTime;
    activeTime = ULTime / 1000UL;
    //Serial.println(activeTime);

    SecurityActive();
    
    if(activeTime == 30){ //waits 30 seconds then deactivates active state
      activeState = false;
      Serial.println("Script deactivated");
      digitalWrite(8, LOW); //LED active state
    }
    if(RFIDState == true) {
      activeState = false;
      RFIDState == false;
      Serial.println("Script deactivated");
      digitalWrite(8, LOW); //LED active state
    }
  }
}

void SecurityActive() { //checksum script that triggers mp3 to scream
  ldrcMinimum = ldrcStart - 10;
  ldrcMaximum = ldrcStart + 10;
  Serial.println("====================");
  Serial.println(ldrcStart);
  Serial.println(ldrValue);
  Serial.println(ldrcMinimum);

  if(ldrcMinimum <= ldrcStart) { 
    SecurityTrigger = true;
    Serial.println("Trigger");
    delay(3000);
  }
}

void ldrModule() {
  ldrValue = analogRead(ldrSensor); //LRD Sensor data 
//  Serial.print("LDR Value: ");Serial.println(ldrValue);
//  delay(100);
}

void pirModule() {
  pirValue = digitalRead(pirSensor); //PIR Sensor code
  if (pirValue == HIGH) {
    if (pirState == LOW) {
      pirState = HIGH;
      activeState = true;
      triggerTime = millis();
      Serial.println("Script activated");
      digitalWrite(8, HIGH); //LED active state
      ldrcStart = ldrValue; //locks value to this
    }
  }
  else {
    if (pirState == HIGH) {
      Serial.println("PIR sensor delay resynced!");
      pirState = LOW;
    }
  }
}

void cardModule() {
  if(! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  // Select one of the cards
  if(! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for(byte i = 0; i < mfrc522.uid.size; i++) {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if(content.substring(1) == "29 79 76 A3" || "39 8B AE 99") {//change here the UID of the card/cards that you want to give access
    Serial.println("Access granted");
    Serial.println();
    delay(3000);
    RFIDState = true; //makes it true for script
  }
 else{
    Serial.println("Access denied");
    delay(3000);
    RFIDState = false; //makes it false for script
  }
}

And then see #6 again :wink: , where it also says "With coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code."

So post complete code in the sense that it must be a sketch that compiles and runs, and shows the problem, but doesn't require other hardware like in your case the rfid and pir.

I get that you were trying not to clutter things, but there's no way anyone can run or comment on a sketch that isn't complete.

I'm going to hazard a guess that the problem's to do with the delays.

From the opening post

  if(updatedValue >= Minimum || updatedValue <= Maximum) {

Serial.println("Trigger");
  }

Minimum equals -10, Maximum equals +10.

Pick a number for updatedValue and check the outcome of the if; use e.g. -20, 0 and +20. The result will always be true.

You probably want a logic AND (&&) instead of a logic OR (||).

sterretje:
You probably want a logic AND (&&) instead of a logic OR (||).

oh yeah... good spot: I should have seen that (blush)

12Stepper:
And then see #6 again :wink: , where it also says "With coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code."

So post complete code in the sense that it must be a sketch that compiles and runs, and shows the problem, but doesn't require other hardware like in your case the rfid and pir.

I get that you were trying not to clutter things, but there's no way anyone can run or comment on a sketch that isn't complete.

I'm going to hazard a guess that the problem's to do with the delays.

Ah sorry about that.

sterretje:
From the opening postMinimum equals -10, Maximum equals +10.

Pick a number for updatedValue and check the outcome of the if; use e.g. -20, 0 and +20. The result will always be true.

You probably want a logic AND (&&) instead of a logic OR (||).

Shouldn't i be using the logic OR(||) since it can either be max or minimum instead of logic AND(&&) ?

Frixionmc:
Shouldn't i be using the logic OR(||) since it can either be max or minimum instead of logic AND(&&) ?

Say the minimum is 50 and the max is 80...

You're testing to see if a number is >50 or < 80.

Case 20: is <80, test passes.
Case 60: is >50 and <80, test passes
Case 90: is >50, test passes.

But if you use and:

Case 20: <50, test fails
Case 60: >50 and < 80, test passes
Case 90: >80, test fails.

Frixionmc:
Shouldn't i be using the logic OR(||) since it can either be max or minimum instead of logic AND(&&) ?

As I said, fill in some numbers. Further it's not quite clear to me what you mean.

If you need a trigger if it's smaller than Minimum or greater than Maximum, you need to reverse the the compares.

  if(updatedValue < Minimum || updatedValue > Maximum) {
    Serial.println("Trigger");
  }