/*
Arduino | hardware-help
Tyby Allx 9/9/25 — 11:25 AM
Hello, I tried replicating this project
https://youtu.be/HkyMUBDLYUo?si=zxNtcyAFVbEg3SvO
Unfortunately, when more than 1 row of leds is opened,
the matrix gets a flicker effect, it's not that bad
but I would like to know if there is anything specific
in the code that makes it act this way or it's caused
by the led matrix
*/
#include "LedControl.h"
const int DATA_PIN = 2;
const int LOAD_PIN = 3;
const int CLOCK_PIN = 4;
const int ECHO_PIN = 11;
const int TRIG_PIN = 12;
LedControl lc = LedControl(DATA_PIN, CLOCK_PIN, LOAD_PIN, 1);
int getDistance() {
// send trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// calculate distance
float duration = pulseIn(ECHO_PIN, HIGH);
float distance = (duration * .0343) / 2;
// return distance as an integer
return (int)(distance + 0.5);
}
void setup() {
Serial.begin(115200);
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
// initialize MAX72xx
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
}
void loop() {
int val = 0;
int dist = getDistance();
int mapDist = map(dist, 0, 400, 7, 0);
for (int i = 0; i < 8; i++) {
if (mapDist < i) {
lc.setColumn(0, i, 0x0);
} else {
switch (i) {
case 0: val = 0x18; break;
case 1: val = 0x18; break;
case 2: val = 0x3C; break;
case 3: val = 0x3C; break;
case 4: val = 0x7E; break;
case 5: val = 0x7E; break;
case 6: val = 0xFF; break;
case 7: val = 0xFF; break;
default: break;
}
lc.setColumn(0, i, val);
}
}
Serial.print("Distance: ");
Serial.println(dist);
delay(100);
}