Hi All,
I just can't seem to find any sample code that covers this, lots of examples of one button toggling the relay on and off with one button but I need to have 2 separate buttons.
One button turns the relay on
Another button turns the same relay off.
Sorry but I just cannot seem to figure this out, I know it must be simple but I am just staring on trying to learn all this. If someone has an example or knows a you tube video or link to this I thank you very much.
Thanks All
Show us your attempt at doing this.
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags,
use the </> icon in the posting menu.
[code]Paste your sketch here[/code]
Hi,
My attempts have not come close, I have been looking at the sample code provided by robojax. His code uses one button toggling the relay on and off. (I have found many dozens of examples of one button but not a single one with two buttons.) I didn't want to post someone else code.....
I was hoping that someone had seen an example somewhere. The code I have tried so far has all ended up in utter and complete failure.
We don’t know who robojax is.
Show us ‘your’ attempt.
When switch 1 is closed (pushed) turn on the relay.
When switch 2 is closed (released) turn off the relay.
const byte onSwitch = 2;
const byte offSwitch = 3;
const byte relay = 13;
void setup()
{
pinMode(onSwitch, INPUT_PULLUP);
pinMode(offSwitch, INPUT_PULLUP);
pinMode(relay, OUTPUT);
} //END of setup()
void loop()
{
if (digitalRead(onSwitch) == LOW)
{
digitalWrite(relay, HIGH);
}
if (digitalRead(offSwitch) == LOW)
{
digitalWrite(relay, LOW);
}
} //END of loop()
Bingo, That is it. Thanks so much LarryD, I now understand it a little bit better and your sample code does just what I needed.
I cannot believe I just couldn't figure this out, that is why it is so good to have folks like you on this forum..
Thanks Again.....
As an exercise add a comment to each line of code explaining what it does