#include <LedControl.h>
LedControl lc = LedControl (11,13,10,3);
#define echoPin 3 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 2 //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,50); // 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 panel) {
int i = 0;
for (i = 0; i < 8; i++) {
lc.setRow(panel, i, character[i]);
}
}
void loop()
{
int panel; // which panel to us
//byte red[8]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
int value = analogRead(A0); // read of potentiometer value
int scale = map(value, 0, 1023, 0, 17); // 16 rows, plus one
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)
{
panel = 0;
int scale = map(distance, 0 , 400, 0, 8); // Mapping 0 to 400CM with 0 to 8 rows
for (int i=0;i<8;i++){
if (i<= scale){
dot[i] = 0xff;
}
else{
dot[i] = 0x00;
}
}
}*/
if (value > 512) { // if raw potentiometer is above middle, use panel 0, subtract 8 for row
panel = 0;
scale = scale - 8;
} else { // if raw potentiometer is below middle, use panel 1, subtract row from 8
panel = 1;
scale = 8 - scale;
}
for (int j = 0; j < scale; j++) {
dot[j] = 0xff;
}
printByte(dot, panel);
//printByte(red, panel);
delay(100);
}