Dear Arduino lovers ,
I have a limited coding experience, till today with time and reflexion I ever find how to move out with my project but this time I'm lost....
I can't operate the relay,after initialising it simply stay in ON state, could you help me to sort out ht is wrong with my bellow code ?
I wnated to activite the solar pump when S1 is above temperature of S2 else leave it OFF.
thank by advance
// include libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 13 on the Arduino
#define ONE_WIRE_BUS 13 // Arduino pin connected to DS18B20 sensor's DQ pin
#define precision 12 //onewire precision dallas sensor
//const int digPin = 12;
int sen_number = 0; //counter of Dallas sensors
int S1TempC, S2TempC, S3TempC, S4TempC;
int Ch_Pump = 6; //Relay pump pin
// flow measurement definition
int flowPin = 2; //This is the input pin on the Arduino
double flowRate; //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with devices
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
DeviceAddress T1, T2, T3, T4;// arrays to hold device adresses
//float tempCelsius; // temperature in Celsius
//float tempFahrenheit; // temperature in Fahrenheit
void setup()
{
Serial.begin(9600); // initialize serial
// put your setup code here, to run once:
pinMode(flowPin, INPUT); //Sets the pin as an input
attachInterrupt(0, Flow, RISING); //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
pinMode(Ch_Pump, OUTPUT);
digitalWrite(Ch_Pump, LOW);
sensors.begin(); // initialize the sensor
Serial.print("*Temp Sensor:* ");
Serial.print("Found: ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" Devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Search for devices on the bus and assign based on an index.
if (!sensors.getAddress(T1, 0)) Serial.println("Not Found Sensor 1");
if (!sensors.getAddress(T2, 1)) Serial.println("Not Found Sensor 2");
if (!sensors.getAddress(T3, 2)) Serial.println("Not Found Sensor 3");
if (!sensors.getAddress(T4, 3)) Serial.println("Not Found Sensor 4");
//show the addresses we found on the bus
for (int k =0; k < sensors.getDeviceCount(); k++) {
Serial.print("Sensor "); Serial.print(k+1);
Serial.print(" Address: ");
if (k == 0) { printAddress(T1); Serial.println();}
else if (k == 1) { printAddress(T2); Serial.println();}
else if (k == 2) { printAddress(T3); Serial.println();}
else if (k == 3) { printAddress(T4); Serial.println();}
}
// set the resolution to 12 bit per device
sensors.setResolution(T1, precision);
sensors.setResolution(T2, precision);
sensors.setResolution(T3, precision);
sensors.setResolution(T4, precision);
for (int k =0; k < sensors.getDeviceCount(); k++) {
Serial.print("Sensor "); Serial.print(k+1);
Serial.print(" Resolution(best=12 bit): ");
if (k == 0) { Serial.print(sensors.getResolution(T1), DEC); Serial.println();
} else if (k == 1) { Serial.print(sensors.getResolution(T2), DEC); Serial.println();
} else if (k == 2) { Serial.print(sensors.getResolution(T3), DEC); Serial.println();
} else if (k == 3) { Serial.print(sensors.getResolution(T4), DEC); Serial.println();
}
}
Serial.println("Flow sensor in use YF-S201 (1-30l/min)");
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
//zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print(",Temp.[°C]|[°F]:, ");
Serial.print(tempC);
//Serial.print(" °C ");
Serial.print(" , "); // separator between Celsius and Fahrenheit
Serial.print(DallasTemperature::toFahrenheit(tempC));
Serial.print(" , ");
//Serial.print(" °F ");
}
// function to print a device's resolution
//void printResolution(DeviceAddress deviceAddress)
//{}
void printData(DeviceAddress deviceAddress)
{
Serial.print(" Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
//Serial.println();
}
void loop()
{
float S1TempC = sensors.getTempC(T1);
float S2TempC = sensors.getTempC(T2);
float S3TempC = sensors.getTempC(T3);
float S4TempC = sensors.getTempC(T4);
digitalWrite(Ch_Pump, LOW);
count = 0; // Reset the counter so we start counting from 0 again
interrupts(); //Enables interrupts on the Arduino
// call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus
//Serial.print("Reading DATA...");
sensors.requestTemperatures();
//Serial.println("DONE");
// print the device information
for (int k =0; k < sensors.getDeviceCount(); k++) {
Serial.print("Sensor "); Serial.print(k+1); Serial.print(",");
if (k == 0) { printData(T1);}
else if (k == 1) { printData(T2);}
else if (k == 2) { printData(T3);}
else if (k == 3) { printData(T4);}
}
if (sen_number == sensors.getDeviceCount()) {
sen_number = 0; // reset counter
}
// ***Relay Pump Managment***
//Serial.print("S1TempC: ");Serial.print(S1TempC);Serial.print(" ");
//Serial.print("S2TempC: ");Serial.print(S2TempC);Serial.print(" ");
//Serial.print("S3TempC: ");Serial.print(S3TempC);Serial.print(" "); // verif. fonc. variables SxTempC
//Serial.print("S4TempC: ");Serial.print(S4TempC);Serial.print(" ");
if ((S1TempC < S2TempC))
{
digitalWrite(Ch_Pump, LOW);
Serial.print("Temp.: S1< S2 = PUMP OFF --> ");
}
else if(S1TempC > S2TempC)
{
digitalWrite(Ch_Pump, HIGH);
Serial.print("Temp : S1 > S2 = PUMP ON --> ");
}
}
//Serial.print("Sensor Number="); Serial.println(sen_number);
delay(30000); // give the time between each temp measure and used for rotation calculation --> 10.000 = 10sec
noInterrupts(); //Disable the interrupts on the Arduino
//Start the math of Flow measurement
flowRate = (count * 2.236); //rotation determination : Take counted pulses(rotation) in the last "delay" period and multiply by 2.236mL (mean of info found over internet 2,22 2,5ml/rotation)
flowRate = flowRate * 2; //time conversion : Convert "delay period" to minutes, giving you mL / Minute
flowRate = flowRate / 1000; //Convert mL to Liters, giving you Liters / Minute
//Flow measurement indication
Serial.print("Flow YF-S201 [l/min]:, ");
Serial.println(flowRate); //Print the variable flowRate to Serial in l/min
//Serial.print(" l/min");
//Serial.println(" - normal pump flow 13,3l/min");
//sen_number++ ;
}
void Flow()
{
count++; //Every time this function is called, increment "count" by 1
}