I am working on project in which I have to take inputs from two different switches and give to arduino and it should on (or) off the light or relay according to inputs. Like, if there are two switches (s1 & s2). If both switches are in “off” condition and when I switched “on” s1 the light should “on”. And after I switched “on” s2 then light should stay “on”. when s2 is “off” light should “off” and now s1 will be in “on” condition then if I switch “off” s1 and “on” again immediately now light should “on”.
This is the code i wrote but result is not expected. Result is like if s1 is “on” light is switching “on” but when I am switching “on” s2 the light is “off”, but actually it should be in “on” condition. It is working like 2-way electrical wiring switch.
Please help me in code to control 1 output from 2 inputs.
int in1 = 13; // GPIO13-D7
int i;
int j;
const int button1Pin = 5; // pushbutton 1 pin
const int button2Pin = 4; // pushbutton 2 pin
void setup()
{
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(in1, OUTPUT);
digitalWrite(in1, HIGH);
}
void loop()
{
switch1loopcode();
switch2loopcode();
actionloopcode();
}
void switch1loopcode()
{
int button1State;
button1State = digitalRead(button1Pin);
if (button1State == LOW) // button 1 is on
{
i = 1;
}
if (button1State == HIGH) // button 1 is off
{
i = 0;
}
}
void switch2loopcode()
{
int button2State;
button2State = digitalRead(button2Pin);
if (button2State == LOW) // button 2 is on
{
j = 1;
}
if (button2State == HIGH) // button 2 is off
{
j = 0;
}
}
void actionloopcode()
{
int button1State, button2State;
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
if (((button1State == LOW) || (button2State == LOW)) // if we're pushing button 1 OR button 2
&& ! // AND we're NOT
((button1State == LOW) && (button2State == LOW))) // pushing button 1 AND button 2
// then...
{
digitalWrite(in1, HIGH); // turn the LED on
}
else
{
digitalWrite(in1, LOW); // turn the LED off
}
}
Okay, to get it straight, you want to only switch on the output when you switch on s1, not matter s2. And only switch off the light when you switch off s2?
Because a user case you don't give is switching on s2 first...
I'm to lazy to read to code because it loops waaaaaayyyyyyy to long for what you want. Here is code that does what you want (if that's what I described). I use Bounce2 to keep the switch handling simple.
#include <Bounce2.h>
const byte OutputPin = 13; //calling it in1 is plain stupid
const byte SwitchPins[] = {5, 4}; //once you start numbering variables, use an array
Bounce switches[sizeof(SwitchPins)];
void setup()
{
//setup the switches
for(byte i = 0; i < sizeof(SwitchPins); i++){
switches[i].attach(SwitchPins[i], INPUT_PULLUP); //use INPUT if you have external pull up/down resistors
}
//setup the output
pinMode(OutputPin, OUTPUT);
}
void loop()
{
checkSwitches();
}
void checkSwitches()
{
for(byte i = 0; i < sizeof(SwitchPins); i++){
switches[i].update();
}
//I here assume pull up resistors (like with INPUT_PULLUP)
if(switches[0].fell()){
digitalWrite(OutputPin, HIGH);
}
if(switches[1].rose()){
digitalWrite(OutputPin, LOW);
}
}
Thank you for your quick reply mr.septillion. May be i failed to explain my project exactly.The code you sent is to switch on the light with switch 1 and switch off the light with switch 2. what i need is to switch light with both switches and also switch off with both switches.
That means both switches are in off condition initially. if i switch s1 then light should on. Now s2 is in off condition, if i toggle s2 to on state and then to off state light should off. Now s1 is in on condition, if i toggle s1 to off state and then to on state light should on.
Raghavendra431:
Now s2 is in off condition, if i toggle s2 to on state and then to off state light should off. [/quote] Why toggle it twice? If you skip the toggle twice part (which sounds way easier to use) but just change the output state every time a switch changes (for example s1: on => out:on, s2:on => out:off, s1:off => out:on) it's also very easy. ``` **#include <Bounce2.h>
const byte OutputPin = 13; //calling it in1 is plain stupid
const byte SwitchPins[] = {5, 4}; //once you start numbering variables, use an array
Bounce switches[sizeof(SwitchPins)];
void setup()
{
//setup the switches
for(byte i = 0; i < sizeof(SwitchPins); i++){
switches[i].attach(SwitchPins[i], INPUT_PULLUP); //use INPUT if you have external pull up/down resistors
}
//setup the output
pinMode(OutputPin, OUTPUT);
}
void loop()
{
checkSwitches();
}
void checkSwitches()
{
for(byte i = 0; i < sizeof(SwitchPins); i++){
if(switches[i].update()){
digitalToggle(OutputPin);
}
}
}
In this condition i want to make s1 as web button. And s2 is physical push button. Both are inputs to arduino.
That means both switches are in off condition initially. if i switch s1 then light should on. Now s2 is in off condition, if i toggle s2 to on state and then to off state light should off. Now s1 is in on condition, if i toggle s1 to off state and then to on state light should on.
In this condition i want to make s1 as web button. And s2 is physical push button. Both are inputs to arduino.
That means both switches are in off condition initially. if i switch s1 then light should on. Now s2 is in off condition, if i toggle s2 to on state and then to off state light should off. Now s1 is in on condition, if i toggle s1 to off state and then to on state light should on.
It looks like this what you have:
S1: normally open push button (momentary type).
S2: web control (high or low), not momentary, default is low
Each time you "just" press S1, the light should change state.
Each time S2 goes from low to high, the light should change state.
Default status on power up is S1 open (not pressed), S2 is low, light is off.
Is this correct?