// Including the required Arduino libraries for MAX7219 Display and Ultrasonic sensor #include #include #include #include //Ultrasonic sensor pin settings #define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 6 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 1000 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. #define HARDWARE_TYPE MD_MAX72XX::FC16_HW // Defining size, and output pins #define MAX_DEVICES 4 #define CS_PIN 3 // Create a new instance of the MD_Parola class with hardware SPI connection MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES); void setup() { Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results. // Intialize the object myDisplay.begin(); // Set the intensity (brightness) of the display (0-15) myDisplay.setIntensity(1); // Clear the display myDisplay.displayClear(); } void loop() { myDisplay.setTextAlignment(PA_CENTER); Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range) Serial.println("cm"); // check serial monitor if(sonar.ping_cm() < 5 && sonar.ping_cm() > 0) // condition to tell if the object is too close. { myDisplay.print("STOP"); } else{ myDisplay.print(""); } delay(50); }