naivxnaivet

FINAL DISASTERCHECKER WITH WORKING SMS

Jun 8th, 2022
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1. #include <avr/wdt.h>
  2.  
  3. unsigned long logicMillisOld1 = 0;
  4. unsigned long logicMillisNew1 = 0;
  5. unsigned long logicInterval1 = 1000;
  6.  
  7. unsigned long logicMillisOld2 = 0;
  8. unsigned long logicMillisNew2 = 0;
  9. unsigned long logicInterval2 = 1000;
  10.  
  11. unsigned long logicMillisOld3 = 0;
  12. unsigned long logicMillisNew3 = 0;
  13. unsigned long logicInterval3 = 1000;
  14.  
  15.  
  16.  
  17. int logicNumber1 = 1;
  18. bool runlogic1 = false;
  19.  
  20. int logicNumber2 = 1;
  21. bool runlogic2 = false;
  22.  
  23. int logicNumber3 = 1;
  24. bool runlogic3 = false;
  25.  
  26. int buzzerPin  = 12; //Digital Pin 13
  27.  
  28. int floatSwitch = 2;
  29. int floatState = HIGH;
  30. int sequence = 0;
  31.  
  32.  
  33. int tiltPin = 3;     //Digital Pin 3
  34. int tiltValue = 0;
  35. int tiltCounter = 0;
  36.  
  37. #define mqPin (0)    //Analog 0
  38. float sensorValue;
  39. int mqSequence = 0;
  40.  
  41. int pwmBuzzer = 9;
  42.  
  43. char msg;
  44. char incomingByte;
  45. String inputString;
  46. const String number =  "9275156419"; // number na itetext ng gsm module
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. void setup()
  54. {
  55.   Serial.begin(115200);
  56.   Serial3.begin(115200);
  57.   pinMode(pwmBuzzer, OUTPUT);
  58.   pinMode(buzzerPin, OUTPUT);
  59.   pinMode(floatSwitch, INPUT_PULLUP);
  60.   pinMode(tiltPin, INPUT);
  61.   //  digitalWrite(buzzerPin, LOW);
  62.   digitalWrite(buzzerPin, HIGH);
  63. }
  64.  
  65. void loop() {
  66.   // put your main code here, to run repeatedly:
  67.   tiltValue = digitalRead(tiltPin);
  68.  
  69.   floatState = digitalRead(floatSwitch);
  70.   sensorValue = analogRead(mqPin);
  71.   sensorValue = map(sensorValue, 0, 1023, 0, 255);
  72.   logicMillisNew1 = millis();
  73.   logicMillisNew2 = millis();
  74.   logicMillisNew3 = millis();
  75.  
  76.   floatCheck();
  77.   floatBuzz();
  78.  
  79.   tiltCheck();
  80.   tiltBuzz();
  81.   //
  82.   mqCheck();
  83.   mqBuzz();
  84. }
  85.  
  86.  
  87. void floatBuzz() //buzzer condition if naka angat ba si float or hindi
  88. {
  89.   if (runlogic1)
  90.   {
  91.     switch (logicNumber1)
  92.     {
  93.       case 1:
  94.         logicInterval1 = 10000; //milliseconds kung gano katagal si buzzer if naka angat ba si float switch. 1 sec = 1000
  95.         logicNumber1 = 2;
  96.         Serial.println(F("BUZZER ON"));
  97.         digitalWrite(buzzerPin, LOW);
  98.         tone(pwmBuzzer, 500, 10000);
  99.         SendSMS(number, "FLOOD");
  100.         break;
  101.  
  102.       case 2:
  103.         logicInterval1 = 0; //
  104.         Serial.println(F("BUZZER OFF"));
  105.         //     digitalWrite(buzzerPin, HIGH);
  106.         digitalWrite(buzzerPin, HIGH);
  107.         logicNumber1 = 0;
  108.         noTone(pwmBuzzer);
  109.         break;
  110.     }
  111.     runlogic1 = false;
  112.   }
  113. }
  114.  
  115. void floatCheck() //this will check if naka angat si float switch or naka baba. naka angat = mataas tubig. naka baba = no flood.
  116. {
  117.  
  118.   if (floatState == LOW)  //NAKABABA SI FLOATSWITCH
  119.   {
  120.     Serial.println("WATER LEVEL - LOW");
  121.     if (sequence > 0)
  122.     {
  123.       logicNumber1 = 1;
  124.     }
  125.   }
  126.   else if (floatState == HIGH)    //NAKA ANGAT SI FLOAT SWITCH
  127.   {
  128.     Serial.println("WATER LEVEL - HIGH");
  129.     sequence++;
  130.     if (logicMillisNew1 - logicMillisOld1 >= logicInterval1)
  131.     {
  132.       logicMillisOld1 = logicMillisNew1;
  133.       runlogic1 = true;
  134.     }
  135.   }
  136. }
  137.  
  138. void tiltCheck() //this condition will check if may tilting ba si sensor or wala
  139. {
  140.   if (tiltValue == 1)    //condition na hindi naalog si sensor
  141.   {
  142.     Serial.println("No motion");
  143.   }
  144.  
  145.   else if (tiltValue == 0)  //condition is naalog si sensor
  146.   {
  147.     tiltCounter++;      //dumadagdag tilt count kada maalog si sensor
  148.   }
  149.  
  150.   if (tiltCounter >= 6)  //change this for calibration. this is for the tilt times.
  151.   {
  152.     if (logicMillisNew2 - logicMillisOld2 >= logicInterval2)
  153.     {
  154.       logicMillisOld2 = logicMillisNew2;
  155.       runlogic2 = true;
  156.     }
  157.   }
  158.  
  159.  
  160. }
  161.  
  162.  
  163. void tiltBuzz() //buzzer condition if na meet yung tilt times
  164. {
  165.   if (runlogic2)
  166.   {
  167.     switch (logicNumber2)
  168.     {
  169.       case 1:
  170.         logicInterval2 = 10000; //milliseconds kung gano katagal si buzzer if nameet yung tilt condition. 1 sec = 1000
  171.         logicNumber2 = 2;
  172.         Serial.println(F("BUZZER ON"));
  173.         digitalWrite(buzzerPin, LOW);
  174.         tone(pwmBuzzer, 500, 10000);
  175.         SendSMS(number, "EARTHQUAKE");
  176.         //digitalWrite(buzzerPin, LOW);
  177.  
  178.         break;
  179.  
  180.       case 2:
  181.         logicInterval2 = 0;
  182.         Serial.println(F("BUZZER OFF"));
  183.         digitalWrite(buzzerPin, HIGH);
  184.         //digitalWrite(buzzerPin, HIGH);
  185.         logicNumber2 = 0;
  186.         noTone(pwmBuzzer);
  187.         wdt_enable(WDTO_15MS);     //rechecking condition
  188.         break;
  189.     }
  190.     runlogic2 = false;
  191.   }
  192. }
  193.  
  194. void mqCheck() //this will check if may smoke/carbon dioxide ba na detected si Mq2
  195. {
  196.   Serial.println(sensorValue);
  197.   if (sensorValue < 99) //change this for calibration. this is the no smoke value
  198.   {
  199.     Serial.println("No smoke");
  200.   }
  201.  
  202.   else if (sensorValue >= 100) //change this for calibration. this is the smoke detected value
  203.   {
  204.     Serial.println("SMOKE DETECTED");
  205.  
  206.     if (logicMillisNew3 - logicMillisOld3 >= logicInterval3)
  207.     {
  208.       logicMillisOld3 = logicMillisNew3;
  209.       runlogic3 = true;
  210.     }
  211.   }
  212.  
  213. }
  214.  
  215. void mqBuzz() //BUZZER CONDITION IF MQ2 DETECTED CARBON DIOXIDE / SMOKE
  216. {
  217.   if (runlogic3)
  218.   {
  219.     switch (logicNumber3)
  220.     {
  221.       case 1:
  222.         logicInterval3 = 10000; //milliseconds kung gano katagal si buzzer if nakadetect ba si mq2 ng usok. 1 sec = 1000
  223.         logicNumber3 = 2; // Next Function
  224.         Serial.println(F("BUZZER ON"));
  225.         digitalWrite(buzzerPin, LOW);
  226.         tone(pwmBuzzer, 500, 10000);
  227.         //digitalWrite(buzzerPin, LOW);
  228.         SendSMS(number, "Smoke Detected");
  229.         break;
  230.  
  231.       case 2:
  232.         logicInterval3 = 0; // Delay
  233.         Serial.println(F("BUZZER OFF"));
  234.         digitalWrite(buzzerPin, HIGH);
  235.         //digitalWrite(buzzerPin, HIGH);
  236.         logicNumber3 = 0;
  237.         noTone(pwmBuzzer);
  238.         wdt_enable(WDTO_15MS);
  239.         break;
  240.     }
  241.     runlogic3 = false;
  242.   }
  243. }
  244.  
  245. void SendSMS(String number, String msg) { // Ito ung function na magsesend ng SMS
  246.   Serial3.println("AT+CMGF=1");
  247.   delay(1000);
  248.   Serial3.print("AT+CMGS=\"+63");
  249.   Serial3.print(number);
  250.   Serial3.print("\"\r");
  251.   delay(1000);
  252.   Serial3.print(msg);
  253.   delay(100);
  254.   Serial3.print((char)26);
  255.   delay(1000);
  256. }
  257.  
  258. void DelSentSMS() {
  259.   Serial3.print("AT+CMGDA=\"");
  260.   Serial3.println("DEL SENT\"");
  261.   delay(500);
  262.   Serial.println( "All Sent Messages Deleted" );
  263. }
  264.  
  265. void DelReadSMS() {
  266.   Serial3.print("AT+CMGDA=\"");
  267.   Serial3.println("DEL READ\"");
  268.   delay(500);
  269.   Serial.println( "All Read Messages Deleted" );
  270. }
  271.  
  272. void ReceiveMessage() {
  273.   Serial3.println("AT+CNMI=2,2,0,0,0 "); // AT Command to recieve a live SMS
  274.   delay(1000);
  275. }
Advertisement
Add Comment
Please, Sign In to add comment