Hi Guys,
Thank you very much for your help, now I have identified the push button, and tried to write up a code to control the solenoid valve for my project. Could you help me look at the code and tell me whether it will do what I want, or I need to modify it.
Here is what I want physically:
In my circuit, I have Arduino, a solenoid valve (12V DC), 12V power, 2 LEDs (red and green), a push button. When I push the button, the green LED will light (red LED is off) and the solenoid valve is energized (or open) for a time period (e.g. in my code is 3 s). And after 3 s duration, the valve is closed and green LED will be off while red LED will be on. Then I want the system to stop until I tell it to start again.
Questions:
I am not sure when I press or push the button once (say for the first time) and don't hold it, will the button keep sending signal to Arduino telling Arduino that the button's state is HIGH all the time after I press it once ? Or this depends on the button itself. Please see the link of the button (Tactile Switch x10 6x6mm Momentary Arduino Hobby Project PCB Push Button SPST UK) I bought on ebay:
If yes then I think my code can work. If not then maybe I should buy another button ? Because I don't want to hold it all the time. The code is here:
// Solenoid Project
// To initiave the program by 1 button, solenoid is energized for a certain period then off
// Assign the Pins to electrical parts
const int Button1Pin1=2;
const int RedLed1Pin1=10;
const int GreenLed2Pin2=11;
const int Solenoid1Pin1=12;
// Define the variables
int Button1State = 0;
// Define the Pin Modes
void setup(){
pinMode(RedLed1Pin1, OUTPUT);
pinMode(GreenLed2Pin2, OUTPUT);
pinMode(Solenoid1Pin1, OUTPUT);
pinMode(Button1Pin1, INPUT);
}
// Define the Loop
void loop(){
Button1State = digitalRead(Button1Pin1);
if (Button1State == HIGH) {
digitalWrite(RedLed1Pin1, LOW);
digitalWrite(GreenLed2Pin2, HIGH);
digitalWrite(Solenoid1Pin1, HIGH);
delay(3000);
digitalWrite(RedLed1Pin1, HIGH);
digitalWrite(GreenLed2Pin2, LOW);
digitalWrite(Solenoid1Pin1, LOW);
delay(5000); }
else {
digitalWrite(RedLed1Pin1, HIGH);
digitalWrite(GreenLed2Pin2, LOW);
digitalWrite(Solenoid1Pin1, LOW);
}
}
Thank you very much.
Jeff