1

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 on mainPin 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

enter image description here

  • 1. Your problem statement is very hard to understand. A pair of switches can be in four different states (off/off, off/on, on/off, on/on): please clearly state what the program should do in each of these four cases. 2. Do not connect the relay directly to the Arduino: first search the Web for “connect relay to Arduino”, then use a suitable transistor. – Edgar Bonet Dec 5 '17 at 14:59
  • Thank you for your response, actually I'm using relay alone on schematics because I'm using module instead. There is 2 switch, first one acts like normal and override second switch which are reed switch. IF first one is on, then second one has a function to on/off. IF the first one is off, then second one has NO function – Muhammad Nur Aliif Dec 5 '17 at 15:02
  • Only you know what “first one acts like normal and override second switch” means. Again, please, tell us what the Arduino should do in each of the four possible situations. If you are unable to tell us what your Arduino should do, then you will never be able to program it to do what you want. – Edgar Bonet Dec 5 '17 at 15:06
  • Alright, first switch SW1 is main switch - referred as firstState in code. when this is off, U1 (referred as secondState in code) has no function What U1 does is simple on/off (reed switch). What SW1 does enables U1 function Sorry for confusion. – Muhammad Nur Aliif Dec 5 '17 at 15:13
  • The first one "enables" the second one ? You are missing a pinMode(switchMain, INPUT). Please use 'relayPin' instead of 'relaySwitch'. Use consistant names for everything. Please use always the same name for the first one, second one, and so on. Both in your sketch and in your explanation. Update your question and the sketch as well. – Jot Dec 5 '17 at 15:15
0

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)));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.