Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino Code for Level Monitoring using Buzzer #59

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

animeshsrivastava24
Copy link

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

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

Checklist:

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

Reviewer: Vinit Shahdeo

The code adds an alarm to indicate that the tank is about to overflow. The controller used is Arduino-Uno
The detailed description is mentioned in vinitshahdeo#54
fixes vinitshahdeo#54
…tch-1

Added Arduino Code for Level Monitoring Buzzer
@welcome
Copy link

welcome bot commented Mar 1, 2020

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

Copy link
Owner

@vinitshahdeo vinitshahdeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@animeshsrivastava24 Please add a README.md in hardware and explain the implementation + installation + how to run sections.

@vinitshahdeo vinitshahdeo added the gssoc20 Welcome to GirlScript Summer of Code 2020 label Mar 2, 2020
@animeshsrivastava24
Copy link
Author

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

@ramanaditya
Copy link

#59 (comment)
@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?

Copy link
Owner

@vinitshahdeo vinitshahdeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@animeshsrivastava24 Can you please also add a SETUP.md file and explain the working and how to run sections?

@animeshsrivastava24
Copy link
Author

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

@vinitshahdeo
Copy link
Owner

@animeshsrivastava24 Make the changes in same PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gssoc20 Welcome to GirlScript Summer of Code 2020
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an alarm to indicate that the tank is about to overflow.
3 participants