// Define the pins for the magnetic door contacts, the relays, and the touch sensor
int doorContact1Pin = 2;
int doorContact2Pin = 3;
int relay1Pin = 4;
int relay2Pin = 5;
int touchSensorPin = 6;
// Setup the pins for input and output
void setup() {
pinMode(doorContact1Pin, INPUT);
pinMode(doorContact2Pin, INPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(touchSensorPin, INPUT);
}
// Loop forever and check the state of the magnetic door contacts and the touch sensor
void loop() {
int touchSensorState = digitalRead(touchSensorPin);
int relay1State = digitalRead(relay1Pin);
// If the touch sensor state is HIGH and the relay is LOW, toggle the relay
if (touchSensorState == HIGH && relay1State == LOW) {
digitalWrite(relay1Pin, HIGH);
} else if (touchSensorState == HIGH && relay1State == HIGH) {
digitalWrite(relay1Pin, LOW);
}
// If the first magnetic door contact is closed, turn on the first relay
if (digitalRead(doorContact1Pin) == HIGH) {
digitalWrite(relay1Pin, HIGH);
} else {
digitalWrite(relay1Pin, LOW);
}
// If the second magnetic door contact is closed, turn on the second relay
if (digitalRead(doorContact2Pin) == HIGH) {
digitalWrite(relay2Pin, HIGH);
} else {
digitalWrite(relay2Pin, LOW);
}
}