#include <MD_Parola.
h>
#include <MD_MAX72xx.h>
#include <SPI.h>
// Uncomment according to your hardware type
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
//#define HARDWARE_TYPE MD_MAX72XX::GENERIC_HW
// Defining size, and output pins
#define MAX_DEVICES 4
#define CS_PIN 10
#define CLK_PIN 13
#define DATA_PIN 11
#define BUF_SIZE 75
String SPEED = "";
char teststr[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);
#include <HCSR04.h>
// 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;
void setup()
{
// Intialize the object
myDisplay.begin();
// Set the intensity (brightness) of the display (0-15)
myDisplay.setIntensity(5);
// Clear the display
myDisplay.displayClear();
// 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);
}
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);
if((distance1<50)&&(distance2<50)){
Serial.print("Distance1 : ");
Serial.print(distance1);
Serial.print(" ");
Serial.print("Distance2 : ");
Serial.print(distance2);
Serial.print(" ");
Serial.print("Speed in Km/hr :");
Serial.print(speed);
Serial.println();
if(speed>25){
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
delay(1000);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
else{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
}
else{
Serial.print("Distance1 : ");
Serial.print(0);
Serial.print(" ");
Serial.print("Distance2 : ");
Serial.print(0);
Serial.print(" ");
Serial.print("Speed in Km/hr :");
Serial.print(0);
Serial.println();
}
if((distance1<50)&&(distance2<50)){
SPEED = String(speed, DEC) + "Km/hr";
SPEED.toCharArray(teststr, 75);
myDisplay.displayText(teststr, PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
myDisplay.displayAnimate();
delay(500);
}
}
// 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;
}