Personen im Raum zählen – Teil2

Mit einem Elegoo Mega2560 R3, an dem ein Infrarot Sensor Sharp GP2Y0A02Y angeschlossen ist, um vor den betreten eines Raumes zu erfahren, ob der Raum schon mit der maximale Anzahl an Personen gefüllt ist.

Entwicklungsboard = Elegoo Mega2560 R3
Alternativ = Arduino Uno
Infrarot Senor = Sharp GP2Y0A02Y
LCD-Display = LCD Board Module 16×2 HD44780
2 LEDs (Rot und Grün)

Infrarot Sensoren:
Reichweite 20 cm bis 150 cm
Datenblatt: https://global.sharp/products/
Alternative könnten Laser mit Reflektoren oder UltraSchall Sensoren angebracht werden.

Erweiterbarkeit: Anstatt oder zusätzlich zu den LEDs kann auch über ein Relay angeschlossene Schranke gesteuert werden

Arduino IDE = https://www.arduino.cc/en/software

Quellcode:

// prilchen.de
#include<LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#include <SharpIR.h> //quelle https://github.com/qub1750ul/Arduino_SharpIR
#define gruen 2 // LED gruen
#define rot 5 // LED rot
boolean IR1active = false;
boolean IR2active = false;
int count = 0;
unsigned int zustand = 3;

SharpIR SharpIR1( SharpIR::GP2Y0A02YK0F, A1 );// sharp 1
SharpIR SharpIR2( SharpIR::GP2Y0A02YK0F, A2 );// sharp 2

void setup()
{
  pinMode(2, OUTPUT);//LED gruen
  pinMode(5, OUTPUT);//LED rot

  lcd.begin(16, 2);
  lcd.print(" Starte den ");
  lcd.setCursor(0, 1);
  lcd.print("Besucherzaehler");
  delay(2000);
  Serial.begin(9600);
  lcd.clear();
  lcd.print("Person In Raum:");
  lcd.setCursor(0, 1);
  lcd.print(count);
  digitalWrite(gruen, HIGH);
}

