safwan092

Bus Project New Code #2

Apr 12th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. // IR Obstacle Collision Detection Module - Henry's Bench
  2. // Bus People Counter - Dawaerstore.com
  3. #include <LiquidCrystal_I2C.h>
  4. #include <Wire.h>
  5. LiquidCrystal_I2C lcd(0x3F, 20, 4);
  6.  
  7. int buzzerPin = 9;
  8. int isObstaclePin = 2;
  9. int isObstaclePin3 = 5;
  10.  
  11. int isObstacle = HIGH;
  12. int isObstacle3 = HIGH;
  13.  
  14. int a = 0;
  15. int b = 0;
  16. int c = 0;
  17. int d = 0;
  18. int count = 0;
  19.  
  20. void setup() {
  21.  
  22.   pinMode(isObstaclePin, INPUT);
  23.   pinMode(isObstaclePin3, INPUT);
  24.   pinMode(buzzerPin, OUTPUT);
  25.   digitalWrite(buzzerPin, 1);
  26.   Serial.begin(9600);
  27.   Wire.begin();
  28.   lcd.begin();
  29.   lcd.home();
  30.   lcd.setCursor(0, 0);
  31.   lcd.print(" Scanning...    ");
  32.  
  33. }
  34.  
  35. void loop() {
  36.  
  37.   isObstacle = digitalRead(isObstaclePin);
  38.   isObstacle3 = digitalRead(isObstaclePin3);
  39.  
  40.   if (isObstacle == HIGH && isObstacle3 == LOW && a == 0)
  41.   {
  42.     a = 1;
  43.   }
  44.   else if (isObstacle == LOW && isObstacle3 == HIGH && a == 1)
  45.   {
  46.     b = 1;
  47.   }
  48.   else if (isObstacle == LOW && isObstacle3 == HIGH && a == 0)
  49.   {
  50.     c = 1;
  51.   }
  52.   else if (isObstacle == HIGH && isObstacle3 == LOW && c == 1)
  53.   {
  54.     d = 1;
  55.   }
  56.  
  57.   calc();
  58.  
  59.  
  60. }
  61.  
  62.  
  63. void calc() {
  64.   if (b == 1) {
  65.     lcd.setCursor(0, 0);
  66.     lcd.print(" Count is:    ");
  67.     count = count + 1;
  68.     digitalWrite(buzzerPin, 1);
  69.     a = 0;
  70.     b = 0;
  71.     c = 0;
  72.     d = 0;
  73.     Serial.println(count);
  74.     lcd.setCursor(0, 1);
  75.     lcd.print("                ");
  76.     lcd.setCursor(1, 1);
  77.     lcd.print(count);
  78.     delay(700);
  79.   }
  80.   else if (d == 1) {
  81.     lcd.setCursor(0, 0);
  82.     lcd.print(" Count is:    ");
  83.     if (count == 2) {
  84.       digitalWrite(buzzerPin, 0);
  85.     }
  86.     else {
  87.       digitalWrite(buzzerPin, 1);
  88.     }
  89.     count = count - 1;
  90.     if (count < 0) {
  91.       count = 0;
  92.     }
  93.     a = 0;
  94.     b = 0;
  95.     c = 0;
  96.     d = 0;
  97.     Serial.println(count);
  98.     lcd.setCursor(0, 1);
  99.     lcd.print("                ");
  100.     lcd.setCursor(1, 1);
  101.     lcd.print(count);
  102.     delay(700);
  103.  
  104.   }
  105. }
Add Comment
Please, Sign In to add comment