/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:
Tutorial: Make a Bi-Directional Visitor Counter with Arduino!
Wire Library:
Adafruit_GFX Library:
Wire Library:
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connections from the first ultrasonic sensor:
• ultrasonic sensor VCC pin → Breadboard 5V line
• ultrasonic sensor GND pin → Breadboard ground line
• ultrasonic sensor Trig pin → Arduino pin 3
• ultrasonic sensor Echo pin → Arduino pin 2
Connections from the second ultrasonic sensor:
• ultrasonic sensor VCC pin → Breadboard 5V line
• ultrasonic sensor GND pin → Breadboard ground line
• ultrasonic sensor Trig pin → Arduino pin 4
• ultrasonic sensor Echo pin → Arduino pin 5
Connections from the TFT Display to the breadboard:
• TFT Display VCC pin → Breadboard 5V line
• TFT Display GND pin → Breadboard ground line
• TFT Display LED pin → Breadboard 5V line
Connections from the TFT Display to arduino :
• TFT Display CS pin → Arduino pin 10
• TFT Display RST pin → Arduino pin 8
• TFT Display DC pin → Arduino pin 9
• TFT Display MOSI pin → Arduino pin 11
• TFT Display SCK pin → Arduino pin 13
• TFT Display MISO pin → Arduino pin 12
*/
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Wire.h"
#include "SPI.h"
//Define pin numbers
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_CLK 13
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
char visitorsChar[10];
#define trigPin 3
#define echoPin 2
#define trigPin2 4
#define echoPin2 5
float visitors;
int i = 0;
int currentState = 0;
int previousState = 0;
int currentState2 = 0;
int previousState2 = 0;
void setup(){
//Sets the trig Pin as an output
pinMode(trigPin, OUTPUT);
//Sets the echo Pin as an input
pinMode(echoPin, INPUT);
//Sets the trig Pin as an output
pinMode(trigPin2, OUTPUT);
//Sets the echo Pin as an input
pinMode(echoPin2, INPUT);
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_GREEN);
Wire.begin();
printText("number of visitors ", ILI9341_BLACK,10,130,2);
visitors=i;
//Convert float to string
String visitorsString = String(visitors,1);
visitorsString.toCharArray(visitorsChar,10);
//Draw a Rectangle
tft.fillRect(50,175,150,40,ILI9341_BLACK);
//printing the number of visitors value on the TFT Display
printText(visitorsChar, ILI9341_WHITE,50,180,4);
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
long duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
// object entering in the system
if (distance <= 10){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
while(currentState == 1) {
long duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
if (distance2 <= 10){
currentState2 = 1;
}
else {
currentState2 = 0;
}
delay(100);
if(currentState2 != previousState2){
if(currentState2 == 1) {
i = i+1;
visitors=i;
//Convert float to string
String visitorsString = String(visitors,1);
visitorsString.toCharArray(visitorsChar,10);
//Draw a Rectangle
tft.fillRect(50,175,150,40,ILI9341_BLACK);
//printing the number of visitors value on the TFT Display
printText(visitorsChar, ILI9341_WHITE,50,180,4);
delay(1000); // pause for 1/2 second
}
else {
visitors=i;
//Convert float to string
String visitorsString = String(visitors,1);
visitorsString.toCharArray(visitorsChar,10);
//Draw a Rectangle
tft.fillRect(50,175,150,40,ILI9341_BLACK);
//printing the number of visitors value on the TFT Display
printText(visitorsChar, ILI9341_WHITE,50,180,4);
}
return;
}
}
}
// object exit from the system
if (distance2 <= 10){
currentState2 = 1;
}
else {
currentState2 = 0;
}
delay(100);
if(currentState2 != previousState2){
while (currentState2 == 1) {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance <= 10){
currentState = 1;
}
else {
currentState = 0;
}
delay(100);
if(currentState != previousState){
if(currentState == 1) {
i = i-1;
if (i<0){
i=0;
}
visitors=i;
//Convert float to string
String visitorsString = String(visitors,1);
visitorsString.toCharArray(visitorsChar,10);
//Draw a Rectangle
tft.fillRect(50,175,150,40,ILI9341_BLACK);
//printing the number of visitors value on the TFT Display
printText(visitorsChar, ILI9341_WHITE,50,180,4);
delay(1000); // pause for 1/2 second
}
else {
visitors=i;
//Convert float to string
String visitorsString = String(visitors,1);
visitorsString.toCharArray(visitorsChar,10);
//Draw a Rectangle
tft.fillRect(50,175,150,40,ILI9341_BLACK);
//printing the number of visitors value on the TFT Display
printText(visitorsChar, ILI9341_WHITE,50,180,4);
}
return;
}
}
}
//delay(100);
}
//The function of writing words on the TFT Display
void printText(char *text, uint16_t color, int x, int y,int textSize)
{
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(textSize);
tft.setTextWrap(true);
tft.print(text);
}