Arduino quad 8 segment display, mesure distance in Feet and Inches - Page 1
Forum section: electronics / beginners

author: Jackster
author_rank: Frequent Contributor
timestamp: December 29, 2016, 03:31:27 pm
content: Hi all.I have a small Arduino project I am working on. I am not that good with programming so wondering if anyone here could help.What I have is a sonar taking measurements and outputting that in CM. I have converted it to Inches.But on the quad 8 segment display I would like to show on the first two sets of segments the Feet and on the second two sets the inches.So if the sonar measures 30 inches, the quad segments would show " 2:06".The quad segment display is ran using a TM1637http://playground.arduino.cc/Main/TM1637Here is the code I have mashed together;Code:#include "TM1637.h"/* Macro Define */#define CLK 2 /* 4-digital display clock pin */#define DIO 3 /* 4-digital display data pin *//* Global Variables */TM1637 tm1637(CLK, DIO); /* 4-digital display object */const int trigPin = 5;const int echoPin = 4;void setup() {// initialize serial communication:Serial.begin(9600);/* Initialize 4-digital display */tm1637.init();tm1637.set(BRIGHTEST);}void loop(){// establish variables for duration of the ping,// and the distance result in inches and centimeters:long duration, inches, cm, distance;int8_t bits = {0}; /* array to store the single bits of the value */// The sensor is triggered by a HIGH pulse of 10 or more microseconds.// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:pinMode(trigPin, OUTPUT);digitalWrite(trigPin, LOW);delayMicroseconds(2);digitalWrite(trigPin, HIGH);delayMicroseconds(10);digitalWrite(trigPin, LOW);// Read the signal from the sensor: a HIGH pulse whose// duration is the time (in microseconds) from the sending// of the ping to the reception of its echo off of an object.pinMode(echoPin, INPUT);duration = pulseIn(echoPin, HIGH);// convert the time into a distanceinches = microsecondsToInches(duration);cm = microsecondsToCentimeters(duration);Serial.print(inches);Serial.print("in, ");Serial.print(cm);Serial.print("cm");Serial.println();distance = microsecondsToInches(duration); /* read the value from the sensor */memset(bits, 0, 4); /* reset array when we use it */for(int i = 3; i >= 0; i--) {/* get single bits of the analog value */bits = distance % 10;distance = distance / 10;tm1637.display(i, bits); /* display by 4-digital display */}delay(10);}long microsecondsToInches(long microseconds){// According to Parallax's datasheet for the PING))), there are// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per// second). This gives the distance travelled by the ping, outbound// and return, so we divide by 2 to get the distance of the obstacle.// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdfreturn microseconds / 74 / 2;}long microsecondsToCentimeters(long microseconds){// The speed of sound is 340 m/s or 29 microseconds per centimeter.// The ping travels out and back, so to find the distance of the// object we take half of the distance travelled.return microseconds / 29 / 2;}The library I am using for the display is the DigitalTube libraries by Frankie.Chuhttps://github.com/reeedstudio/libraries/tree/master/DigitalTube

author: elecman14
author_rank: Unknown
timestamp: December 29, 2016, 03:35:02 pm
content: Can you explain what problem you encountered? Also a schematic with part numbers would be helpful.

author: Jackster
author_rank: Frequent Contributor
timestamp: December 29, 2016, 03:42:46 pm
content: Quote from: elecman14 on December 29, 2016, 03:35:02 pmI am just unable to work out how to display the measurement in a certain way.As for schematic, I only have photo editing software, if that is okay I can draw it up.Parts;Arduino Nano v3.0Ultrasonic Ranging Module HC - SR04Don't have the part number but it is a generic TM1637 display driver with 4x 8 segment displays.

author: rrinker
author_rank: Super Contributor
timestamp: December 29, 2016, 05:24:37 pm
content: I just built almost the exact same thing. I have it simply outputting in CMs. It's just math to convert the units. The library for the display has an example where you can see how to send whatever you want to it. You will have 2 variables, one with the feet and one with the inches, send those to the display as shown in the examples.--Randy