Newbie need help!

Hi everybody,
I merged some sketches trying to operate 4 relays, one after one, with one remote analogue signal from a soil humidity sensor, these relays operate solenoids valves. As you can see in the below sketch I use a timer to measure the operation time of the first relay and the others relays will work according to the cycle duration of the first relay, and this way I will ensure to supply the same amount of water in each sections. Additionally, I included a flow meter signal to ensure that the pump will not work without water as a protection, "and here is my problem!" the turn on of the pump and solenoids start before the water flow sensor does and although my sketch could be compiled by the IDE it will not run according my purpose.
I appreciate very much any help....

/*********************************************************
Connect the red wire to +5V,
the black wire to common ground
and the yellow sensor wire to pin #2
**********************************************************/

#define FLOWSENSORPIN 2

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);

if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}

if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}

//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
unsigned long start, finished, elapsed;
int Relay1 = LOW;
int Relay2 = LOW;
int Relay3 = LOW;
int Relay4 = LOW;
int previous = LOW;
long time = 0;
long debounce = 500;
const int minVal=0; //value between 0 and 255
const int maxVal=0; //value between 0 and 255

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 setup()
{
Serial.begin(9600);

//flow sensor
Serial.print("Flow sensor test!");
pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
useInterrupt(true);

//cycle duration
Serial.println("cycle duration");
pinMode(sensorPin, INPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
}

void loop()
{
flowSensor();
cycleDuration();
}

void flowSensor ()
{
// run over and over again
Serial.print("Freq: "); Serial.println(flowrate);
Serial.print("Pulses: "); Serial.println(pulses, DEC);

// if a plastic sensor use the following calculation
// Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
// Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
// Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
// Liters = Pulses / (7.5 * 60)
float liters = pulses;
liters /= 7.5;
liters /= 60.0;

Serial.print(liters); Serial.println(" Liters");

delay(100);
}

void cycleDuration()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the Relay1 on
if (minVal < analogRead <= maxVal && flowrate != 0)
{
digitalWrite(Relay1, HIGH);
delay (200);
}
//turn on millis
if (Relay1==HIGH)
{
start=millis();
delay(200); // for debounce
Serial.println("Started...");
}
if (Relay1==LOW)
{
finished=millis();
delay(200); // for debounce
time = millis();
displayResult();
}
//turn on relay 2 during millis period
if(Relay1 == LOW && Relay3 == LOW && Relay4 == LOW && previous == LOW && millis()- time > debounce && flowrate != 0)
{
digitalWrite(Relay2, HIGH);
start=millis();
delay (200);
}
//turn on relay 3 during millis period
if(Relay1 == LOW && Relay2 == LOW && Relay4 == LOW && previous == LOW && millis()- time > debounce && flowrate != 0)
{
digitalWrite(Relay3, HIGH);
start=millis();
delay (200);
}
//turn on relay 2 during millis period
if(Relay1 == LOW && Relay2 == LOW && Relay3 == LOW && previous == LOW && millis()- time > debounce && flowrate != 0)
{
digitalWrite(Relay4, HIGH);
start=millis();
delay (200);
}
}

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

You can edit your original post.

Without analysing you code, I would be looking at turning on the pump and one outlet then loop around to check the flow meter for the expected wait time. If no flow in the time enter an alarm condition.

The reason for opening one outlet is for safety as you do not want the pump working into a blocked pipe as you risk over pressurising the system and damage the pump or plumbing.

Once the flow meter has started, just test it is still working in your timing outputs loop.

Weedpharma

Hi, it is the new code:

/*********************************************************
Connect the red wire to +5V, 
the black wire to common ground 
and the yellow sensor wire to pin #2
**********************************************************/

// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 2

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
  uint8_t x = digitalRead(FLOWSENSORPIN);
  
  if (x == lastflowpinstate) {
    lastflowratetimer++;
    return; // nothing changed!
  }
  
  if (x == HIGH) {
    //low to high transition!
    pulses++;
  }
  lastflowpinstate = x;
  flowrate = 1000.0;
  flowrate /= lastflowratetimer;  // in hertz
  lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
  }
}

//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
unsigned long start, finished, elapsed;
int Relay1 = LOW;
int Relay2 = LOW;
int Relay3 = LOW;
int Relay4 = LOW;
int previous = LOW;
long time = 0;
long debounce = 500;
const int minVal=0; //value between 0 and 255
const int maxVal=0; //value between 0 and 255

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 setup()
  {
   Serial.begin(9600);
   
  //flow sensor
  Serial.print("Flow sensor test!");
  pinMode(FLOWSENSORPIN, INPUT);
  digitalWrite(FLOWSENSORPIN, HIGH);
  lastflowpinstate = digitalRead(FLOWSENSORPIN);
  useInterrupt(true);
    
  //cycle duration
  Serial.println("cycle duration");
  pinMode(sensorPin, INPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  }



void loop()
{
   // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the Relay1 on
if (minVal < analogRead <= maxVal)
{ 
 digitalWrite(Relay1, HIGH);
 }
 delay (200);

//turn on millis
  if (Relay1==HIGH)
  {
    start=millis();
    delay(200); // for debounce
    Serial.println("Started...");
  }
  if (Relay1==LOW)
  {
    finished=millis();
    delay(200); // for debounce
    time = millis();
    displayResult();
     // waterflow alarm
  }
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay1, LOW);
 }
  
  //turn on relay 2 during millis period
  if (Relay1 == LOW && Relay3 == LOW && Relay4 == LOW && previous == LOW && millis()- time > debounce)
  {
digitalWrite(Relay2, HIGH);
start=millis();
// waterflow alarm
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay2, LOW);
 }
   delay (200);
  }
  
  //turn on relay 3 during millis period
  if (Relay1 == LOW && Relay2 == LOW && Relay4 == LOW && previous == LOW && millis()- time > debounce)
  {
digitalWrite(Relay3, HIGH);
start=millis();
// waterflow alarm
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay3, LOW);
 }
   delay (200);
  }
  
  //turn on relay 2 during millis period
  if (Relay1 == LOW && Relay2 == LOW && Relay3 == LOW && previous == LOW && millis()- time > debounce)
  {
digitalWrite(Relay4, HIGH);
start=millis();
// waterflow alarm
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay4, LOW);
 }
   delay (200);
  }
