Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/wdt.h>
- unsigned long logicMillisOld1 = 0;
- unsigned long logicMillisNew1 = 0;
- unsigned long logicInterval1 = 1000;
- unsigned long logicMillisOld2 = 0;
- unsigned long logicMillisNew2 = 0;
- unsigned long logicInterval2 = 1000;
- unsigned long logicMillisOld3 = 0;
- unsigned long logicMillisNew3 = 0;
- unsigned long logicInterval3 = 1000;
- int logicNumber1 = 1;
- bool runlogic1 = false;
- int logicNumber2 = 1;
- bool runlogic2 = false;
- int logicNumber3 = 1;
- bool runlogic3 = false;
- int buzzerPin = 12; //Digital Pin 13
- int floatSwitch = 2;
- int floatState = HIGH;
- int sequence = 0;
- int tiltPin = 3; //Digital Pin 3
- int tiltValue = 0;
- int tiltCounter = 0;
- #define mqPin (0) //Analog 0
- float sensorValue;
- int mqSequence = 0;
- int pwmBuzzer = 9;
- char msg;
- char incomingByte;
- String inputString;
- const String number = "9275156419"; // number na itetext ng gsm module
- void setup()
- {
- Serial.begin(115200);
- Serial3.begin(115200);
- pinMode(pwmBuzzer, OUTPUT);
- pinMode(buzzerPin, OUTPUT);
- pinMode(floatSwitch, INPUT_PULLUP);
- pinMode(tiltPin, INPUT);
- // digitalWrite(buzzerPin, LOW);
- digitalWrite(buzzerPin, HIGH);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- tiltValue = digitalRead(tiltPin);
- floatState = digitalRead(floatSwitch);
- sensorValue = analogRead(mqPin);
- sensorValue = map(sensorValue, 0, 1023, 0, 255);
- logicMillisNew1 = millis();
- logicMillisNew2 = millis();
- logicMillisNew3 = millis();
- floatCheck();
- floatBuzz();
- tiltCheck();
- tiltBuzz();
- //
- mqCheck();
- mqBuzz();
- }
- void floatBuzz() //buzzer condition if naka angat ba si float or hindi
- {
- if (runlogic1)
- {
- switch (logicNumber1)
- {
- case 1:
- logicInterval1 = 10000; //milliseconds kung gano katagal si buzzer if naka angat ba si float switch. 1 sec = 1000
- logicNumber1 = 2;
- Serial.println(F("BUZZER ON"));
- digitalWrite(buzzerPin, LOW);
- tone(pwmBuzzer, 500, 10000);
- SendSMS(number, "FLOOD");
- break;
- case 2:
- logicInterval1 = 0; //
- Serial.println(F("BUZZER OFF"));
- // digitalWrite(buzzerPin, HIGH);
- digitalWrite(buzzerPin, HIGH);
- logicNumber1 = 0;
- noTone(pwmBuzzer);
- break;
- }
- runlogic1 = false;
- }
- }
- void floatCheck() //this will check if naka angat si float switch or naka baba. naka angat = mataas tubig. naka baba = no flood.
- {
- if (floatState == LOW) //NAKABABA SI FLOATSWITCH
- {
- Serial.println("WATER LEVEL - LOW");
- if (sequence > 0)
- {
- logicNumber1 = 1;
- }
- }
- else if (floatState == HIGH) //NAKA ANGAT SI FLOAT SWITCH
- {
- Serial.println("WATER LEVEL - HIGH");
- sequence++;
- if (logicMillisNew1 - logicMillisOld1 >= logicInterval1)
- {
- logicMillisOld1 = logicMillisNew1;
- runlogic1 = true;
- }
- }
- }
- void tiltCheck() //this condition will check if may tilting ba si sensor or wala
- {
- if (tiltValue == 1) //condition na hindi naalog si sensor
- {
- Serial.println("No motion");
- }
- else if (tiltValue == 0) //condition is naalog si sensor
- {
- tiltCounter++; //dumadagdag tilt count kada maalog si sensor
- }
- if (tiltCounter >= 6) //change this for calibration. this is for the tilt times.
- {
- if (logicMillisNew2 - logicMillisOld2 >= logicInterval2)
- {
- logicMillisOld2 = logicMillisNew2;
- runlogic2 = true;
- }
- }
- }
- void tiltBuzz() //buzzer condition if na meet yung tilt times
- {
- if (runlogic2)
- {
- switch (logicNumber2)
- {
- case 1:
- logicInterval2 = 10000; //milliseconds kung gano katagal si buzzer if nameet yung tilt condition. 1 sec = 1000
- logicNumber2 = 2;
- Serial.println(F("BUZZER ON"));
- digitalWrite(buzzerPin, LOW);
- tone(pwmBuzzer, 500, 10000);
- SendSMS(number, "EARTHQUAKE");
- //digitalWrite(buzzerPin, LOW);
- break;
- case 2:
- logicInterval2 = 0;
- Serial.println(F("BUZZER OFF"));
- digitalWrite(buzzerPin, HIGH);
- //digitalWrite(buzzerPin, HIGH);
- logicNumber2 = 0;
- noTone(pwmBuzzer);
- wdt_enable(WDTO_15MS); //rechecking condition
- break;
- }
- runlogic2 = false;
- }
- }
- void mqCheck() //this will check if may smoke/carbon dioxide ba na detected si Mq2
- {
- Serial.println(sensorValue);
- if (sensorValue < 99) //change this for calibration. this is the no smoke value
- {
- Serial.println("No smoke");
- }
- else if (sensorValue >= 100) //change this for calibration. this is the smoke detected value
- {
- Serial.println("SMOKE DETECTED");
- if (logicMillisNew3 - logicMillisOld3 >= logicInterval3)
- {
- logicMillisOld3 = logicMillisNew3;
- runlogic3 = true;
- }
- }
- }
- void mqBuzz() //BUZZER CONDITION IF MQ2 DETECTED CARBON DIOXIDE / SMOKE
- {
- if (runlogic3)
- {
- switch (logicNumber3)
- {
- case 1:
- logicInterval3 = 10000; //milliseconds kung gano katagal si buzzer if nakadetect ba si mq2 ng usok. 1 sec = 1000
- logicNumber3 = 2; // Next Function
- Serial.println(F("BUZZER ON"));
- digitalWrite(buzzerPin, LOW);
- tone(pwmBuzzer, 500, 10000);
- //digitalWrite(buzzerPin, LOW);
- SendSMS(number, "Smoke Detected");
- break;
- case 2:
- logicInterval3 = 0; // Delay
- Serial.println(F("BUZZER OFF"));
- digitalWrite(buzzerPin, HIGH);
- //digitalWrite(buzzerPin, HIGH);
- logicNumber3 = 0;
- noTone(pwmBuzzer);
- wdt_enable(WDTO_15MS);
- break;
- }
- runlogic3 = false;
- }
- }
- void SendSMS(String number, String msg) { // Ito ung function na magsesend ng SMS
- Serial3.println("AT+CMGF=1");
- delay(1000);
- Serial3.print("AT+CMGS=\"+63");
- Serial3.print(number);
- Serial3.print("\"\r");
- delay(1000);
- Serial3.print(msg);
- delay(100);
- Serial3.print((char)26);
- delay(1000);
- }
- void DelSentSMS() {
- Serial3.print("AT+CMGDA=\"");
- Serial3.println("DEL SENT\"");
- delay(500);
- Serial.println( "All Sent Messages Deleted" );
- }
- void DelReadSMS() {
- Serial3.print("AT+CMGDA=\"");
- Serial3.println("DEL READ\"");
- delay(500);
- Serial.println( "All Read Messages Deleted" );
- }
- void ReceiveMessage() {
- Serial3.println("AT+CNMI=2,2,0,0,0 "); // AT Command to recieve a live SMS
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment