#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
byte dot[8]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
void setup()
{
lc.shutdown(0,false); //sets up 8x8 matrix
lc.setIntensity(0,16); // 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 printByte(byte character [])
{
int i = 0;
for(i=0;i<8;i++)
{
lc.setRow(0,i,character[i]);
}
}
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)
//if (distance > 200)
int scale = map(distance, 0 , 400, 0, 8); // Mapping 0 to 400CM with 0 to 8 rows
if (distance > 200){
for (int i=0;i<8;i++){
if (i<= scale){
dot[i] = 0xff;
}
else{
dot[i] = 0x00;
}
}
}
printByte(dot);
delay(100);
}
/*//The following if statements convert the distance from the ultrasonic sensor into a variable for the matrix called "dm"
if(distance<=50 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<=100 and distance>=51){
dm=6;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=150 and distance>=101){
dm=5;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=200 and distance>=151){
dm=4;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=250 and distance>=201){
dm=3;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=300 and distance>=251){
dm=2;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=350 and distance>=301){
dm=1;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);
if(distance<=400 and distance>=351){
dm=0;
}
lc.setRow(0,dm,dot[7]);
delay(10);
lc.clearDisplay(0);*/
/*#include <Arduino.h>
#include <LedControl.h>
const int LED_DIN = 12;
const int LED_CLK = 11;
const int LED_CS = 10;
const int TRIG = 3;
const int ECHO = 2;
LedControl lc = LedControl(LED_DIN, LED_CLK, LED_CS, 1);
int duration = 0;
int distance = 0;
const int STEP = 400;
const int THRESHOLD = 400;
byte levels[8][8] = {{B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00000000, B00011000, B00011000, B00000000, B00000000, B00000000},
{B00000000, B00000000, B00011000, B00111100, B00111100, B00011000, B00000000, B00000000},
{B00000000, B00011000, B00111100, B01111110, B01111110, B00111100, B00011000, B00000000},
{B00011000, B00111100, B01111110, B11111111, B11111111, B01111110, B00111100, B00011000},
{B00111100, B01111110, B11111111, B11111111, B11111111, B11111111, B01111110, B00111100},
{B01111110, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B01111110},
{B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111}};
void setup() {
Serial.begin(9600);
lc.shutdown(0, false);
lc.setIntensity(0, 15);
lc.clearDisplay(0);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
delay(100);
}
void display(int distance) {
int level = min ((distance - THRESHOLD) / STEP, 7);
for (int i=0; i<8; i++) {
lc.setRow(0, i, levels[level][i]);
}
}
void loop() {
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
if (duration > 0) {
distance = duration * 340.29 / 2 / 1000;
display(distance);
}
Serial.println(duration);
Serial.println(distance);
delay(50);
}*/