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);
}
}