issue with toilet open/closed sign with 3 leg rocker and leds

Im building a toilet "open/closed" sign, where green is for "open" and red is for "closed". I also have a switch that has 3 legs: power to switch, power to the target and ground. I made the code so, that it reads the switch state (HIGH or LOW) to change the LED lights. Its a rocker switch with a blue indicator light.

For some reason it doesnt quite work, the lights flicker, mostly the green light turns on and when i move the wires slightly, rarely the red light turns on.

code:

void setup() {
pinMode(3,INPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
#define punaneLed 4
#define rohelineLed 5
#define lyliti 3

}

void loop() {
if (digitalRead(lyliti) == HIGH){
  digitalWrite(punaneLed,HIGH);
  digitalWrite(rohelineLed,LOW);
}
else if (digitalRead(lyliti) == LOW){
  digitalWrite(punaneLed,LOW);
  digitalWrite(rohelineLed,HIGH);
}
else{
  digitalWrite(punaneLed,LOW);
  digitalWrite(rohelineLed,LOW);
}
}

Help with code or circuit is very appreciated!

The way You describe the switch is not like normal standards. 3 terminals, that’s okey. How many positions can You put it in? 2 or 3 positions?

I guess the switch has 3 positions beacuase of Reading Your code.

How is it wired to the controller? Make a small drawing and attach.

This last Peace of code will never execute. pinilie is either LOW or HIGH. There is no third alternative.

}
else{
  digitalWrite(punaneLed,LOW);
  digitalWrite(rohelineLed,LOW);
}
}

unzboz1:
Help with code or circuit is very appreciated!

Some visual information about your circuit would be appreciated, i.e., a diagram or photo.

Connect a 10k resistor between pin3 and GND.

unzboz1:
For some reason it doesnt quite work, the lights flicker, mostly the green light turns on and when i move the wires slightly, rarely the red light turns on.

That sounds like a bad connection.

...R

I’m lost as to why you need a micro-controller, Arduino or otherwise. All you need is 1 single pole double throw switch, for example a standard 2 way light switch at a cost of about £1 from any DIY store, a power supply and 2 lights of some kind.

PerryBebbington:
I'm lost as to why you need a micro-controller, Arduino or otherwise. All you need is 1 single pole double throw switch, for example a standard 2 way light switch at a cost of about £1 from any DIY store, a power supply and 2 lights of some kind.

That is pretty much what the toilets in my work premises uses. It controls the magnetic lock. :grinning:

I was initially puzzled by the connection between a "3 leg rocker" and the toilet. :roll_eyes: