I got this SPST on-off rocker switch for a project I'm building...
My current set up:
I have 5V power going into the outer silver lead, the middle silver lead going to a bread board supplying power to 26 leds, and the gold lead going to ground. Everything works as it should when I toggle between the two position on the switch (leds toggle on and off)...
I would like to add some more functionality to the switch, by making it set a boolean in my Arduino sketch...
Does anyone have any advice on how to wire this switch to accomplish this task? Do I need to get a relay or can I accomplish this with the switch alone?
Hi,
To tell the Arduino your LEDs are on you can simply take that LED supply out to an Arduino digital input via a large current limiting resistor (say 10k). You can read that value via digitalRead() or read it as an interrupt. That pin will also need a pulldown resistor to force a default 0 value.
Cheers! Geoff
Thanks strykeroz...this approach seems much simpler than the direction I was heading...
So I take the power supply from my 26 leds pass it through a 10 k resister and send it to a digital pin (32) which I read using digitalRead()...
I think I understand the theory of a pull down resistor, but am not sure how to wire it.
Do I attach digital pin (32) to another 10 k resistor that is connected to ground?
This is the code I plan to use read values and apply conditions based on LED states. Does this look right?
int inPin = 32; // LED power supply connected to digital pin 32 via 10k resistor
int val = 0; // variable to store the read value
void setup()
{
pinMode(inPin, INPUT); // sets the digital pin 32 as input
}
void loop()
{
val = digitalRead(inPin); // read the input pin
if(val==0){
//leds are OFF do something here
} else{
//leds are ON do something else here
}
}
Hi
That code will do the trick. And yes the 2nd resistor goes to GND so when the power is of your input pin is pulled low.
You may also need to debounce the returned value. Lots of examples via Google-Fu on how to do that to get a reliable value read.
Cheers! Geoff