Display Feet and Inch measurement on clock 7 segment display (TM1637)

Hi everyone.
Very new to this, though I have looked around to try and find an answer.

I have a Nano board and connected to it a sonar sensor as well as a TM1637 display.

This display has four seven segment digits.

My aim is to display inches in the fist 2 digits and inches in the last two.

Code so far outputs just Inches by themselves.
Trying to find out how to format it correctly so I can go from inches to display it feet and inches.

Also, when I write code to the Arduino, the TM1637 stops displaying anything. I have to disconnect the ground and reconnect it to get it to work.
Any ideas?

Thanks!
Jack,

The problem is on line 47 of the code you didn’t post. Please use [ code ] tags. (The button looks like </> on the full-reply editor.)

Just realised I am using this library not the one linked before..

Jackster:
Just realised I am using this library not the one linked before…
libraries/DigitalTube at master · reeedstudio/libraries · GitHub

But you’re still not going to show us the code you want help with?

Delta_G:
But you're still not going to show us the code you want help with?

I was not on the computer with it on.

 #include "TM1637.h" 

#define CLK               2                  /* 4-digital display clock pin */
#define DIO               3                 /* 4-digital display data pin */
TM1637 tm1637(CLK, DIO);                  /* 4-digital display object */
/* Sonar Sensor Pins */
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[4] = {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 distance
  inches = 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[i] = distance % 10;
        distance = distance / 10;  
        tm1637.display(i, bits[i]);                 /* 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.pdf
  return 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;
}
feet = inches / 12;
inchesRemaining = inches % 12;

Is that what you're looking for? The math?

Delta_G:

feet = inches / 12;

inchesRemaining = inches % 12;




Is that what you're looking for? The math?

How to implement it in code. I am newb

You have already got something almost identical implemented. Or did you just hack and paste that with zero understanding? That will almost always lead to issues.

You obviously need to add a couple of variables to hold the number of feet and the number of inches remaining. The display code you've already got. Are you really that lost or do you just want someone to write your code for you? There's no sin in that. We have a section on this forum called gigs and collaborations where you can offer someone remuneration for writing your code. Why don't you at least give it a try before you just say, "I can't."

If you don't understand coding basic math steps then you should probably back off this project and work through some basic tutorials. It really helps to get a basic understanding of the language before you dive in over your head.

Delta_G:
You have already got something almost identical implemented. Or did you just hack and paste that with zero understanding? That will almost always lead to issues.

You obviously need to add a couple of variables to hold the number of feet and the number of inches remaining. The display code you've already got. Are you really that lost or do you just want someone to write your code for you? There's no sin in that. We have a section on this forum called gigs and collaborations where you can offer someone remuneration for writing your code. Why don't you at least give it a try before you just say, "I can't."

If you don't understand coding basic math steps then you should probably back off this project and work through some basic tutorials. It really helps to get a basic understanding of the language before you dive in over your head.

I have seen the examples given with the libraries and then taken the code and put it together in a new sketch.

Trying to write it all myself atm. But with some of the features I need I will probably end up asking someone to help me for internet cookies. I just got stuck with formatting the output for the display.

You still haven't explained what you mean by "formatted".

There's nothing to format. You've already got the bits array where you're putting the numbers to display. Now you know how to get the numbers. What's more to do, put the numbers in there and display them.

aarg:
You still haven't explained what you mean by "formatted".

Feet and Inches, as in 10' 01". 10 feet and 1 inch.

atm it displays 0121.

Jackster:
Feet and Inches, as in 10’ 01". 10 feet and 1 inch.

atm it displays 0121.

Okay, 121 what? Centimeters?

aarg:
Okay, 121 what? Centimeters?

Inches

Jackster:
Feet and Inches, as in 10' 01". 10 feet and 1 inch.

On a 4 digit 7 segment you're not going to be able to have the ' or the " or the words feet or inches. You just get the 4 digits. You already have the code for displaying the digits from the bits array. All you need to do is put the different numbers in the array bits.

I’m trying to do the same thing. I am using a time-of-flight sensor, a Teensy 3.2 with a Teensyview OLED Display, and coming soon, NeoPixels (A 144 element strip that will light according to distance measured (But I’m not worried about that right now.)) I have the Teensy doing an average of 1000 readings in an attempt to smooth the readings out.

Everything works perfectly fine including displaying the measurement in millimeters on the top line and whole feet on the second line, but I’m having a hard time figuring out exactly how to get it to display inches, even after reading this string. I know it’s something stupid I’m missing. Either it displays nothing, a continuous count of inches beyond 12, Counts to 11 in the 1st foot but then nothing after, or ASCII Characters and symbols. Any help would be appreciated.

#include <Adafruit_NeoPixel.h>

#include <font5x7.h>
#include <font8x16.h>
#include <fontlargenumber.h>
#include <sevenSegment.h>
#include <TeensyView.h>

#include <ComponentObject.h>
#include <RangeSensor.h>
#include <SparkFun_VL53L1X.h>
#include <vl53l1x_class.h>
#include <vl53l1_error_codes.h>

#define PIN_RESET 15
#define PIN_DC    5
#define PIN_CS    10
#define PIN_SCK   13
#define PIN_MOSI  11

TeensyView oled(PIN_RESET, PIN_DC, PIN_CS, PIN_SCK, PIN_MOSI);

const int numReadings = 1000;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;  
int inch = 0;
int totalIn = 0;
int foot = 12;

/*
  Reading distance from the laser based VL53L1X
  By: Nathan Seidle
  SparkFun Electronics
  Date: April 4th, 2018
  License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

  SparkFun labored with love to create this code. Feel like supporting open source hardware?
  Buy a board from SparkFun! https://www.sparkfun.com/products/14667

  This example prints the distance to an object.

  Are you getting weird readings? Be sure the vacuum tape has been removed from the sensor.
*/

#include <Wire.h>
#include "SparkFun_VL53L1X.h"

//Optional interrupt and shutdown pins.
#define SHUTDOWN_PIN 2
#define INTERRUPT_PIN 3

SFEVL53L1X distanceSensor;
//Uncomment the following line to use the optional shutdown and interrupt pins.
//SFEVL53L1X distanceSensor(Wire, SHUTDOWN_PIN, INTERRUPT_PIN);

void setup()
{
    
  for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;}
    
  oled.begin();    // Initialize the OLED
  oled.clear(ALL); // Clear the display's internal memory
  oled.display();  // Display what's in the buffer (splashscreen)
  delay(1000);     // Delay 1000 ms
  oled.clear(PAGE); // Clear the buffer.
  
  Wire.begin();

  Serial.begin(9600);
  Serial.println("VL53L1X Test");

  if (distanceSensor.begin() == true)
  {
    Serial.println("Sensor online!");
  }
    

}

