advice for merging sketches....

Hi everybody!

I need your advice to run an irrigation system. I have a remote sensor (soil moisture) that send a analog signal This signal controls a solenoid valve according to a range (upper and lower limits). By the moment, everything goes fine!

Now, I expanded the irrigation covering, up to four areas (four solenoids valves) and keeping the same sensor.

For this purpose, due to the pump is no so bigger to irrigate all the areas at the same time, I am trying to count the time duration of the period for the first area, log the amount of millis (interval duration) and repeating this interval in every valve.

Here is the question! how does merge? “if statement” sketch + cycle duration counter sketch + sequence of control ON and OFF sketch.

Additionally to my idea, I would like to hear about other suggestions.

I would appreciate very much your attention on this subject.

Regards

//sketch for relay operation
int sensorPin = A0;    // select the input pin for the remote analog signal
int sensorValue = 0;  // variable to store the value coming from the remote sensor
const int Relay1 = 4;


int RelayPins[] = {
 5,6,7
};       // an array of pin numbers to which Relays are attached
int pinCount = 3 ;           // the number of pins (i.e. the length of the array)

unsigned long start, finished, elapsed;

int previous = LOW;
long time = 0;
long debounce = 50;
const int minVal=220; //value between 0 and 1023
const int maxVal=280; //value between 0 and 1023


long interval = 100;
long previousMillis = 0;
       

unsigned long timer = 200;  //¡¡¡this variable supposedly must be given by the elapsed time to ensure the same time in each relay!!!!



void setup()
{
  Serial.begin(9600);
  Serial.println("cycle duration");
  pinMode(sensorPin, INPUT);
  pinMode(Relay1, OUTPUT);
  
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(RelayPins[thisPin], OUTPUT);
    
  }
 

}

void displayResult()
{
  float h,m,s,ms;
  unsigned long over;
  elapsed=finished-start;
  h=int(elapsed/3600000);
  over=elapsed%3600000;
  m=int(over/60000);
  over=over%60000;
  s=int(over/1000);
  ms=over%1000;
  Serial.print("Raw elapsed time: ");
  Serial.println(elapsed);
  Serial.print("Elapsed time: ");
  Serial.print(h,0);
  Serial.print("h ");
  Serial.print(m,0);
  Serial.print("m ");
  Serial.print(s,0);
  Serial.print("s ");
  Serial.print(ms,0);
  Serial.println("ms");
  Serial.println();
}

void loop()
{
 
   // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  Serial.print("sensor = ");
  Serial.println(sensorValue);
  delay (100);


  // turn the Relay1 on
if ((minVal <= sensorValue) && (sensorValue <= maxVal))
{ 
 digitalWrite(Relay1 , HIGH);
}
else {
  digitalWrite(Relay1, LOW);
      
  }
  if (digitalRead(Relay1)==LOW) {
      start = millis();
  delay(10); // for debounce
  Serial.println("Started...");
}
if (digitalRead(Relay1) == HIGH){
   finished = millis();
    delay(10); // for debounce
    time = millis();
    elapsed=finished-start;
  
    displayResult();
    delay(100);
}




 
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
      digitalWrite(RelayPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(RelayPins[thisPin], LOW); 
}
  // loop from the highest pin to the lowest:
  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
    // turn the pin on:
    digitalWrite(RelayPins[thisPin], HIGH);
    delay(timer);
    // turn the pin off:
    digitalWrite(RelayPins[thisPin], LOW);
          
    }

    delay(50);
  }

cycle_duration_v2.2.ino (2.74 KB)

nicorodri: For this purpose, due to the pump is no so bigger to irrigate all the areas at the same time, I am trying to count the time duration of the period for the first area, log the amount of millis (interval duration) and repeating this interval in every valve.

I suspect you have most of the project described in this paragraph. Try writing it out more clearly with each step of the process on a separate line and I think you will find you have a design for your program.

I simple terms you need to measure, save the value, use the saved value a few more times. Then (perhaps) repeat tht process. Think about how you would run the pump for 10,176 millisecs (for example).

Have a look at Planning and Implementing a Program

...R