Getting the reading off two hall sensors to trigger a lock

Hi All,

So im working on a project that uses two Hall sensors and so far i can make a reading print from both and change independently when a magnet is in range. However the aim is that when both the hall sensors have a magnet in place it triggers a relay which unlocks an electromagnetic lock.

If anyone could advise on the right statements as at the moment it is triggering the lock opening when a magnet is on one hall specific hall sensor. Whereas i only want it to unlock when there are two magnets in place.


//Chess Hall Sensor Project
//using a Sunfounder Analog and Digital sensor module
* Hall Sensor1 
* D0 7
* VCC 5V

* Hall Sensor2 
* D0 2
* VCC 5V

const int MagLock = 13;//the Maglock attached to pin13
int sensorPin = A5; // select the input pin for the Hall
int hallPin = A0; // select the input pin for the Hall

int digitalPin=7; //D0 attach to pin7
int digitalHallPin=2; //D0 attach to pin2

boolean digitalValue= 0;// variable to store the value coming from pin7
boolean digitalHallValue=0;// variable to store the value coming from pin2

int state = HIGH;
int previous = LOW;
int reading;
void setup() 
pinMode(digitalPin && digitalHallPin, INPUT);//set the state of D7 & D2 as INPUT
pinMode(MagLock,OUTPUT);//set the state of pin13 as OUTPUT
Serial.begin(9600); // initialize serial communications at 9600 bps
void loop() 

int reading = digitalValue=digitalRead(digitalPin); //read the value of D7
digitalHallValue=digitalRead(digitalHallPin); //read the value of D2

Serial.print("Digital Value1 "); // print label to serial monitor 
Serial.println(digitalValue); //print the value of D0 in the serial

Serial.print("Digital Value2 "); // print label to serial monitor 
Serial.println(digitalHallValue); //print the value of D0 in the serial

// if (reading == HIGH && previous == LOW) { if (digitalHallValue == (state == HIGH)) state = LOW; else state = HIGH;
if( digitalValue && digitalHallValue == LOW )//if the value of D0 is HIGH
digitalWrite(MagLock,HIGH);//turn off the Lock
if( digitalValue && digitalHallValue ==HIGH)//else
digitalWrite(MagLock,LOW);//turn on the lock
pinMode(digitalPin && digitalHallPin, INPUT);

The logical-and of two pin numbers is going to be true. But "true" isn't a pin.

To set two pins, you need to use two lines of code.

Try to use more meaningful names for variables than "digitalValue". Try giving the sensors names and then using those names in your code. If one is the "top" sensor then you would have Top_Pin, Top_Value Top_Previous or whatever.