varli_ketanpl

PATR Tunel

Jan 18th, 2024
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <Arduino_FreeRTOS.h>
  3. #include <semphr.h>
  4. #include "timers.h"
  5.  
  6. #define IN1 30
  7. #define IN2 32
  8.  
  9. #define OUT1 40
  10. #define OUT2 42
  11.  
  12. #define MAX 3
  13.  
  14. #define LED 24
  15. #define BUTTON 26
  16.  
  17. #define SMOKEPIN 22
  18.  
  19. LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
  20.  
  21. int carIn[] = { IN1, IN2 };
  22. int carOut[] = { OUT1, OUT2 };
  23.  
  24. int carCount = 0;
  25.  
  26. bool full = false;
  27. bool fire = false;
  28. bool panick = false;
  29. bool stopped = false;
  30.  
  31. void onEntrance(int* entrance);
  32. void onExit(int* exit);
  33.  
  34. void printCarCount();
  35. void printError();
  36.  
  37. void check(int* temp);
  38.  
  39. void panickEvent(int* temp);
  40.  
  41. void fireEvent(int* temp);
  42.  
  43. void setup() {
  44.   Serial.begin(9600);
  45.   lcd.begin(16, 2);
  46.   lcd.noCursor();
  47.  
  48.   for (int i = 0; i < 2; i++) {
  49.     pinMode(carIn[i], INPUT);
  50.     pinMode(carOut[i], INPUT);
  51.   }
  52.  
  53.   pinMode(LED, OUTPUT);
  54.   pinMode(BUTTON, INPUT);
  55.  
  56.   lcd.setCursor(0, 0);
  57.   lcd.print("Car count:");
  58.   printCarCount();
  59.  
  60.   xTaskCreate(onEntrance, "intrare1", 128, &carIn[0], 1, NULL);
  61.   xTaskCreate(onEntrance, "intrare2", 128, &carIn[1], 1, NULL);
  62.  
  63.   xTaskCreate(onExit, "iesire1", 128, &carOut[0], 1, NULL);
  64.   xTaskCreate(onExit, "iesire2", 128, &carOut[1], 1, NULL);
  65.  
  66.   xTaskCreate(check, "check", 128, NULL, 1, NULL);
  67.  
  68.   xTaskCreate(panickEvent, "panick", 128, NULL, 1, NULL);
  69.   xTaskCreate(fireEvent, "fire", 128, NULL, 1, NULL);
  70. }
  71.  
  72. void loop() {
  73. }
  74.  
  75. void onEntrance(int* entrance) {
  76.   while (true) {
  77.     int sensorData = digitalRead(*entrance);
  78.  
  79.     if (digitalRead(*entrance) == HIGH) {
  80.       if (!stopped) {
  81.         while (digitalRead(*entrance) == HIGH) {}
  82.         carCount++;
  83.         Serial.println("Am intrat");
  84.         if (carCount == MAX) {
  85.           full = true;
  86.           digitalWrite(LED, HIGH);
  87.         }
  88.  
  89.         printCarCount();
  90.       } else {
  91.         printError();
  92.         delay(1000);
  93.         printCarCount();
  94.       }
  95.     }
  96.   }
  97. }
  98.  
  99. void printError() {
  100.   lcd.clear();
  101.   lcd.setCursor(0, 0);
  102.  
  103.   if (full) {
  104.     lcd.print("Tunel plin!");
  105.     Serial.println("Tunel plin!");
  106.   } else if (panick) {
  107.     lcd.print("Mod de panica activat!");
  108.     lcd.setCursor(0, 1);
  109.     lcd.print("Nu se intra!");
  110.     Serial.println("Mod de panica activat! Nu se intra in tunel!");
  111.   } else {
  112.     lcd.print("Foc detectat!");
  113.     lcd.setCursor(0, 1);
  114.     lcd.print("Nu se intra!");
  115.     Serial.println("Foc detectat! Nu se intra!");
  116.   }
  117. }
  118.  
  119. void onExit(int* exit) {
  120.   while (true) {
  121.     if (digitalRead(*exit) == HIGH) {
  122.       while (digitalRead(*exit) == HIGH) {}
  123.       carCount--;
  124.       Serial.println("Am iesit");
  125.       if (carCount < MAX && full) {
  126.         digitalWrite(LED, LOW);
  127.         full = false;
  128.       }
  129.  
  130.       printCarCount();
  131.     }
  132.   }
  133. }
  134.  
  135. void printCarCount() {
  136.   lcd.clear();
  137.   lcd.setCursor(0, 0);
  138.   lcd.print("Numar masini:");
  139.   lcd.setCursor(0, 1);
  140.   lcd.print(carCount);
  141.   Serial.print("Numar masini: ");
  142.   Serial.println(carCount);
  143. }
  144.  
  145. void check(int* temp) {
  146.   while (true) {
  147.     stopped = full | fire | panick;
  148.     delay(100);
  149.   }
  150. }
  151.  
  152. void panickEvent(int* temp) {
  153.   while (true) {
  154.     int x = digitalRead(BUTTON);
  155.  
  156.     if (x == HIGH) {
  157.       panick = true;
  158.  
  159.       digitalWrite(LED, HIGH);
  160.  
  161.       lcd.clear();
  162.       lcd.setCursor(0, 0);
  163.       lcd.print("Buton de panica");
  164.       lcd.setCursor(0, 1);
  165.       lcd.print("apasat!");
  166.       delay(2000);
  167.       printCarCount();
  168.       Serial.println("Buton de panica apasat!");
  169.     }
  170.   }
  171. }
  172.  
  173. void fireEvent(int* temp) {
  174.   while (true) {
  175.     int x = digitalRead(SMOKEPIN);
  176.  
  177.     if (x == LOW) {
  178.       fire = true;
  179.  
  180.       digitalWrite(LED, HIGH);
  181.  
  182.       lcd.clear();
  183.       lcd.setCursor(0, 0);
  184.       lcd.print("Foc detectat!");
  185.       delay(2000);
  186.       printCarCount();
  187.     }
  188.   }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment