Water-Monitoring-System icon indicating copy to clipboard operation
Water-Monitoring-System copied to clipboard

Arduino Code for Level Monitoring using Buzzer

Open animeshsrivastava24 opened this issue 6 years ago • 5 comments

Hello @vinitshahdeo @PragatiVerma18 @ramanaditya I'll like to make a contribution to the following:

My Solution:

Description

For this we will have to implement a finite state machine. For our particular problem, I would implement a machine with three states: IDLE, ARMED and ALARM, the ALARM state being the only one in which the buzzer is on. The transitions would be: (Say time for ALARM to sound is 15 seconds)

ARMED → ALARM when the level hits 100% ALARM → IDLE after 15 seconds spent in the ALARM state IDLE → ARMED when the level gets below 90%

Note that the only difference between IDLE and ARMED is that the IDLE state will not go into ALARM when the level reads 100%, as you have to go through the ARMED state before. The threshold at 90% percent (can be changed as desired, added to maintain any lag in working).

#define trigPin 8
#define echoPin 9
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int BUZZER = 10 ;

void setup(){
      int duration,distance,percentage,heightTank;
      Serial.begin (9600);
      pinMode(trigPin,OUTPUT);
      pinMode(echoPin,INPUT);
      lcd.begin(16,2);
      lcd.print("HELLO");
        pinMode(BUZZER,OUTPUT);
}

void loop(){
      int duration,distance,percentage,heightTank,deviation;
      // We can change the next 2 lines.
      // The first one is the max. level of the water.
      // The next one is how high the sensor is above that max. level.
      heightTank=65;
      deviation=4;

      digitalWrite(trigPin,HIGH);
      delayMicroseconds(1000);
      digitalWrite(trigPin,LOW);
      duration=pulseIn(echoPin,HIGH);
      distance=(duration/2)/29.1;
      percentage=100-(((distance-deviation)*100)/heightTank);
      Serial.println(distance);
      Serial.println(percentage);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Current tank");
      //lcd.print(distance);
      //lcd.print(" cm");
      lcd.setCursor(0,1);
      lcd.print("level: ");
      lcd.print(percentage);
      lcd.print(" %");
      delay(1000);

      digitalWrite(BUZZER,HIGH);
     static unsigned long lastBuzzer = 0;

      if(percentage > 99 && lastBuzzer == 0) {
      lastBuzzer = millis();
}

      if(percentage > 99) {
      if(millis() - lastBuzzer < 15000L) {
        digitalWrite(BUZZER,HIGH);
        delay(100);
        digitalWrite(BUZZER,LOW);
        delay(100);
    }
}      else  {
       digitalWrite(BUZZER,LOW);
       lastBuzzer = 0;
}
  }

lastBuzzer is the last buzzer activation time (in ms). It's use to control that buzzing stop after 15 seconds. It's zero when the percentage is < 99; it's not zero when percentage >= 100.

The code is based on finite state machine where the state is implicitly encoded into the variables lastBuzzer and percentage. The implicit state is:

ARMED when lastBuzzer == 0 ALARM when percentage > 99 && millis() - lastBuzzer < 15000L IDLE when percentage > 99 && millis() - lastBuzzer >= 15000L The line lastBuzzer = 0 establishes the ARMED state as soon as percentage ≤ 99

Fixes #54

Type of change

  • [x] Bug fix (non-breaking change which fixes an issue)

Checklist:

  • [x] My code follows the style guidelines of this project
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [x] I have made corresponding changes to the documentation

Reviewer: Vinit Shahdeo

animeshsrivastava24 avatar Mar 01 '20 01:03 animeshsrivastava24

Thanks for opening this pull request! Please add @vinitshahdeo as a reviewer if you haven't added.

welcome[bot] avatar Mar 01 '20 01:03 welcome[bot]

It was accidental, sure I'll do the necessary.

animeshsrivastava24 avatar Mar 02 '20 10:03 animeshsrivastava24

https://github.com/vinitshahdeo/Water-Monitoring-System/pull/59#issue-381969564 @animeshsrivastava24 have you tested the code? if yes, then please attach the screenshots of the output. If not then please do the above

Please share the microcontroller using which you have written/tested the code, and how much memory does it consume?

ramanaditya avatar Mar 02 '20 10:03 ramanaditya

Hello @ramanaditya @vinitshahdeo should I add the mentioned file via a separate PR?

animeshsrivastava24 avatar Mar 03 '20 04:03 animeshsrivastava24

@animeshsrivastava24 Make the changes in same PR.

vinitshahdeo avatar Mar 03 '20 06:03 vinitshahdeo