Thursday, March 14, 2019

Arduino based Damn Controller

This is the code for one of my recent projects. It is an Arduino based dam gate controller which detects water level at three levels. When it is at level one, indicator LED 1 will lit. When level is at level 2 indicator LED 1 and LED 2 will lit and also gate opens 50% after a while. When water level is at level 3, all LEDs will lit, buzzer will on and a SMS will be sent as an alert message. All levels will be indicated in 2*16 LCD display too.

Arduino code is as follows.

#include
#include
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
const int s1 = 14, s2 = 15, s3 = 16;// Sensors pins
const int l1 = 24, l2 = 26, l3 = 28;// LED pins
const int b1 = 22;//Buzzer
int level1 = 0, level2 = 0, level3 = 0;
boolean check3 = true;
boolean check4 = true;
char number[20];

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin



void setup() {
//Set up the LCD's number of columns and rows:
lcd.begin(16, 2);

//Attaches the servo on pin 9 to the servo object
myservo.attach(9);

//GSM Module
Serial1.begin(9600);

Serial.begin(9600);

//Sensors
pinMode(s1, INPUT_PULLUP);
pinMode(s2, INPUT_PULLUP);
pinMode(s3, INPUT_PULLUP);

//LEDs
pinMode(l1, OUTPUT);
pinMode(l2, OUTPUT);
pinMode(l3, OUTPUT);

//Buzzer
pinMode(b1, OUTPUT);

saveNumber();

}

void loop() {
level1 = digitalRead(s1);
level2 = digitalRead(s2);
level3 = digitalRead(s3);

//Off event
if (level1 == HIGH) {
digitalWrite(l1, LOW);//l1 off when s1 off
digitalWrite(b1, LOW);
}

if (level2 == HIGH) {
digitalWrite(l2, LOW);//l2 off when s2 off
digitalWrite(b1, LOW);
}

if (level3 == HIGH) {
digitalWrite(l3, LOW);//l3 off when s3 off
digitalWrite(b1, LOW);//b1 off when s3 off
check3 = true;
check4 = true;

}

//On event

if (level1 == LOW) {
digitalWrite(l1, HIGH);
if (level2 == LOW) {
digitalWrite(l2, HIGH);
if (level3 == LOW) {



digitalWrite(l3, HIGH);
digitalWrite(b1, HIGH);
lcd.clear();
lcd.print("Water level 3");
lcd.setCursor(0, 1);
lcd.print("Dam door 100%");

if (check3) {
Send();

if (Serial1.available() > 0) {
Serial.write(Serial1.read());
}
check3 = false;
}
delay(10000);//Door opens after 10 seconds
myservo.write(180);// sets the servo position s3 triggered
delay(15);// waits for the servo to get there
lcd.setCursor(0, 0);



} else {
lcd.clear();
lcd.print("Water level 2");
lcd.setCursor(0, 1);
lcd.print("Dam door 50%");
delay(3000);// Door opens after three seconds
myservo.write(90);// sets the servo position s2 triggered
delay(15);// waits for the servo to get there
lcd.setCursor(0, 0);
}


} else {
lcd.clear();
lcd.print("Water level 1");
lcd.setCursor(0, 1);
lcd.print("Dam door closed");
myservo.write(0);// sets the servo position s2 not triggered
delay(15);// waits for the servo to get there
lcd.setCursor(0, 0);
}

} else {
lcd.clear();
lcd.print("Normal level");
lcd.setCursor(0, 1);
lcd.print("Dam door closed");
myservo.write(0);// sets the servo position no level
delay(15);// waits for the servo to get there
lcd.setCursor(0, 0);

}


}

void Send()
{
Serial.print("Sending"); Serial1.println("AT+CMGF=1\r");
delay(300);
//Serial1.println("AT+CMGS=\"+xxxxxxxxxxxx\"\r"); // Replace x with mobile number
//delay(1000);
Serial1.print("AT + CMGS = \"");
Serial1.print(number);
Serial1.println("\"");
delay(300);

Serial1.println("Alert! Water level is High");// The SMS text you want to send
//delay(100);
//Serial1.println((char)26); // ASCII code of CTRL+Z
//delay(1000);

delay(300);
Serial1.println((char)26);
delay(300);
Serial1.println();
}

void call() {
Serial.println("Calling....");
Serial1.print("ATD xxxxxxxxxxx;");
//Serial1.println(number);
delay(100);
Serial1.println();
delay(30000);
Serial1.println("ATH");

}

void saveNumber() {
number[0] = '+';
number[1] = '9';
number[2] = '4';
number[3] = 'x';
number[4] = 'x';
number[5] = 'x';
number[6] = 'x';
number[7] = 'x';
number[8] = 'x';
number[9] = 'x';
number[10] = 'x';
number[11] = 'x';


}

No comments:

Post a Comment

Machine Learning

If you are a regular internet user you might be using Social media like Facebook and use Google daily as a search engine. Have you ever...