#include <LedControl.h>
int echoPin = 2;
int trigPin = 3;
int duration;
int distance;
int DIN = 13;
int CS1 = 11;
int CS2 = 10;
int CLK = 12;
LedControl lc1 = LedControl(DIN, CLK, CS1, 1);
LedControl lc2 = LedControl(DIN, CLK, CS2, 2);
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
lc1.shutdown(0, false);
lc2.shutdown(0, false);
lc1.setIntensity(0, 50);
lc2.setIntensity(0, 50);
lc1.clearDisplay(0);
lc2.clearDisplay(0);
}
void printByte1(byte character []) {
int i = 0;
for (i = 0; i < 8; i++) {
lc1.setRow(0, i, character[i]);
}
}
void printByte2(byte character [], int panel) {
int i = 0;
for (i = 0; i < 8; i++) {
lc2.setRow(panel, i, character[i]);
}
}
void loop() {
int panel; // which panel to use
byte cero1[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00};
byte cero2[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
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
for (int i=0;i<8;i++){
if (distance >= 200){
if (i<= scale){
cero1[i] = 0xff;
}
else{
cero1[i] = 0x00;
}
}
}
}
int value = analogRead(A0); // to read potentiometer value
int scale = map(value, 0, 1023, 0, 17);
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++) {
cero2[j] = 0xff;
}
printByte2(cero2, panel);
delay(100);
printByte1(cero1);
delay(100);
}