0% found this document useful (0 votes)
10 views3 pages

Segment Pins

The document contains an Arduino code for measuring distance using an HC-SR04 ultrasonic sensor and displaying the result on a 7-segment display. It defines pin configurations for segment and digit pins, initializes them in the setup function, and continuously measures distance in the loop function. The measured distance is then processed and displayed by controlling the segments of the display accordingly.

Uploaded by

jofanchileshe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Segment Pins

The document contains an Arduino code for measuring distance using an HC-SR04 ultrasonic sensor and displaying the result on a 7-segment display. It defines pin configurations for segment and digit pins, initializes them in the setup function, and continuously measures distance in the loop function. The measured distance is then processed and displayed by controlling the segments of the display accordingly.

Uploaded by

jofanchileshe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Segment pins (a, b, c, d, e, f, g, dp)

const int segmentPins[8] = {2, 3, 4, 5, 6, 7, A0}; // skip dp for now

// Digit pins (DIG1 to DIG4)

const int digitPins[4] = {A1, A2, A3, A4};

// HC-SR04 pins

const int trigPin = 9;

const int echoPin = 8;

// Digit patterns (common cathode)

const byte digits[10] = {

// a b c d e f g

B00111111, // 0

B00000110, // 1

B01011011, // 2

B01001111, // 3

B01100110, // 4

B01101101, // 5

B01111101, // 6

B00000111, // 7

B01111111, // 8

B01101111 // 9

};
void setup() {

for (int i = 0; i < 7; i++) pinMode(segmentPins[i], OUTPUT);

for (int i = 0; i < 4; i++) pinMode(digitPins[i], OUTPUT);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

void loop() {

long duration;

int distance;

// Measure distance

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration * 0.034 / 2; // in cm

// Display distance

displayNumber(distance);

}
void displayNumber(int num) {

int d[4] = {0, 0, 0, 0};

d[0] = (num / 1000) % 10;

d[1] = (num / 100) % 10;

d[2] = (num / 10) % 10;

d[3] = num % 10;

for (int i = 0; i < 4; i++) {

setSegments(digits[d[i]]);

digitalWrite(digitPins[i], LOW); // turn on digit

delay(5);

digitalWrite(digitPins[i], HIGH); // turn off digit

void setSegments(byte val) {

for (int i = 0; i < 7; i++) {

digitalWrite(segmentPins[i], (val >> i) & 0x01);

You might also like