How to program an interrupt code with automatic output pin

Hi there all.

I'm new here but in need some help
I'm making a new project, for water level detection in a pond, for high water but also low.
so that the pump or overflow valve will turn on or off.

But I'm a bit stuck on how to program an interrupt in it.
what i want is to switch the pump of on pin bases from the Arduino with a push button or latching switch.
a so call service mode. it's like a multi switch with 1 output pin.

greeting K.t

Welcome to the forum

What makes you think that you need to use an interrupt rather than just polling the input in the loop() function ?

Hi and welcome to the community.

Huh?

Could you restate your want?

I do not think you will need an interrupt.

What kind of high low water detection devices will be used?

Well i making a water level detection with a ultrasonic detection so that it is a bit stand alone.

And that loop is completely working with the switching the pump on or of and the overflow valve and the measuring of waterlevel with indication with LCD display.
But i need a service mode that i can overrule the automatic bit so i can clean the filter out.

Bit i was looking around for what codes i can use but I’m lost in a large forest with so many more questions. :rofl:.

Being curious I ask You post that code, autoformatted in the IDE using the code tag symbol, </> at the top of this window.
There's no need for any interrupts for a project like this.

Interrupts calls for deep knowledge but is believed to be the bright solution by ignorant, new programmers, but often gives problems You can't dream of.

That's a waste of time. The code You need has not been written. You will have to do it.

Welcome to the forum

1 Like

MY irrigation system uses a SPDT switch to do that. Center is off. One side is on continuously and the other on is Arduino control.

add a button, select a GPIO pin, wire the button to that pin, in setup() code the button with a pullup and set the pin to input. The button can be latch or toggle. The code in loop() will be different depending on latch or toggle.

In loop() read/store the button state, surround the code to run normal with an if statement, code the if statement to read the button state and either run the normal routine or do the maintenance thing.

As you write the code and you run into issues, post them and someone may be of help.

The main reasons you might use interrupts are:

  • To detect pin changes (eg. rotary encoders, button presses)

  • Watchdog timer (eg. if nothing happens after 8 seconds, interrupt me)

  • Timer interrupts - used for comparing/overflowing timers

  • SPI data transfers

  • I2C data transfers

  • USART data transfers

  • ADC conversions (analog to digital)

  • EEPROM ready for use

  • Flash memory ready
    https://gammon.com.au/interrupts

Solution right here. No code needed.

Hi all,

Thanks for the welcome and all the information.

I will look in the GPIO Pin else the SPDT switch.

but i will include the code down here,

maby you will spot some problemes or inprovements :slight_smile:

But i can't upload the file because i'm new and they wound allow me...

#include <LiquidCrystal.h> // includes the LiquidCrystal Library

LiquidCrystal lcd(12, 11, 9, 8, 7, 6); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 3;
const int echoPin = 2;
long duration;
int distanceCm;
int good;
int high;
int Empty;
const int LEDlampRed = 31;
const int LEDlampYellow = 32;
const int LEDlampGreen = 33;
const int Relaypomp = 52;
const int Klep = 53;

void setup() {
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT);
pinMode(Relaypomp, OUTPUT);
pinMode(Klep, OUTPUT);

}
void loop()
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin,HIGH);

distanceCm = ( duration / 2) / 29.1;
Serial.println("cm:");
Serial.println(distanceCm);

if( (distanceCm > 0) && (distanceCm <= 5) )
{
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, HIGH);
digitalWrite(Klep, HIGH);
digitalWrite(Relaypomp, LOW);
} else
if( (distanceCm > 5) && (distanceCm <= 20) )
{
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, LOW);
digitalWrite(Relaypomp, HIGH);
digitalWrite(Klep, LOW);
} else
if( (distanceCm > 20) && (distanceCm <=40) )
{

digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampRed, HIGH);
digitalWrite(Relaypomp, HIGH);
digitalWrite(Klep, LOW);

} else

