Hi Genius,
Can you please help me with the code, I can't get it work. I am controlling 2 independent valves (v1, v2). At time t1, v1 opens, after a period t2, the second valve v2 opens. After a time period t3, v2 closes, and after time t4, v1 closes. I am sure it's simple for you, but I have been struggling for a while now....
// Assign Pins
const int Solenoid1outPin = 7; // the output pin to control solenoid1(gate valve)
const int Solenoid2outPin = 6; // the output pin to control solenoid2(pump valve)
const int ButtoninPin = 2; // the input pin to read voltage (button)
// Define variables
int ButtonState = LOW;
int Solenoid1State = LOW; // the current state of solenoid1(gate valve)
int Solenoid2State = LOW; // the current state of solenoid2(pump valve)
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 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 = 100; // the button debounce time, increase if the output flickers
// Setup desired valve's openning time period before each run
unsigned long DischargeTime = 960; // L/D discharge time period
unsigned long OffsetTimeOpen = 120; // 111 the offset time between valve 1 and valve 2
unsigned long OffsetTimeClose = 4000; // 111 the offset time between valve 1 and valve 2
unsigned long OperationTimeV1 = 20000; // the total operation time adding the offset time for valve 1; optional to use
// Setup Pin mode
void setup()
{
pinMode(Solenoid1outPin, OUTPUT);
pinMode(Solenoid2outPin, OUTPUT);
pinMode(ButtoninPin, INPUT_PULLUP);
Serial.begin(9600);
}
// Setup Loop
void loop(){
CurrentTime = millis();
ButtonRead();
Solenoid1();
Solenoid2();
Switch();
}
void ButtonRead(){
Reading = digitalRead(ButtoninPin);
if ( Reading == LOW && CurrentTime - ButtonTime > DebounceTime )
{
ButtonTime = CurrentTime;
ButtonState = HIGH;
Serial.print("5");
}
if ( Reading == HIGH && CurrentTime - ButtonTime >= DischargeTime + OffsetTimeOpen )
{
ButtonTime = CurrentTime;
ButtonState = LOW;
Serial.print("6");
}
}
void Solenoid1(){
Reading = digitalRead(ButtoninPin);
if ( Reading == LOW && CurrentTime - Solenoid1Time >= DebounceTime )
{
Solenoid1State = HIGH;
Solenoid1Time = CurrentTime;
Solenoid2Time = CurrentTime;
Serial.print("1");
}
if ( Reading == HIGH && CurrentTime - Solenoid1Time >= (DischargeTime + OffsetTimeClose))
{
Solenoid1State = LOW;
Solenoid1Time = CurrentTime;
Serial.print("2");
}
}
void Solenoid2(){
if ( ButtonState == HIGH && CurrentTime - Solenoid1Time >= OffsetTimeOpen )
{
Solenoid2State= HIGH;
Serial.print("3");
}
if ( ButtonState = LOW && CurrentTime - Solenoid2Time >= DischargeTime + OffsetTimeClose )
{
Solenoid2State = LOW;
Solenoid2Time = CurrentTime;
Serial.print("4");
}
}
void Switch(){
digitalWrite(Solenoid1outPin, Solenoid1State);
digitalWrite(Solenoid2outPin, Solenoid2State);
}
Thank you very much !
Jeff