#include <MD_Parola.
h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <HCSR04.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
// Create a new instance of the MD_Parola class with hardware SPI connection
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Declaration of trigger and echo pins
int trigPin = A1;
int echoPin = A0;
// declaration of variables going to be used for the program
int distance1 = 0;
int distance2 = 0;
int measured_speed = 0, speed = 0;
long duration = 0;
int distance = 0;
char SPEED[100];
void setup()
{ // Setting trigger pin as OUTPUT
pinMode(trigPin, OUTPUT);
// Setting echoPin as INPUT
pinMode(echoPin, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
// Starts the serial communication at baud rate 9600
Serial.begin(9600);
myDisplay.begin();
myDisplay.setIntensity(0);
myDisplay.displayClear();
myDisplay.setTextAlignment(PA_CENTER);
void loop()
{ // Measuring distance 1
distance1 = ultrasonicRead(); // calls ultrasoninicRead()
delay(1000); // gives delay of 1 second
distance2 = ultrasonicRead(); // calls ultrasoninicRead() function below
// Formula to calculate speed from distance1 and distance2
// We are dividing it by 1, since the time interval between the two distance
measurement is 1000 ms or 1 second
measured_speed = (distance2 - distance1) / 1.0;
// Displaying the speed value on the serial monitor
speed = abs((18/5)*measured_speed);
String temp = String(speed) + "Km/h";
temp.toCharArray(SPEED, 100);
if((distance1<50)&&(distance2<50)){
//Serial.print("Speed in Km/hr :");
//Serial.print(speed);
//Serial.println();
myDisplay.print(SPEED);
myDisplay.displayReset();
if(speed>70){
myDisplay.print(SPEED);
myDisplay.displayReset();
delay(1000);
myDisplay.print("OVER SPEED");
myDisplay.displayReset();
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(1000);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
else{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
}
else{
//Serial.print("Speed in Km/hr :");
//Serial.print(0);
//Serial.println();
myDisplay.print("0 Km/h");
myDisplay.displayReset();
}
// Function declaration to measure the distance based on the working principle of
ultrasonic sensor
float ultrasonicRead()
{ // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Waits for the amount of time echoPin remains high and records the duration
of the same
duration = pulseIn(echoPin, HIGH);
// Calculates the distance based on the speed of sound in ambient air
// and divide it by two since the sound traveled twice - once to the object and
then back
distance = (duration * 0.034)/ 2;
// returning measured distance
return distance;
}