#include "LedControl.h"
#include "binary.h"
/* DIN connects to pin 12 CLK connects to pin 11 CS connects to pin 10 */
LedControl lc=LedControl(12,11,10,1);
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04 // defines variables
int duration; // variable for the duration of sound wave travel
int distance;//variable for the distance measurement
int dm; // variable for what row to light up on the matrix
// Binary for the bar
byte dot[8]= {B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B00000000,B11111111};
void setup() {
lc.shutdown(0,false); //sets up 8x8 matrix
lc.setIntensity(0,100000); // Set brightness to a medium value
lc.clearDisplay(0); // Clear the display
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
}
void loop(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); //Creates a ten microsecond delay
digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
//The following if statements convert the distance from the ultrasonic sensor into a variable for the matrix called "dm"
if(distance<=3 and distance>=0){
dm=7;
}
//This section of code lights up the LEDs on the matrix
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=5 and distance>=4){
dm=6;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=7 and distance>=6){
dm=5;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=9 and distance>=8){
dm=4;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=11 and distance>=10){
dm=3;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=13 and distance>=12){
dm=2;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=15 and distance>=14){
dm=1;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=19 and distance>=16){
dm=0;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
}