void loop()
{
  delay(200);
  lcd.clear();
  lcd.print("Person In Raum:");
  lcd.setCursor(0, 1);
  lcd.print(count);
  Serial.print("Zähler: ");
  Serial.println(count);
  IR1active = false;
  IR2active = false;
  //unsigned long startTime=millis();
  int dis1 = SharpIR1.getDistance();
  if (dis1 < 25) {
    IR1active = true;
  }
  delay(10);
  int dis2 = SharpIR2.getDistance();
  if (dis2 < 25) {
    IR2active = true;
  }

  Serial.print("Distance (1): ");
  Serial.print(dis1);
  Serial.println("cm");

  Serial.print("Distance (2): ");
  Serial.print(dis2);
  Serial.println("cm");

  // Mögliche 9 Zustände:
  //
  // 1 Standard ir1 und ir2 beide gleichzeitig still (Zustandsname "Still")
  // 2 Fehler ir1 und ir2 beide gleichzeitig aktiv (Zustandsname "Fehler")
  //
  // Ablauf Person geht rein:
  // 3 zuerst ir1 "rein" aktiv + ir2 still (Zustandsname "ReinA")
  // 4 zuerst ir1 "rein" aktiv + ir2 dann erst aktiv (Zustandsname "ReinB") oder direkt zu "ReinC"
  // 5 ir1 wieder still + ir2 aktiv (Zustandsname "ReinC")
  // 5.1 beide ir1 und ir2 wieder still (Zustandsname "Still")
  // 6 Sonderfall nach "ReinA" beide ir1 und ir2 wieder still (Zustandsname "FalschAlarm")
  //
  // Ablauf Person geht raus:
  // 7 zuerst ir2 "raus" aktiv + ir1 still (Zustandsname "RausA")
  // 8 zuerst ir2 "raus" aktiv + ir1 dann erst aktiv (Zustandsname "RausB")
  // 9 ir2 wieder still + ir1 aktiv (Zustandsname "RausC")
  // 9.1 beide ir2 und ir1 wieder still (Zustandsname "Still")
  // 6 Sonderfall nach "RausA" beide ir1 und ir2 wieder still (Zustandsname "FalschAlarm")


  if (IR1active == false && IR2active == false) {
    Serial.println("ir1 und 2 false");
    zustand = 1; //zuerst ir1 "rein" aktiv + ir2 still
  }
  if (IR1active == true && IR2active == false) {
    Serial.println("ir1 true und 2 false");
    zustand = 3; //zuerst ir1 "rein" aktiv + ir2 still
  }
  if (IR2active == true && IR1active == false) {
    Serial.println("ir2 true und 1 false");
    zustand = 7; //zuerst ir2 "raus" aktiv + ir1 still
  }
  if (IR2active == true && IR1active == true) {
    Serial.println("ir1 true und 2 true");
    zustand = 2; //zuerst ir1 "rein" aktiv + ir2 "raus" aktiv
  }
  Serial.println(zustand);
  switch (zustand) {
    case 1:
      Serial.println("Zustand: 1 - nichts unter 25 cm");
      break;
    case 7:
      Serial.println("Zustand: 7 - IR2 unter 25 cm");
      delay (500);
      IR1active = false;
      IR2active = false;
      dis1 = SharpIR1.getDistance();
      if (dis1 < 25) {
        IR1active = true;
      }
      delay(10);
      dis2 = SharpIR2.getDistance();
      if (dis2 < 25) {
        IR2active = true;
      }
      if (IR1active == false && IR2active == false) {
        Serial.println("ir1 und 2 false sehr still");
        zustand = 1; //jetzt aber wieder beide still
      }

      if (IR2active == true && IR1active == false) {
        Serial.println("ir2 immer noch true und 1 false");
        zustand = 7; //ir2 "raus" aktiv aber nichts weiter - neue Runde (ohne zählung)
      }
      if (IR2active == true && IR1active == true) {
        Serial.println("ir1 true und 2 true");
        zustand = 8; //ir1 "rein" aktiv + ir2 "raus" aktiv -  OK zählung minus
      }
      if (IR1active == true && IR2active == false) {
        Serial.println("ir1 true und 2 false");
        zustand = 9; //zuerst ir2 "raus" jetzt still und weiter zu ir1 -  OK zählung minus
      }

      switch (zustand) {
        case 1:
          Serial.println("Zustand: 1 - nichts unter 25 cm (fehlalarm?");
          break;

        case 7:
          Serial.println("Immer noch Zustand: 7 - neue Runde (ohne zählung)");
          IR1active = false;

          break;
        case 8:
          count--;
          Serial.print("Zähler: ");
          Serial.println(count);
          lcd.clear();
          lcd.print("Person In Raum:");
          lcd.setCursor(0, 1);
          lcd.print(count);
          IR1active = false;
          IR2active = false;
          delay(500);
          break;
        case 9:
          count--;
          Serial.print("Zähler: ");
          Serial.println(count);
          lcd.clear();
          lcd.print("Person In Raum:");
          lcd.setCursor(0, 1);
          lcd.print(count);
          IR1active = false;
          IR2active = false;
          delay(500);
          break;
      }
      break;
    case 3:
      Serial.println("Zustand: 3 - IR1 unter 25 cm");
      delay (500);
      IR1active = false;
      IR2active = false;
      int dis1 = SharpIR1.getDistance();
      if (dis1 < 25) {
        IR1active = true;
      }
      delay(10);
      int dis2 = SharpIR2.getDistance();
      if (dis2 < 25) {
        IR2active = true;
      }
      if (IR1active == false && IR2active == false) {
        Serial.println("ir1 und 2 false");
        zustand = 1; //jetzt aber wieder beide still
      }
      if (IR1active == true && IR2active == false) {
        Serial.println("ir1 true und 2 false");
        zustand = 3; //zuerst ir1 "rein" aktiv + aber immer noch ir2 still
      }
      if (IR2active == true && IR1active == false) {
        Serial.println("ir2 true und 1 false");
        zustand = 5; //zuerst ir2 "raus" aktiv + ir1 still - OK zählung plus 1
      }
      if (IR2active == true && IR1active == true) {
        Serial.println("ir1 true und 2 true");
        zustand = 4; //ir1 "rein" aktiv + ir2 "raus" aktiv - OK zählung plus 1
      }
      switch (zustand) {
        case 1:
          Serial.println("Zustand: 1 - nichts unter 25 cm (fehlalarm?");

          break;
        case 3:
          Serial.println("Immer noch Zustand: 3 - neue Runde (ohne zählung)");
          IR1active = false;

          break;
        case 4:
          count++;
          Serial.print("Zähler: ");
          Serial.println(count);
          lcd.clear();
          lcd.print("Person In Raum:");
          lcd.setCursor(0, 1);
          lcd.print(count);
          IR1active = false;
          IR2active = false;
          delay(500);
          break;
        case 5:
          count++;
          Serial.print("Zähler: ");
          Serial.println(count);
          lcd.clear();
          lcd.print("Person In Raum:");
          lcd.setCursor(0, 1);
          lcd.print(count);
          IR1active = false;
          IR2active = false;
          delay(500);
          break;
      }
      break;
    case 2:
      Serial.println("Zustand: 2 - ACHTUNG 2 Sensoren aktiv");
      lcd.clear();
      lcd.print("Achtung");
      lcd.setCursor(0, 1);
      lcd.print("2 Sensoren aktiv");
      IR1active = false;
      IR2active = false;
      delay (1000);
      break;
  }

  if (count > 5)
  {
    lcd.clear();
    digitalWrite(gruen, LOW);
    digitalWrite(rot, HIGH);
    lcd.clear();
    lcd.print("Raum ist voll");
    lcd.setCursor(0, 1);
    lcd.print("KEIN ZUTRITT");
    delay(200);
  }

  else
    digitalWrite(gruen, HIGH);
  digitalWrite(rot, LOW);

  if (count < 0)
  {
    count = 0;
    lcd.clear();
    lcd.print("Fehler");
    lcd.setCursor(0, 1);
    lcd.print("Wert im Minus");
    delay(1000);
  }
}
, , ,