// run over and over again
  Serial.print("Freq: "); Serial.println(flowrate);
  Serial.print("Pulses: "); Serial.println(pulses, DEC);
  
  // if a plastic sensor use the following calculation
  // Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
  // Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
  // Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
  // Liters = Pulses / (7.5 * 60)
  float liters = pulses;
  liters /= 7.5;
  liters /= 60.0;

/*
  // if a brass sensor use the following calculation
  float liters = pulses;
  liters /= 8.1;
  liters -= 6;
  liters /= 60.0;
*/
  Serial.print(liters); Serial.println(" Liters");
 
  delay(100);
}

Please edit your OP to put it in code tags as described in my first reply. It makes reading easier for all.

You have included your new code but forgot to tell us what it does. Does it work?

Weedpharma

You seem to have lots of delay()s in your code. Have a look at Several Things at a Time which illustrates how to manage timing without using delay(). Nothing else can happen during a delay() interval so it is a function that is usually only suitable in a short demo program.

You should write code so that loop() can repeat hundreds or thousands of times per second.

...R

Excuse me Weedpharma!

Here the sketch, I don't know if this sketch works, I waiting for the parcel with the devices.

Thanks you all!

/*********************************************************
Connect the red wire to +5V, 
the black wire to common ground 
and the yellow sensor wire to pin #2
**********************************************************/

// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 2

// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
  uint8_t x = digitalRead(FLOWSENSORPIN);
  
  if (x == lastflowpinstate) {
    lastflowratetimer++;
    return; // nothing changed!
  }
  
  if (x == HIGH) {
    //low to high transition!
    pulses++;
  }
  lastflowpinstate = x;
  flowrate = 1000.0;
  flowrate /= lastflowratetimer;  // in hertz
  lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
  }
}

//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
unsigned long start, finished, elapsed;
int Relay1 = LOW;
int Relay2 = LOW;
int Relay3 = LOW;
int Relay4 = LOW;
int previous = LOW;
long time = 0;
long debounce = 500;
const int minVal=0; //value between 0 and 255
const int maxVal=0; //value between 0 and 255

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 setup()
  {
   Serial.begin(9600);
   
  //flow sensor
  Serial.print("Flow sensor test!");
  pinMode(FLOWSENSORPIN, INPUT);
  digitalWrite(FLOWSENSORPIN, HIGH);
  lastflowpinstate = digitalRead(FLOWSENSORPIN);
  useInterrupt(true);
    
  //cycle duration
  Serial.println("cycle duration");
  pinMode(sensorPin, INPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  }



void loop()
{
   // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the Relay1 on
if (minVal < analogRead <= maxVal)
{ 
 digitalWrite(Relay1, HIGH);
 }
//turn on millis
  if (Relay1==HIGH)
  {
    start=millis();
    delay(200); // for debounce
    Serial.println("Started...");
  }
  if (Relay1==LOW)
  {
    finished=millis();
    delay(200); // for debounce
    time = millis();
    displayResult();
    delay (100);    
  }
  
  // waterflow alarm
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay1, LOW);
 }
  
  //turn on relay 2 during millis period
  if (Relay1 == LOW && Relay3 == LOW && Relay4 == LOW && previous == LOW && millis()- time > debounce)
  {
digitalWrite(Relay2, HIGH);
start=millis();
// waterflow alarm
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay2, LOW);
 }
   delay (200);
  }
  
  //turn on relay 3 during millis period
  if (Relay1 == LOW && Relay2 == LOW && Relay4 == LOW && previous == LOW && millis()- time > debounce)
  {
digitalWrite(Relay3, HIGH);
start=millis();
// waterflow alarm
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay3, LOW);
 }
   delay (200);
  }
  
  //turn on relay 2 during millis period
  if (Relay1 == LOW && Relay2 == LOW && Relay3 == LOW && previous == LOW && millis()- time > debounce)
  {
digitalWrite(Relay4, HIGH);
start=millis();
// waterflow alarm
 if (FLOWSENSORPIN !=HIGH)
 {
  digitalWrite (Relay4, LOW);
 }
   delay (200);
  }
  
// run over and over again
  Serial.print("Freq: "); Serial.println(flowrate);
  Serial.print("Pulses: "); Serial.println(pulses, DEC);
  
  // if a plastic sensor use the following calculation
  // Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
  // Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
  // Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
  // Liters = Pulses / (7.5 * 60)
  float liters = pulses;
  liters /= 7.5;
  liters /= 60.0;

/*
  // if a brass sensor use the following calculation
  float liters = pulses;
  liters /= 8.1;
  liters -= 6;
  liters /= 60.0;
*/
  Serial.print(liters); Serial.println(" Liters");
 
  delay(200);
}

nicorodri:
Here the sketch, I don't know if this sketch works,

When you do, and if it does not, let us know exactly what happens and what should happen.

The Arduino system is great for learning-by-doing.

...R

Nicorodri, please edit your first post to put code tags around the code as I have requested in #1 & #3.

This is part of the forum rules and helps readability.

Weedpharma