safwan092

parking tracker with lcd and iot

Jun 28th, 2020
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal.h>
  2. /*
  3.    LCD in 4-Bit-Modus:
  4.        LCD RS pin an digital pin 8
  5.        LCD RW pin an digital pin 13
  6.        LCD Enable pin an digital pin 9
  7.        LCD D4 pin an digital pin 4
  8.        LCD D5 pin an digital pin 5
  9.        LCD D6 pin an digital pin 6
  10.        LCD D7 pin an digital pin 7
  11. */
  12. //LCD Lib
  13. int p1Red = 22;
  14. int p2Red = 24;
  15. int p3Red = 26;
  16.  
  17. int p1Green = 23;
  18. int p2Green = 25;
  19. int p3Green = 27;
  20.  
  21. int p1Pin = 14;
  22. int p2Pin = 15;
  23. int p3Pin = 16;
  24.  
  25. int p1Status = 0;
  26. int p2Status = 0;
  27. int p3Status = 0;
  28.  
  29. int emptySpaces = 3;
  30. LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
  31.  
  32. void setup() {
  33.   Serial.begin(9600);
  34.   Serial1.begin(115200);
  35.  
  36.   pinMode(p1Pin, INPUT);
  37.   pinMode(p2Pin, INPUT);
  38.   pinMode(p3Pin, INPUT);
  39.  
  40.   pinMode(p1Red, OUTPUT);
  41.   pinMode(p2Red, OUTPUT);
  42.   pinMode(p3Red, OUTPUT);
  43.   pinMode(p1Green, OUTPUT);
  44.   pinMode(p2Green, OUTPUT);
  45.   pinMode(p3Green, OUTPUT);
  46.  
  47.   // Car Park No.1 LED Control
  48.   digitalWrite(p1Green, 1);
  49.   digitalWrite(p1Red, 0);
  50.  
  51.   // Car Park No.2 LED Control
  52.   digitalWrite(p2Green, 1);
  53.   digitalWrite(p2Red, 0);
  54.  
  55.   // Car Park No.3 LED Control
  56.   digitalWrite(p3Green, 1);
  57.   digitalWrite(p3Red, 0);
  58.  
  59.  
  60.   // Display initialisieren
  61.   lcd.clear();
  62.   lcd.begin(16, 2); // 16x2 Zeichen
  63.   lcd.setCursor(0, 0);
  64.   lcd.print("Parking Tracker");
  65.   lcd.setCursor(0, 1);
  66.   lcd.print("Initializing...");
  67.   delay(1000);
  68.   lcd.clear();
  69.  
  70.   //delay(50000);
  71. }
  72.  
  73.  
  74. void loop() {
  75.  
  76.   p1Status = digitalRead(p1Pin);
  77.   p2Status = digitalRead(p2Pin);
  78.   p3Status = digitalRead(p3Pin);
  79.   /*Serial.print(p1Status);
  80.     Serial.print(p2Status);
  81.     Serial.println(p3Status);*/
  82.  
  83.  
  84.   /////////////////////////////////////////////////////////////// 2 SPACES LEFT OPEN
  85.   if (p1Status == 1 && p2Status == 0 && p3Status == 0) {
  86.     emptySpaces = 2;
  87.     // Car Park No.1 LED Control
  88.     digitalWrite(p1Green, 0);
  89.     digitalWrite(p1Red, 1);
  90.  
  91.     // Car Park No.2 LED Control
  92.     digitalWrite(p2Green, 1);
  93.     digitalWrite(p2Red, 0);
  94.  
  95.     // Car Park No.3 LED Control
  96.     digitalWrite(p3Green, 1);
  97.     digitalWrite(p3Red, 0);
  98.  
  99.     //Show 2 on screen
  100.     sos2();
  101.     //Send 2 to Nodemcu
  102.     Serial1.write(2);
  103.  
  104.   }
  105.   else if (p1Status == 0 && p2Status == 1 && p3Status == 0) {
  106.     emptySpaces = 2;
  107.     // Car Park No.1 LED Control
  108.     digitalWrite(p1Green, 1);
  109.     digitalWrite(p1Red, 0);
  110.  
  111.     // Car Park No.2 LED Control
  112.     digitalWrite(p2Green, 0);
  113.     digitalWrite(p2Red, 1);
  114.  
  115.     // Car Park No.3 LED Control
  116.     digitalWrite(p3Green, 1);
  117.     digitalWrite(p3Red, 0);
  118.  
  119.     //Show 2 on screen
  120.     sos2();
  121.     //Send 2 to Nodemcu
  122.     Serial1.write(2);
  123.   }
  124.   else if (p1Status == 0 && p2Status == 0 && p3Status == 1) {
  125.     emptySpaces = 2;
  126.     // Car Park No.1 LED Control
  127.     digitalWrite(p1Green, 1);
  128.     digitalWrite(p1Red, 0);
  129.  
  130.     // Car Park No.2 LED Control
  131.     digitalWrite(p2Green, 1);
  132.     digitalWrite(p2Red, 0);
  133.  
  134.     // Car Park No.3 LED Control
  135.     digitalWrite(p3Green, 0);
  136.     digitalWrite(p3Red, 1);
  137.  
  138.     //Show 2 on screen
  139.     sos2();
  140.     //Send 2 to Nodemcu
  141.     Serial1.write(2);
  142.   }
  143.   //////////////////////////////////////////////////////////////////////////
  144.  
  145.   /////////////////////////////////////////////////////////////// 1 SPACES LEFT OPEN
  146.   else if (p1Status == 1 && p2Status == 1 && p3Status == 0) {
  147.     emptySpaces = 1;
  148.     // Car Park No.1 LED Control
  149.     digitalWrite(p1Green, 0);
  150.     digitalWrite(p1Red, 1);
  151.  
  152.     // Car Park No.2 LED Control
  153.     digitalWrite(p2Green, 0);
  154.     digitalWrite(p2Red, 1);
  155.  
  156.     // Car Park No.3 LED Control
  157.     digitalWrite(p3Green, 1);
  158.     digitalWrite(p3Red, 0);
  159.  
  160.     //Show 1 on screen
  161.     sos1();
  162.     //Send 1 to Nodemcu
  163.     Serial1.write(1);
  164.   }
  165.   else if (p1Status == 1 && p2Status == 0 && p3Status == 1) {
  166.     emptySpaces = 1;
  167.     // Car Park No.1 LED Control
  168.     digitalWrite(p1Green, 0);
  169.     digitalWrite(p1Red, 1);
  170.  
  171.     // Car Park No.2 LED Control
  172.     digitalWrite(p2Green, 1);
  173.     digitalWrite(p2Red, 0);
  174.  
  175.     // Car Park No.3 LED Control
  176.     digitalWrite(p3Green, 0);
  177.     digitalWrite(p3Red, 1);
  178.  
  179.     //Show 1 on screen
  180.     sos1();
  181.     //Send 1 to Nodemcu
  182.     Serial1.write(1);
  183.   }
  184.   else if (p1Status == 0 && p2Status == 1 && p3Status == 1) {
  185.     emptySpaces = 1;
  186.     // Car Park No.1 LED Control
  187.     digitalWrite(p1Green, 1);
  188.     digitalWrite(p1Red, 0);
  189.  
  190.     // Car Park No.2 LED Control
  191.     digitalWrite(p2Green, 0);
  192.     digitalWrite(p2Red, 1);
  193.  
  194.     // Car Park No.3 LED Control
  195.     digitalWrite(p3Green, 0);
  196.     digitalWrite(p3Red, 1);
  197.  
  198.     //Show 1 on screen
  199.     sos1();
  200.     //Send 1 to Nodemcu
  201.     Serial1.write(1);
  202.   }
  203.   //////////////////////////////////////////////////////////////////////////
  204.  
  205.   /////////////////////////////////////////////////////////////// 0 SPACES LEFT OPEN
  206.   else if (p1Status == 1 && p2Status == 1 && p3Status == 1) {
  207.     emptySpaces = 0;
  208.     // Car Park No.1 LED Control
  209.     digitalWrite(p1Green, 0);
  210.     digitalWrite(p1Red, 1);
  211.  
  212.     // Car Park No.2 LED Control
  213.     digitalWrite(p2Green, 0);
  214.     digitalWrite(p2Red, 1);
  215.  
  216.     // Car Park No.3 LED Control
  217.     digitalWrite(p3Green, 0);
  218.     digitalWrite(p3Red, 1);
  219.  
  220.     //Show 0 on screen
  221.     sos0();
  222.     //Send 0 to Nodemcu
  223.     Serial1.write(0);
  224.   }
  225.   //////////////////////////////////////////////////////////////////////////
  226.  
  227.   /////////////////////////////////////////////////////////////// 3 SPACES LEFT OPEN
  228.   else if (p1Status == 0 && p2Status == 0 && p3Status == 0) {
  229.     emptySpaces = 3;
  230.     // Car Park No.1 LED Control
  231.     digitalWrite(p1Green, 1);
  232.     digitalWrite(p1Red, 0);
  233.  
  234.     // Car Park No.2 LED Control
  235.     digitalWrite(p2Green, 1);
  236.     digitalWrite(p2Red, 0);
  237.  
  238.     // Car Park No.3 LED Control
  239.     digitalWrite(p3Green, 1);
  240.     digitalWrite(p3Red, 0);
  241.  
  242.     //Show 3 on screen
  243.     sos3();
  244.     //Send 3 to Nodemcu
  245.     Serial1.write(3);
  246.   }
  247.  
  248.   delay(2000);
  249. }
  250.  
  251.  
  252. void sos0() {
  253.   lcd.setCursor(0, 0);
  254.   //////////"1234567891234567"
  255.   lcd.print("Full parking!!  ");
  256.   lcd.setCursor(1, 1);
  257.   lcd.print("No Space Left");
  258.   delay(500);
  259. }
  260.  
  261. void sos1() {
  262.   lcd.setCursor(0, 0);
  263.   //////////"1234567891234567"
  264.   lcd.print("Empty parking:  ");
  265.   lcd.setCursor(1, 1);
  266.   lcd.print(1);
  267.   delay(500);
  268. }
  269.  
  270. void sos2() {
  271.   lcd.setCursor(0, 0);
  272.   //////////"1234567891234567"
  273.   lcd.print("Empty parking:  ");
  274.   lcd.setCursor(1, 1);
  275.   lcd.print(2);
  276.   delay(500);
  277. }
  278.  
  279. void sos3() {
  280.   lcd.setCursor(0, 0);
  281.   //////////"1234567891234567"
  282.   lcd.print("Empty parking:  ");
  283.   lcd.setCursor(1, 1);
  284.   lcd.print(3);
  285.   delay(500);
  286. }
RAW Paste Data