#include "Arduino.h"
#include <Wire.h>
#define BLYNK_TEMPLATE_ID "TMPL6KotIH9Cj"
#define BLYNK_TEMPLATE_NAME "wk10"
#define BLYNK_AUTH_TOKEN "HW-wLZyBX-qZhAqqzgj6sWjL5-RxW2-0"
#define trigPin 11
#define echoPin 12
#define trig_Pin 7
#define echo_Pin 8
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <DHT.h>// include adafruit library unified sensor
#define DHTPIN A0 //
#define DHTTYPE DHT22
const int CO2Pin = A0;
DHT dht = DHT(DHTPIN, DHTTYPE);
BlynkTimer timer;
char ssid[] = "DGEM1234";
char pass[] = "dgem4321";
#define BLYNK_PRINT Serial
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int counter = 0;
int counter2 = 0;
int currentState = 0;
int previousState = 0;
float readcounter();
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trig_Pin, OUTPUT);
pinMode(echo_Pin, INPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600); // baude rate is 9600
dht.begin();
Blynk.virtualWrite(V0, t);
Blynk.virtualWrite(V1, h);
Serial.println("Connection complete");
delay(1000);
}
void setup()
{
Serial.begin(115200);
dht.begin(); // Setup sensor:
timer.setInterval (1000L,sensor);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("Setup starting...");
Serial.println("Set UP Complete");
pinMode(ledPin, OUTPUT);
}
void loop()
{
float readCO =analogRead (COPin) ;
Serial.println(readCO);
delay (1000);
lcd.backlight(); // Turn on the LCD backlight
lcd.setCursor(0, 0);
lcd.print( "Number of people: ");
lcd.print(counter);
lcd.print(" Person");
long duration, distance;
long duration2, distance2;
//for sensor1
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//for sensor2
digitalWrite(trig_Pin, LOW);
delayMicroseconds(2);
digitalWrite(trig_Pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_Pin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
duration2 = pulseIn(echo_Pin, HIGH);
distance2 = (duration/2) / 29.1;
//instead of parenthesis () put angle bracket as YouTube description does not allow angle bracket
if (distance <= 10){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
Serial.println(counter);
}
}
if (distance2 <= 10){
counter2 = counter - 1;
}
else {
currentState = previousState;
}
delay(100);
Serial.println(counter2);
delay(2000); // Wait a few seconds between measurements:
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old'
float h = dht.readHumidity();// Read the humidity in %:
float t = dht.readTemperature(); // Read the temperature as Celsius:
Blynk.run();
timer.run();
}