if( distanceCm >40 )
{
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed, HIGH);
digitalWrite(Relaypomp, LOW);
digitalWrite(Klep, LOW);
}
lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
lcd.print("Distance: "); // Prints string "Distance" on the LCD
lcd.print(distanceCm); // Prints the distance value from the sensor
lcd.print("cm");

if( (distanceCm > 5) && (distanceCm <= 10) )
{
lcd.setCursor(0, 1);
lcd.print("high ");
} else

if( (distanceCm > 10) && (distanceCm <=40))
{
lcd.setCursor(0, 1);
lcd.print("good ");
} else

if ( distanceCm >40 )
{
lcd.setCursor(0, 1);
lcd.print("Empty");
}
delay(10);
}

Look again at the documentation for pulseln() and what it returns.

If you looking to turn the pump off for maintenance then for safety that should be by electrical isolation , not under software control.

2 Likes

You didn't put the code in code tags for posting. Also you should try auto formatting your code with ctrl-T in the IDE before re-posting.

To post images etc you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can…

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Hmm lets try.
I have if its correct attachts the document now :smile:

lvl.ino (2.6 KB)

Could you post the code in code tags?

#include <LiquidCrystal.h> // includes the LiquidCrystal Library

LiquidCrystal lcd(12, 11, 9, 8, 7, 6); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7)

const int trigPin = 3;
const int echoPin = 2;
long duration;
int distanceCm;
int good;
int high;
int Empty;
const int LEDlampRed = 31;
const int LEDlampYellow = 32;
const int LEDlampGreen = 33;
const int Relaypomp = 52;
const int Klep = 53;

void setup() {
  lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDlampRed, OUTPUT);
  pinMode(LEDlampYellow, OUTPUT);
  pinMode(LEDlampGreen, OUTPUT);
  pinMode(Relaypomp, OUTPUT);
  pinMode(Klep, OUTPUT);
 
}
void loop() 
{
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);  
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin,HIGH);

distanceCm = ( duration / 2) / 29.1;
Serial.println("cm:"); 
Serial.println(distanceCm);

if(  (distanceCm > 0) && (distanceCm <= 6)   ) 
{
  digitalWrite(LEDlampGreen, LOW);
  digitalWrite(LEDlampYellow, LOW);
  digitalWrite(LEDlampRed, HIGH);
  digitalWrite(Klep, HIGH);
  digitalWrite(Relaypomp, LOW);
} else
if(  (distanceCm > 5) && (distanceCm <= 20)  ) 
{
    digitalWrite(LEDlampGreen, HIGH);
    digitalWrite(LEDlampYellow, LOW);
    digitalWrite(LEDlampRed, LOW);
    digitalWrite(Relaypomp, HIGH);
    digitalWrite(Klep, LOW);
} else
if(  (distanceCm > 20) && (distanceCm <=40)  ) 
{

    digitalWrite(LEDlampYellow, HIGH);
    digitalWrite(LEDlampGreen, LOW);
    digitalWrite(LEDlampRed, HIGH);
    digitalWrite(Relaypomp, HIGH);
    digitalWrite(Klep, LOW);
} else

if(  distanceCm >40 ) 
{
    digitalWrite(LEDlampGreen, LOW);
    digitalWrite(LEDlampYellow, LOW);
    digitalWrite(LEDlampRed, HIGH);
    digitalWrite(Relaypomp, LOW);
    digitalWrite(Klep, LOW);
}
  lcd.setCursor(0, 0); // Sets the location at which subsequent text written to the LCD will be displayed
  lcd.print("Distance: "); // Prints string "Distance" on the LCD
  lcd.print(distanceCm); // Prints the distance value from the sensor
  lcd.print("cm");

if( (distanceCm > 5) && (distanceCm <= 10)  )
  {
    lcd.setCursor(0, 1);
    lcd.print("high ");
   } else
   
   if( (distanceCm > 10) && (distanceCm <=40))
  {
    lcd.setCursor(0, 1);
    lcd.print("good ");
  } else
  
  if (  distanceCm >40 ) 
  {
    lcd.setCursor(0, 1);
    lcd.print("Empty");  
  }  
  delay(10);
}
1 Like