Hi Guys,
Can you kindly help me with my project please. I already have successful code and setup to control the synchronization of two solenoid valves: both open and close together. Now I want to modify the code so that the first valve opens first, after an offset time of 0.2 second, the second valve opens. After a period time, the two valves close simultaneously together.
I tried to use the delay function, but it did not work. The delay code actually delayed the whole action together: the two valves still open and close together after the delay period. Can you help me with this please ? Here is my original code:
// Assign Pins
const int Solenoid1outPin = 7; // the output pin to control solenoid1
const int Solenoid2outPin = 6; // the output pin to control solenoid2
const int ButtoninPin = 2; // the input pin to read voltage (button)
const int Led1outPin = 12; // the output pin to supply voltage (green LED)
const int Led2outPin = 11; // the output pin to supply voltage (red LED)
// Define variables
int Led1State = LOW; // the current state of green LED1
int Led2State = HIGH; // the current state of red LED2
int ButtonState = LOW;
int Solenoid1State = LOW; // the current state of solenoid1
int Solenoid2State = LOW; // the current state of solenoid2
int Reading; // the current reading from the input pin (button)
int LastReading = LOW; // the previous reading from the input pin (button)
unsigned long CurrentTime = 0; // set the clock
unsigned long ButtonTime = 0;
unsigned long Led1Time = 0; // the last time LED1 was toggled
unsigned long Led2Time =0; // the last time LED2 was toggled
unsigned long Solenoid1Time = 0; // the last time the output pin (solenoid1) was toggled
unsigned long Solenoid2Time = 0; // the last time the output pin (solenoid2) was toggled
unsigned long DebounceTime = 200; // the button debounce time, increase if the output flickers
unsigned long DischargeTime = 1000; // discharge time period
// Setup Pin mode
void setup()
{
pinMode(Led1outPin, OUTPUT);
pinMode(Led2outPin, OUTPUT);
pinMode(Solenoid1outPin, OUTPUT);
pinMode(Solenoid2outPin, OUTPUT);
pinMode(ButtoninPin, INPUT_PULLUP);
}
// Setup Loop
void loop(){
CurrentTime = millis();
ButtonRead();
Solenoid();
LED();
Switch();
}
void ButtonRead(){
Reading = digitalRead(ButtoninPin);
if ( Reading == LOW && CurrentTime - ButtonTime > DebounceTime)
{
ButtonTime = millis();
ButtonState = HIGH;
}
if ( Reading == HIGH && CurrentTime - ButtonTime > DischargeTime)
{
ButtonTime = millis();
ButtonState = LOW;
}
}
void Solenoid(){
Reading = digitalRead(ButtoninPin);
if ( Reading == LOW && CurrentTime - Solenoid1Time > DebounceTime)
{
Solenoid1State = HIGH;
Solenoid1Time = millis();
Solenoid2State = HIGH;
Solenoid2Time = millis();
}
if ( CurrentTime - Solenoid1Time > DischargeTime )
{
Solenoid1State = LOW;
Solenoid1Time = millis();
Solenoid2State = LOW;
Solenoid2Time = millis();
}
}
void LED(){
if (ButtonState == HIGH)
{
Led1State = HIGH;
Led2State = LOW;
}
if ( ButtonState == LOW )
{
Led1State = LOW;
Led2State = HIGH;
}
}
void Switch(){
digitalWrite(Led1outPin, Led1State);
digitalWrite(Led2outPin, Led2State);
digitalWrite(Solenoid1outPin, Solenoid1State);
digitalWrite(Solenoid2outPin, Solenoid2State);
}
Thank you very much for help !
Best, Jeff