Situation: I have a 2 channel relay to control powering on a 12v LED strip. I'll be using an Arduino Uno (that has been collecting way too much dust) with a rocker switch that I will test powering the led strip on and off.
The code provided works perfectly and I understand it enough to follow along however, how can I change the code such that when I toggle the rocker switch that the led strip turns on and off?
What is happening currently:
The code has been compiled and uploaded.
The led strip only turns on via the relay when I toggle the switch twice (which is to be expected since the code is for a push button and not a rocker)
I would greatly appreciate any kind of help on this! I have been googling my eyes out over here attempting this. Forgot to add that I tested the ezbutton toggle library by itself with said rocker switch on its own but I am unsure of how make it such that this library works the way I want it. I am thinking maybe add code for the signal pin in this library so it knows that the relay is there and to switch it off when there is an on/off event?
This is a bad idea. You're using code that's meant to make a button act like a switch and now you want the switch to act like a button so you can make it act like a switch. Just let it be a switch. IF it's just to power the leds then put it in place of the relay. If it does other things in the code then leave it on Arduino but treat it as a switch.
The button code probably looks for a press and release, which is the same as a switch being closed and then opened. That's two actions for a switch. You don't need all that. You just want one thing when the switch is closed and something else when it's open. The fact that it is a rocker means it already is a latching toggle. You just need an if statement and nothing else.
This code assumes that the switch is between the pin and ground. Change the pins to suit your setup.
int switchPIn = 6;
int relayPin = 7;
void setup(){
pinMode(switchPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
}
void loop(){
if (digitalRead(switchPin == LOW){
// Switch is closed so activate the relay
digitalWrite(relayPin, HIGH);
} else {
// switch is open kill the relay
digitalWrite(relayPin, LOW);
}
}
Thank you for your time and explanation. I must concede that I am quite green at this. The most I've done is turning on an LED ring so I figured that what I had here wasn't the most efficient way to accomplish the task. I've spent so much time searching for a step by step similar to my setup to no avail. I compiled the code you provided however unfortunately, the relay/led does not power on. I used the pins as-is. One switch wire is connected to a ground with the other to the switchpin.
Changed: switchPIn to switchPin
Changed: if (digitalRead(switchPin == LOW){ to if (digitalRead(switchPin == LOW)){
Can you show me how you wired things up? How did you power things?
The code I gave you was for demonstration. Since I don't know anything about your setup (because you didn't really say) that code wasn't specific to you. If you're wanting something you can copy and drop in, then you need to show all the details of what you have so someone can write to that.
Really, if all you want the switch to do is power the LEDs then cut the Arduino and the relay out of the equation and just use the switch to switch the power to the LEDs.
Ok, just wanted to be safe about it all. Since I had the Arduino laying around, I thought it might be an interesting project but If I can do that direct I'm all for simplicity too.