Go Down

Topic: Display Feet and Inch measurement on clock 7 segment display (TM1637) (Read 1 time) previous topic - next topic


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.

Code: [Select]
#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


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

SFEVL53L1X distanceSensor;
//Uncomment the following line to use the optional shutdown and interrupt pins.

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.

  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
  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;

    // calculate the average:
  average = total / numReadings;
  // send it to the computer as ASCII digits

  Serial.print("Distance(mm): ");
  oled.setCursor(0, 0);
  oled.print("Distance: ");

  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(totalIn, 0);



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()?

"The problem is in the code you didn't post."


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.


"The problem is in the code you didn't post."



totalIn is an int. When you add ",0" to print or println, you get something different to the zero decimal places you were expecting. An int already has no decimal places.

You need to fix the error I already told you about in my last paragraph.
"The problem is in the code you didn't post."

Go Up