Skip to content

Instantly share code, notes, and snippets.

@moshfeu
Last active March 17, 2018 20:46
Show Gist options
  • Save moshfeu/43f6a00118e487cff36b12db45f4578c to your computer and use it in GitHub Desktop.
Save moshfeu/43f6a00118e487cff36b12db45f4578c to your computer and use it in GitHub Desktop.
[Arduino] 7 segments display present the distance in meters

With this circuit you can present the distance (in meters) with a 7 segments display component.

Component Quantity
Anode 7 Segment Display 1
330 ohm Resistor 1
Ultrasonic Distance Sensor 1

7 segments display distance

https://www.tinkercad.com/things/bzFECN3pcfE

//bool zero[7] = {true, true, true, true, true, true, false};
//bool one[7] = {false, true, true, false, false, false, false};
//bool two[7] = {true, true, false, true, true, false, true};
//bool three[7] = {true, true, true, true, false, false, true};
//bool four[7] = {false, true, true, false, false, true, true};
//bool five[7] = {true, false, true, true, false, true, true};
//bool six[7] = {true, false, true, true, true, true, true};
//bool seven[7] = {true, true, true, false, false, false, false};
//bool eight[7] = {true, true, true, true, true, true, true};
//bool nine[7] = {true, true, true, false, false, true, true};
bool numbers[10][7] = {
{true, true, true, true, true, true, false},
{false, true, true, false, false, false, false},
{true, true, false, true, true, false, true},
{true, true, true, true, false, false, true},
{false, true, true, false, false, true, true},
{true, false, true, true, false, true, true},
{true, false, true, true, true, true, true},
{true, true, true, false, false, false, false},
{true, true, true, true, true, true, true},
{true, true, true, false, false, true, true}//
};
int distanceSensor = A0;
void setup() {
Serial.begin(9600);
pinMode(distanceSensor, INPUT);
for (int pin = 2 ; pin < 9 ; pin++){ // setup pins 2 – 8 to be output
pinMode(pin, OUTPUT);
}
}
void loop() {
int meters = cm(readUltrasonicDistance(distanceSensor)) / 100;
Serial.println(meters);
runNumber(numbers[meters]);
delay(100);
}
long cm(long dist) {
return 0.01723 * dist;
}
void runNumber(bool number[7]) {
for (int n = 0; n <= 7; n++) {
int pin = n + 2;
if (number[n]) {
digitalWrite(pin, LOW);
} else {
digitalWrite(pin, HIGH);
}
}
}
long readUltrasonicDistance(int pin)
{
pinMode(pin, OUTPUT); // Clear the trigger
digitalWrite(pin, LOW);
delayMicroseconds(2);
// Sets the pin on HIGH state for 10 micro seconds
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
// Reads the pin, and returns the sound wave travel time in microseconds
return pulseIn(pin, HIGH);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment