Searching for a creative solution for an "if" statement

Noob here, I have been working on a project using an esp8266 with a r503 fingerprint sensor which includes a relay for a door lock, a buzzer and a reed switch for sensing if the door has been opened without fingerprint being authorized, which should turn on the buzzer.
But I cant manage to find a creative solution for my problem.
Also Is there a way to digitalRead a pin that is in output mode.
I dont know many options with coding like using the while function or boolean etc... but please can someone guide me whats my best way to make this run.
I have posted the code i was trying but didnt work for me

ex

if(digitalRead(switchReed !=LOW) && (digitalRead(relayPin == High) {  //or some other solution from you guys
   serialPrint("Authorized access")
} else if (digitalRead(switchReed !=LOW) && (digitalRead(relayPin != High) {
   serialPrint("Intruder alert")
   digitalWrite(buzzerAlarm, HIGH)
}

Please post the complete sketch or a smaller but complete sketch that illustrates the problem

relayPin == High

Surely gave a compiler error unless you have defined your own version of the built in HIGH constant.

A couple of errors:
1:

if(digitalRead(switchReed !=LOW) && (digitalRead(relayPin == High) {

non-matching parenthesis, and "High" is spelled "HIGH":

~~ ~~if(digitalRead(switchReed !=LOW) && (digitalRead(relayPin == HIGH)) {~~ ~~

if(digitalRead(switchReed) !=LOW && digitalRead(relayPin) == HIGH) {

2:
Same for the third line:

} else if (digitalRead(switchReed) !=LOW) && (digitalRead(relayPin) != HIGH) {

3:
serialPrint() (lines 2 and 4) is not correct, you mean serial.print().

4:
Line 2, 4 and 5 must have a semicolon at the end.

I much prefer to simplify complex IF tests - for example like this

if(digitalRead(switchReed !=LOW) {
    if (digitalRead(relayPin == HIGH) {  //or some other solution from you guys
       serialPrint("Authorized access")
    } else {
        serialPrint("Intruder alert")
        digitalWrite(buzzerAlarm, HIGH)
   }
}

Better still, I like to read the pins prior to the IF tests as that allows me to print the values that are being considered by the tests if I need to. It also ensures that all the IF tests work from the same data.

reedSwitchState = digitalRead(switchReed);
relayState = digitalRead(relayPin);
if(reedSwitchState !=LOW) {
    if (relayState == HIGH) {  //or some other solution from you guys
       serialPrint("Authorized access")
    } else {
        serialPrint("Intruder alert")
        digitalWrite(buzzerAlarm, HIGH)
   }
}

...R

Even better is to handle the buttons with a debouncing library first, then you can code
entirely in terms of events (button up, button down), which hides a whole lot of
detail about the interface and keeps debouce logic out of the business logic (separation
of concerns is good!).

However this can be overkill in simple cases - probably good to have in a MIDI controller,
unnecessary if just lighting an LED!

Erik_Baas:
A couple of errors:
1:

if(digitalRead(switchReed !=LOW) && (digitalRead(relayPin == High) {

needs one more closing parenthesis, and "High" is spelled "HIGH":

if(digitalRead(switchReed !=LOW) && (digitalRead(relayPin == HIGH)) {

That will read digital pin 0 or 1, not relayPin. Same for switchReed.

You're right David, I missed those. It's fixed now.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.