#include <LedControl.h>
const int DIN_PIN = 11;
const int CS_PIN = 6;
const int CLK_PIN = 13;
// Ultrasonic Sensor Pins
const int ECHO_PIN = 3;
const int TRIG_PIN = 4;
// Max distance to measure
const int MAX_DISTANCE = 120;
// Image definitions (emojis) for different ranges
const uint64_t IMAGES[] = {
0x00427e7e18666600, // Image 1
0x24187e3c18181800, // Image 2
0x003c420000666600, // Image 3
0xcce673399cce6733, // Image 4
0x00002472994e2400, // Image 5
0x1818001830647e3c, // Image 6
0x3232323232323232, // Image 7
0x38380038387c7c7c, // Image 8
0x00c3ffdb3c7e7e18, // Image 9
0x003c0066ffff0000, // Image 10
0x2466ff3c1818183c, // Image 11
0x0408183e0c183000, // Image 12
0x0024187e5ae70000 // Image 13
};
const int IMAGES_LEN = sizeof(IMAGES) / 8;
LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN);
void setup() {
// Initialize the LED display
display.clearDisplay(0);
display.shutdown(0, false);
display.setIntensity(0, 10);
// Initialize the ultrasonic sensor
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void displayImage(uint64_t image) {
// Display the image with correct orientation
for (int i = 0; i < 8; i++) {
byte row = (image >> (i * 8)) & 0xFF;
for (int j = 0; j < 8; j++) {
display.setLed(0, i, 7 - j, bitRead(row, j)); // Mirroring adjustment
}
}
}
int getDistance() {
// Send a pulse to the ultrasonic sensor
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure the time it takes for the echo to return
long duration = pulseIn(ECHO_PIN, HIGH);
// Convert duration to distance in cm
int distance = duration * 0.034 / 2;
// Ensure the distance is within bounds
if (distance > MAX_DISTANCE) {
distance = MAX_DISTANCE;
}
return distance;
}
void loop() {
int distance = getDistance();
// Choose the emoji based on distance ranges
int imageIndex = map(distance, 0, MAX_DISTANCE, 0, IMAGES_LEN - 1);
displayImage(IMAGES[imageIndex]);
delay(200);
}