/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:
Tutorial: Make a Car Speed Detector using Arduino!
Wire Library:
Adafruit_GFX Library:
Wire Library:
The purpose of this sketch is to detect the speed of the moving car using two IR sensor and display the speed of the car on a TFT Display.
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connections from the first IR sensor:
• IR sensor VCC pin → Breadboard 5V line
• IR sensor GND pin → Breadboard ground line
• IR sensor OUT pin → Arduino pin 2
Connections from the second IR sensor:
• IR sensor VCC pin → Breadboard 5V line
• IR sensor GND pin → Breadboard ground line
• IR sensor OUT pin → Arduino pin 3
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);
int timer1;
int timer2;
float Time;
int flag1 = 0;
int flag2 = 0;
float distance = 10.0;
float speed;
//Define IR sensor pin numbers
int ir_s1 = 3;
int ir_s2 = 2;
char car_speedChar[10];
void setup(){
//Define IR sensor pins as input
pinMode(ir_s1, INPUT);
pinMode(ir_s2, INPUT);
Serial.begin(9600);
tft.begin();
tft.setRotation(0);
tft.fillScreen(ILI9341_GREEN);
Wire.begin();
printText("CAR SPEED:", ILI9341_WHITE,20,130,3);
}
void loop()
{
if(digitalRead (ir_s1) == LOW && flag1==0){timer1 = millis(); flag1=1;}
if(digitalRead (ir_s2) == LOW && flag2==0){timer2 = millis(); flag2=1;}
if (flag1==1 && flag2==1){
if(timer1 > timer2){Time = timer1 - timer2;}
else if(timer2 > timer1){Time = timer2 - timer1;}
Time=Time/1000;//convert millisecond to second
speed=(distance/Time);//v=d/t
speed=speed*3600;//multiply by seconds per hr
speed=speed/1000;//division by meters per Km
}
if(speed==0){
if(flag1==0 && flag2==0){
tft.fillRect(20,250,200,20,ILI9341_RED);
printText("No car detected", ILI9341_GREEN,20,250,2);}
else{
tft.fillRect(20,250,200,20,ILI9341_RED);
printText("Searching...", ILI9341_GREEN,20,250,2);
}
}
else{
//Convert float to string
String car_speedString = String(speed,1);
car_speedString.toCharArray(car_speedChar,10);
//Draw a Rectangle
tft.fillRect(50,175,125,40,ILI9341_BLACK);
//printing the car speed value on the TFT Display
printText(car_speedChar, ILI9341_WHITE,50,180,4);
printText("Km/hr", ILI9341_WHITE,180,180,2);
if(speed > 50){
tft.fillRect(20,250,200,20,ILI9341_RED);
printText(" Over Speeding ", ILI9341_GREEN,20,250,2);}
else{
tft.fillRect(20,250,200,20,ILI9341_RED);
printText(" Normal Speed ", ILI9341_GREEN,20,250,2);}
delay(3000);
speed = 0;
flag1 = 0;
flag2 = 0;
}
}
//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);
}