void loop()
{
  distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
  int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
  distanceSensor.stopRanging();
  distance = distance + 40; //Minimum reading distance is 40mm

    // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = distanceSensor.getDistance();
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;

    // if we're at the end of the array...
  if (readIndex >= numReadings) {
    // ...wrap around to the beginning:
    readIndex = 0;
  }
  delay(1);

    // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits
  Serial.println(average);

  oled.clear(PAGE);
  Serial.print("Distance(mm): ");
  oled.setCursor(0, 0);
  oled.print("Distance: ");
  Serial.print(average);
  oled.print(distance);
  oled.print("mm");

  float distanceInches = distance * 0.0393701 + 1.57;
  float distanceFeet = distanceInches / 12.0;
  inch = distanceInches;

  if(totalIn >= 12){
    inch = totalIn % 12;
  }


  Serial.print("\tDistance(ft): ");
  oled.setCursor(0, 8);
  oled.print("Imperial: ");
  Serial.print(distanceFeet, 2);
  oled.print(distanceFeet, 0);
  oled.print("'");
  oled.print(totalIn, 0);
  oled.print("''");

  Serial.println();
  oled.display();
}

You read from the sensor twice. Only one of those gets the extra 40 added to it.

Then, when outputting the data, you put entirely different results on the OLED and Serial. They look like they should be the same.

Then you add 1.57 inches to the measurement which already had 40mm added.

What value did you put in totalIn before using it in the if()?

Adding the 40mm after the primary read and before any other calculations are done to the reading gives me the desired result of the minimum value being 40mm or 1.57 inches to compensate for the minimum reading threshold.

What I’m having a hard time doing is figuring out how to put the variables to get it to display in feet & inches.

This should display:

Distance: 104mm
Imperial: 0’4"

but here’s what I get:

With the current setup, inches doesn’t display anything.

Meter1.jpg

…and the code which made that display?

The code is in my original reply above.