bigone5500
- Apr 9, 2014
- 712
- Joined
- Apr 9, 2014
- Messages
- 712
I found this on youtube and tried it out. It works but I am confused as to why I can't activate both pushbuttons at the same time. How can this sketch be modified to do this?
Code:
// constnts that won't change. They're used here to set pin numbers
const int button1 = 2; // the number of the pushbutton pin
const int button2 = 3; // the number of the pushbutton pin
const int relay1 = 11; // the number of the relay1 pin
const int relay2 = 12; // the number of the relay2 pin
// variables that will change
int buttonState1 = 0; // variable for reading the pushbutton1 status
int buttonState2 = 0; // variable for reading the pushbutton2 status
void setup()
{
// initialize the relay pin as an output
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// initialize the pushbutton pin as an input
pinMode(button1, INPUT_PULLUP); // PULLUP for add 5v to pin without hardware prevent pin from floating between HIGH and LOW
pinMode(button2, INPUT_PULLUP); // PULLUP for add 5v to pin without hardware prevent pin from floating between HIGH and LOW
}
void loop()
{
// read the state of the pushbutton value
buttonState1 = digitalRead(button1);
// check if the pushbutton is pressed. If it is, the buttonState is LOW.
if (buttonState1 == LOW)
{
digitalWrite(relay1, LOW); // pin 2 is pressed and connected to GND so it will be LOW
delay (1000); // remove 5v from pin 11 so relay in1 will be 0v and this will make relay on
} // wait 1 sec.
else
{
digitalWrite(relay1, HIGH); // add 5v to arduino pin 11 so relay in1 will be 5v and this will make the relay turn off.
}
// read the state of the pushbutton value
buttonState2 = digitalRead(button2);
// check if the pushbutton is pressed
// if it is, the buttonState is LOW
if (buttonState2 == LOW) // pin 3 is pressed and connected to GND so it will be LOW
{ // remove 5v from pin 12 so relay in2 will be 0v and this will make the relay turn on
digitalWrite(relay2, LOW); // and wait 5 seconds.
delay (5000);
}
else
{
digitalWrite(relay2, HIGH); // add 5v to arduino pin 12 so relay in2 will be 5v and this will make the relay turn off.
}
}