Advertisement
HaS5HeM

SMART HOME

Feb 17th, 2023 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.94 KB | Source Code | 0 0
  1. #include <dht.h>
  2. #include <Keypad.h>
  3.  
  4. //------------------PINS-------------------//
  5. //MONITORING
  6. int IR2 = 4;
  7. int IR1 = 3;
  8.  
  9. //SAFETY
  10. const byte ROWS = 4;
  11. const byte COLS = 4;
  12. byte rowPins[ROWS] = { 13, 12, 11, 10 };
  13. byte colPins[COLS] = { 9, 8, 7, 6 };
  14. int motor = 42;
  15.  
  16. //BULB AND LDR
  17. int ldr = A0;
  18. int bulb = 36;
  19. int bulbButton = 37;
  20. int ledsLow = 38;
  21. int ledsHigh = 39;
  22.  
  23. //FLAME DETECTOR WITH BAZAR
  24. int flame = 40;
  25. int bazar = 41;
  26.  
  27. //----------------LOGIC-----------------//
  28. //MONITORING
  29. int doneReading = false;
  30. char dir = 'n';
  31. int count = 0;
  32.  
  33. //SAFETY
  34. #define Password_Length 9
  35. char Data[Password_Length];
  36. // Password
  37. char Master[Password_Length] = "123456AB";
  38. byte data_count = 0;
  39. char keyValue;
  40. char hexakeys[ROWS][COLS] = {
  41.   { '1', '2', '3', 'A' },
  42.   { '4', '5', '6', 'B' },
  43.   { '7', '8', '9', 'C' },
  44.   { '*', '0', '#', 'D' }
  45. };
  46. Keypad customKeypad = Keypad(makeKeymap(hexakeys), rowPins, colPins, ROWS, COLS);
  47. bool isLocked = true;
  48.  
  49. //BULB
  50.  
  51. void setup() {
  52.   Serial.begin(9600);
  53.   pinMode(IR1, INPUT);
  54.   pinMode(IR2, INPUT);
  55.   pinMode(ldr, INPUT);
  56.   pinMode(bulb, OUTPUT);
  57.   pinMode(bulbButton, INPUT);
  58.   pinMode(ledsLow, OUTPUT);
  59.   pinMode(ledsHigh, OUTPUT);
  60.   pinMode(flame, INPUT);
  61.   pinMode(bazar, OUTPUT);
  62.   pinMode(motor, OUTPUT);
  63. }
  64.  
  65. void loop() {
  66.   monitoring();
  67.   keyPress();
  68.   bulbAndLeds();
  69.   flameCode();
  70. }
  71.  
  72. void countPersons() {
  73.   //digitalReads
  74.   bool ir1 = !digitalRead(IR1);
  75.   bool ir2 = !digitalRead(IR2);
  76.   Serial.println(ir1);
  77.   Serial.println(ir2);
  78.  
  79.   if (doneReading && !ir1 && !ir2) {
  80.     doneReading = false;
  81.     dir = 'n';
  82.   }
  83.   if (dir != 'n' && !doneReading) {
  84.     //dir e (for enter) && ir2 is 1 => make count++ and doneReading true
  85.     if (dir == 'e' && ir2) {
  86.       count++;
  87.       doneReading = true;
  88.     }
  89.     //dir l && ir1 => make count-- and doneReading true
  90.     if (dir == 'l' && ir1) {
  91.       count--;
  92.       doneReading = true;
  93.     }
  94.  
  95.   } else if (dir == 'n') {
  96.     if (ir1) {
  97.       dir = 'e';
  98.       doneReading = false;
  99.     } else if (ir2) {
  100.       dir = 'l';
  101.       doneReading = false;
  102.     }
  103.   }
  104.   Serial.print("doneReading ");
  105.   Serial.println(doneReading);
  106.   Serial.print("count ");
  107.   Serial.println(count);
  108.   Serial.print("direction ");
  109.   Serial.println(dir);
  110. }
  111.  
  112. void monitoring() {
  113.   countPersons();
  114. }
  115.  
  116. void keyPress() {
  117.   Serial.print("Enter Password:");
  118.   keyValue = customKeypad.getKey();
  119.   if (keyValue) {
  120.     Data[data_count] = keyValue;
  121.     Serial.print(Data[data_count]);
  122.     data_count++;
  123.   }
  124.   if (data_count == Password_Length - 1) {
  125.     Serial.println("Password Length exceeded");
  126.  
  127.     if (!strcmp(Data, Master)) {
  128.       // Password is correct
  129.       Serial.println("Correct");
  130.       // Turn on relay for 5 seconds
  131.       isLocked = false;
  132.       Serial.print("Locked: ");
  133.       Serial.println(isLocked);
  134.       //      digitalWrite(lockOutput, HIGH);
  135.       digitalWrite(motor, HIGH);
  136.       delay(500);
  137.       digitalWrite(motor, LOW);
  138.       isLocked = true;
  139.       Serial.print("Locked: ");
  140.       Serial.println(isLocked);
  141.     } else {
  142.       // Password is incorrect
  143.       Serial.println("Incorrect");
  144.       delay(1000);
  145.     }
  146.     clearPassword();
  147.   }
  148. }
  149.  
  150. void clearPassword() {
  151.   while (data_count != 0) {
  152.     Data[data_count--] = 0;
  153.   }
  154.   return;
  155. }
  156.  
  157. bool value = false;
  158. void bulbAndLeds() {
  159.   // CODE FOR BULB
  160.   int bulbRead = digitalRead(bulbButton);
  161.   while (bulbRead) {
  162.     //to block the code
  163.     value = !value;
  164.   }
  165.   if (value) {
  166.     digitalWrite(bulb, HIGH);
  167.   } else {
  168.     digitalWrite(bulb, LOW);
  169.   }
  170.  
  171.   //CODE FOR LEDS
  172.   int ldrRead = analogRead(ldr);
  173.   if (ldr <= 45) {
  174.     digitalWrite(ledsHigh, HIGH);
  175.     digitalWrite(ledsLow, HIGH);
  176.   } else if (ldr > 45 && ldr = < 90) {
  177.     digitalWrite(ledsHigh, HIGH);
  178.     digitalWrite(ledsLow, LOW);
  179.   } else {
  180.     digitalWrite(ledsHigh, LOW);
  181.     digitalWrite(ledsLow, LOW);
  182.   }
  183. }
  184.  
  185. void flameCode() {
  186.   digitalWrite(bazar, digitalRead(flame))
  187. }
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement