Problem
I'm stuck on triggering different switch. I'll explain what each of my switch should do. Output of this setup : No output from relayPin
mainPin
- Main power of the switch to control
relayPin
— keeps on/off - Override
reedPin
state - Let
SecondState
reedPin
- Secondary power switch to control
relayPin
— depends onmainPin
on/off state - Overridden by
mainPin
- Let
FirstState
*another explanation in comment
Is it caused by coding or hardware? Pardon my ignorance, it is my beginner code. I would appreciate any help, thank you.
Code
const int reedPin = 8; // Reed Switch
const int mainPin = 9; // Main Switch
const int relayPin = 6; // Relay
int FirstState = 0;
int SecondState = 0;
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(reedPin, INPUT);
pinMode(mainPin, INPUT);
}
void loop() {
FirstState = digitalRead(reedPin);
SecondState = digitalRead(mainPin);
if (FirstState == HIGH) {
digitalWrite(relayPin, LOW);
}
else if (SecondState == HIGH) {
digitalWrite(relayPin, HIGH);
}
else {
digitalWrite(relayPin, HIGH);
}
}
Only else if (SecondState == HIGH) {
works in output.
Schematics
1 Answer 1
Draw a truth table and look at it from there. From your words, it is confusing. From your code it looks like this:
mainPin reedPin relayPin
HIGH LOW LOW
HIGH HIGH LOW
LOW LOW HIGH
LOW HIGH HIGH
So:
void loop() {
digitalWrite(relayPin, !digitalRead(mainPin));
}
If you want:
mainPin reedPin relayPin
HIGH LOW LOW
HIGH HIGH LOW
LOW LOW HIGH
LOW HIGH LOW
then:
void loop() {
digitalWrite(relayPin, !(digitalRead(mainPin) || digitalRead(reedPin)));
}
SW1
is main switch - referred asfirstState
in code. when this is off,U1
(referred assecondState
in code) has no function WhatU1
does is simple on/off (reed switch). WhatSW1
does enablesU1
function Sorry for confusion.