I have a project that seems like it is simple, but I do not have the time to learn to code it.
I need help. Three switches activate three relays independently.
I can send a schematic. Help?$
Chris
Hi Chris,
Do you need the circuit to be built, or just the code to control it?
Pat.
if (digitalRead(button1) == LOW){ // button with internal pullup, connect to Gnd when pressed
digitalWrite (coil1, HIGH); // transistor drive to sink current thru coil
}
else{
digitalWrite (coil1, LOW);
}
if (digitalRead(button2) == LOW){ // button with internal pullup, connect to Gnd when pressed
digitalWrite (coil2, HIGH); // transistor drive to sink current thru coil
}
else{
digitalWrite (coil2, LOW);
}
if (digitalRead(button3) == LOW){ // button with internal pullup, connect to Gnd when pressed
digitalWrite (coil3, HIGH); // transistor drive to sink current thru coil
}
else{
digitalWrite (coil3, LOW);
}
Not too hard.
Why do you need an arduino for this? Can’t you just wire the switches to the relays?
Pat - i just need code.
Cross - Thank you.
Paul - I did not disclose every detail, which is this: the light needs to stay on 2 seconds after a button push, and stay on if the button remains pushed. Then off 2 seconds after released. I copied a script that had a 2 second delay, but it waited 2 seconds to poll the next switch, and I need these all to work independently.
I figure it is simple - but I am totally green. I need to git er done - so willing to pay if needed.
I sure appreciate your help.
// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2; // the number of the pushbutton1 pin
const int button2Pin = 3; // the number of the pushbutton2 pin
const int button3Pin = 4; // the number of the pushbutton3 pin
const int relay1Pin = 7; // the number of the Relay1 pin
const int relay2Pin = 8; // the number of the Relay2 pin
const int relay3Pin = 9; // the number of the Relay3 pin
void setup()
{
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
// initialize the relay pin as an output:
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
// variables will change:
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0; // variable for reading the pushbutton status
int button3State = 0; // variable for reading the pushbutton status
// read the state of the pushbutton values:
// button1State = digitalRead(button1Pin);
// button2State = digitalRead(button2Pin);
// button3State = digitalRead(button3Pin);
// For the second button, we just activate the solenoid/relay for two seconds
if (button1State == HIGH)
{
// turn relay on
digitalWrite(relay1Pin, HIGH);
delay(2000); // waits for 2 seconds
//turn relay off
digitalWrite(relay1Pin, LOW);
} //end button 1 = high
ELSE
{
if (button2State == HIGH);
// turn relay on
digitalWrite(relay2Pin, HIGH);
delay(2000); // waits for 2 seconds
//turn relay off
digitalWrite(relay2Pin, LOW);
} // end button 2 = high
ELSE
{
If (button3State == HIGH);
// turn relay on
digitalWrite(relay3Pin, HIGH);
delay(2000); // waits for 2 seconds
//turn relay off
digitalWrite(relay3Pin, LOW);
} // end button 3 = high
} //end button 2 if statement
} //end button 1 if statement
} //endloop
I believe I have code that does this already written and tested. If you can wait a few hours for me to
verify that, it's yours.
Sounds great!!
Thank you.
chris
OK, I found the code I mentioned. It controls four outputs from four inputs, however, the feature of extending the delay would need to be added. I know you said you're green, but if you are willing to learn, it would be fairly simple. I would do it, but I am literally swamped with projects at the moment and I won't have available time for probably a week or two.
Let me know and I can do it when I have time.