analogRead function without delay:
I am using Arduino in an application where I have to measure the flow rate (flow meter YF-s-402) and temperature (thermistor) reading in a same program. I can't use delay because of the flow meter program, which is using millis function.
I do not have any issue in flow meter program but when I am writing temperature sensor ADC program without delay function, the delay is not showing, Could you please help me what I can modify following code, or any other alternative?
#define INTERVAL 1000 // [ms]
#define temp1 A0 // for temp sensor
unsigned long previousMillis = 0;
const long interval1 = 1000;
const long interval2 = 2000;
long ADC_temp;
float volt_temp, logR2, R2;
float temp, temp2, temp3, temp_c;
float R1 = 10000; // value of R1 as voltage divider on board, 10 K Ohm
float c1 = 0.001129148, c2 = 0.000234125, c3 = 0.0000000876741; //steinhart-hart coeficients for thermistor
void setup() {
Serial.begin(9600);
}
void loop()
{
unsigned long currentMillis = millis();
ADC_temp=0; //Clear the ADC_volt value
for(int i=0; i< 100; i++) // do the task 100 times
{
{
ADC_temp = ADC_temp+analogRead(temp1);
if (currentMillis - previousMillis >= interval1)
{
previousMillis = currentMillis;
}
ADC_temp = ADC_temp+analogRead(temp1);
}
{
if (currentMillis - previousMillis >= interval2)
{
previousMillis = currentMillis;
}
}
}
ADC_temp=ADC_temp/100; //Averaging the ADC_volt value
R2 = R1 * (1023.0 / (float)ADC_temp - 1.0); //calculate resistance of thermistor from voltage divider circuit
logR2 = log(R2); //calculate the log of R2, to be used in the next line
temp2 = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2)); // temperature in Kelvin
temp3 = temp2 - 273.15; //steinhart-hart equation for temperature
Serial.print("Temp: ");
Serial.print(temp3,1);
Serial.println(" C");
}
for(int i=0; i< 100; i++) { // do the task 100 times
ADC_temp = ADC_temp+analogRead(temp1);
if (currentMillis - previousMillis >= interval1)
previousMillis = currentMillis;
ADC_temp = ADC_temp+analogRead(temp1);
if (currentMillis - previousMillis >= interval2)
previousMillis = currentMillis;
}
i doubt the used of currentMillis in the code above is doing anything except to reset previousMillis every sec. In other words, the analogRead() will just be executed 200 time each iteration of loop
doubt there's a need to average the temperature measurement, it's not likely to change very quickly
Thank you @gcjr, I was searching a way to deal with analogRead without delay. Is it possible in someway?
Flow meter code does not work properly with this code, therefore, I tested it separately, calibrated it , it is working.
Also, when dealing with variable, changeable in interrupt, you should switch interrupt off before coping the value and than switch it on again.
So instead of this:
it would be better using this:
noInterrupts();
flowrate1 = (flow_frequency*0.0137); // (Pulse frequency x 60 min) / 7.5Q
flow_frequency = 0; // Reset Counter
interrupts();
LS = flowrate1/60.0;
total=total+LS;