Cant use interrupt with millis()

hi everyone, I am working on a project in which I want to read temperature and humidity using the dht11 sensor, soil moisture, and amount of water inside the tank. Now to check the water in the tank I have made a simple circuit using a transistor when the water is below our given point the motor will turn on and when it reaches its desired point it will turn off. Now the problem that I am facing is I have attacked interrupt with water level circuit on pin 3 of Arduino and I am using millis() to read temp and humidity from dht11 sensor I get accurate temp and soil moisture but interrupts never kick in even if the water goes below our desired point. In the code given below, I have replicated the circuit with simple push-button when the button is pressed it goes active low, and the motor will stop.

#include <LiquidCrystal.h> //LCD library included this is builtin library
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);


const int water_pump= 10;

const int water_level = 3;

unsigned long lastTime = 0;


void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 lcd.begin(16, 2);
  dht.begin();
  pinMode(water_level,INPUT_PULLUP); //button to replicate water level
  pinMode(water_pump,OUTPUT); // relay attached 
  attachInterrupt(digitalPinToInterrupt(water_level), waterpump, LOW);
}

void loop() {
//int water_read =digitalRead(water_level);
//Serial.println(water_read);
//if(water_read == LOW){
//digitalWrite(water_pump,HIGH);
//}
//else
//{
  digitalWrite(water_pump,LOW); // relay operating on active low when button not pressed relay will stay on
//}
  int sensorValue = analogRead(A5);
  // print out the value you read:
  Serial.print("soil moisture sensor");
  Serial.println(sensorValue);
  lcd.setCursor(0, 0);
  lcd.print("soil moist =");
  lcd.setCursor(12, 0);
  lcd.print(sensorValue);
  // Wait a few seconds between measurements.
  
 if (millis() > lastTime)
  {
    
    lastTime = millis() + 2000;
  
  
  //sensor reading after every 2seconds
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");


  lcd.setCursor(0, 1);
  lcd.print("humid=");
  lcd.setCursor(6, 1);
  lcd.print(h);

 
  lcd.setCursor(8, 1);
  lcd.print("temp=");
  lcd.setCursor(14, 1);
  lcd.print(t);


}
}


void waterpump()
{
  digitalWrite(water_pump,HIGH); // when button press relay will turn off
  
}

But here is the thing when I use it without interrupt everything works just fine.But I still need to know what is the problem of using millis() with interrupt,or I doing something wrong.

Every time through loop() the first thing you do is turn the pump off, so even if the interrupt turned it on it would immediately be turned off.

Your ISR should use FALLING rather than LOW and you should have some logic (for example a higher water level) at which the pump is turned off.

To be honest I can see no need for an interrupt for this purpose. It won't matter if the pump is a few millisecs (or even a few seconds) late getting turned on. Just check the water level in every iteration of loop and if it is too low, turn the pump on.

...R

Study the syntax/documentation for "attachinterrupt()" and see if you find the problem.
Paul

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.