// HC-SR04 Ultrasonic Distance Sensor with 7 Segment Display
#include <SevSeg.h>
#define TRIG_PIN A3
#define ECHO_PIN A4
SevSeg sevseg;
uint16_t duration = 0;
uint32_t interval = 0;
float distance = 0;
void setup()
{
uint8_t numDigits = 4;
uint8_t digitPins[] = {2, 3, 4, 5};
uint8_t segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
uint8_t displayType = COMMON_ANODE; // (Common Anode or Common Cathode)
bool resistorsOnSegments = false;
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = false;
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
sevseg.begin(displayType, numDigits, digitPins, segmentPins, resistorsOnSegments,
updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90);
}
void loop()
{
if ((millis() - interval) >= 100) {
interval = millis();
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(5);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read time of the trig and echo pins
duration = pulseIn(ECHO_PIN, HIGH);
// Calculates the distance
distance = (duration / 2) / 29;
sevseg.setNumber(distance);
}
sevseg.refreshDisplay();
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
sonar:VCC
sonar:TRIG
sonar:ECHO
sonar:GND
display:A
display:B
display:C
display:D
display:E
display:F
display:G
display:DP
display:DIG1
display:DIG2
display:DIG3
display:DIG4
display:COM
display:CLN