Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <Arduino_FreeRTOS.h>
- #include <semphr.h>
- #include "timers.h"
- #define IN1 30
- #define IN2 32
- #define OUT1 40
- #define OUT2 42
- #define MAX 3
- #define LED 24
- #define BUTTON 26
- #define SMOKEPIN 22
- LiquidCrystal lcd(12, 11, 9, 8, 7, 6);
- int carIn[] = { IN1, IN2 };
- int carOut[] = { OUT1, OUT2 };
- int carCount = 0;
- bool full = false;
- bool fire = false;
- bool panick = false;
- bool stopped = false;
- void onEntrance(int* entrance);
- void onExit(int* exit);
- void printCarCount();
- void printError();
- void check(int* temp);
- void panickEvent(int* temp);
- void fireEvent(int* temp);
- void setup() {
- Serial.begin(9600);
- lcd.begin(16, 2);
- lcd.noCursor();
- for (int i = 0; i < 2; i++) {
- pinMode(carIn[i], INPUT);
- pinMode(carOut[i], INPUT);
- }
- pinMode(LED, OUTPUT);
- pinMode(BUTTON, INPUT);
- lcd.setCursor(0, 0);
- lcd.print("Car count:");
- printCarCount();
- xTaskCreate(onEntrance, "intrare1", 128, &carIn[0], 1, NULL);
- xTaskCreate(onEntrance, "intrare2", 128, &carIn[1], 1, NULL);
- xTaskCreate(onExit, "iesire1", 128, &carOut[0], 1, NULL);
- xTaskCreate(onExit, "iesire2", 128, &carOut[1], 1, NULL);
- xTaskCreate(check, "check", 128, NULL, 1, NULL);
- xTaskCreate(panickEvent, "panick", 128, NULL, 1, NULL);
- xTaskCreate(fireEvent, "fire", 128, NULL, 1, NULL);
- }
- void loop() {
- }
- void onEntrance(int* entrance) {
- while (true) {
- int sensorData = digitalRead(*entrance);
- if (digitalRead(*entrance) == HIGH) {
- if (!stopped) {
- while (digitalRead(*entrance) == HIGH) {}
- carCount++;
- Serial.println("Am intrat");
- if (carCount == MAX) {
- full = true;
- digitalWrite(LED, HIGH);
- }
- printCarCount();
- } else {
- printError();
- delay(1000);
- printCarCount();
- }
- }
- }
- }
- void printError() {
- lcd.clear();
- lcd.setCursor(0, 0);
- if (full) {
- lcd.print("Tunel plin!");
- Serial.println("Tunel plin!");
- } else if (panick) {
- lcd.print("Mod de panica activat!");
- lcd.setCursor(0, 1);
- lcd.print("Nu se intra!");
- Serial.println("Mod de panica activat! Nu se intra in tunel!");
- } else {
- lcd.print("Foc detectat!");
- lcd.setCursor(0, 1);
- lcd.print("Nu se intra!");
- Serial.println("Foc detectat! Nu se intra!");
- }
- }
- void onExit(int* exit) {
- while (true) {
- if (digitalRead(*exit) == HIGH) {
- while (digitalRead(*exit) == HIGH) {}
- carCount--;
- Serial.println("Am iesit");
- if (carCount < MAX && full) {
- digitalWrite(LED, LOW);
- full = false;
- }
- printCarCount();
- }
- }
- }
- void printCarCount() {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Numar masini:");
- lcd.setCursor(0, 1);
- lcd.print(carCount);
- Serial.print("Numar masini: ");
- Serial.println(carCount);
- }
- void check(int* temp) {
- while (true) {
- stopped = full | fire | panick;
- delay(100);
- }
- }
- void panickEvent(int* temp) {
- while (true) {
- int x = digitalRead(BUTTON);
- if (x == HIGH) {
- panick = true;
- digitalWrite(LED, HIGH);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Buton de panica");
- lcd.setCursor(0, 1);
- lcd.print("apasat!");
- delay(2000);
- printCarCount();
- Serial.println("Buton de panica apasat!");
- }
- }
- }
- void fireEvent(int* temp) {
- while (true) {
- int x = digitalRead(SMOKEPIN);
- if (x == LOW) {
- fire = true;
- digitalWrite(LED, HIGH);
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Foc detectat!");
- delay(2000);
- printCarCount();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment