help printing 4 digit float to seven segment display

I’m using an ultrasonic sensor to make a simple digital read out. I’m new to this and am having trouble printing a variable to the seven segment display. When I simply plug ‘distance’ in to myDisplay… I get the first digit of the value, in the first display of the 4 digit 7seg display. So I tried multiplying by 100, which will be fine because my range will be less than 100cm, to get rid of the decimal, but still get the same result.

I am using the sevseg library from this site.

#include "SevSeg.h"

SevSeg myDisplay;

// using 4 pin ultrasonic module to get distance output 

#define TRIGPIN A4   // send trigger pulse
#define ECHOPIN A5   // receive echo pulse

void setup() {
  
  pinMode(ECHOPIN, INPUT);  // setting pin 3 to input
  pinMode(TRIGPIN, OUTPUT);  // setting pin 2 to output


  
  int displayType = COMMON_CATHODE; 
  
  
       //pins for common ground
       int digit1 = 13; //Pin 1
       int digit2 = 12; //Pin 10
       int digit3 = 11; //Pin 4
       int digit4 = 10; //Pin 6
       
       //anode pins representin' the seven segments and decimal point
       int segA = 2; //Pin 12
       int segB = 3; //Pin 11
       int segC = 4; //Pin 3
       int segD = 5; //Pin 8
       int segE = 6; //Pin 2
       int segF = 7; //Pin 9
       int segG = 8; //Pin 7
       int segDP= 9; //Pin 5
   
  int numberOfDigits = 4; // becaue i'm using 4 digit display

  myDisplay.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
  
  myDisplay.SetBrightness(99); // brightness

}

void loop() {

  digitalWrite(TRIGPIN, LOW);  // process of microbursting trigger pulse 
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);

  float distance = pulseIn(ECHOPIN, HIGH);  //distance calculation
  distance = (distance/58)*100;
  
  char tempString [5] = {distance}; // need help here
  

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

  myDisplay.DisplayString(tempString, 2); //(numberToDisplay, decimal point location)
  
  delay(10);
  }
  
}

This doesn't work:

 char tempString [5] = {distance}; // need help here

Consider using the standard function itoa(), which converts an integer to a null-terminated character array. You may want to scale the value, round it, or something else. You can do that with the float, but itoa() operates on an int. To do it little ado, you can cast it inside the parantheses, like this:

 char tempString [5] = itoa((int)distance);

Before you spend a lot of time troubleshooting the LED display, I'll recommend that you make sure that the array contains what you think it does by Serial.print()'ing it. I think you'll progress faster in your project if you don't try to do too many things at once. Get a good value into the array using test data; then get a good value from the sensor; then display the results while Serial.print()'ing, and compare the results. When you connect three untested processes together, you can't readily tell which one(s) is(are) failing.

Consider using the standard function itoa(), which converts an integer to a null-terminated character array.

I recommend sprintf() instead. This way, you can control how many digits you get. Good if you want leading zeros, for example.

  float distance = pulseIn(ECHOPIN, HIGH);  //distance calculation
  distance = (distance/58)*100;

Don’t use floats if you can help it. I make it:

  // distance calculation:
  unsigned int distance = (unsigned int)((pulseIn(ECHOPIN, HIGH)*100L)/58);

or, if I feel like being fancy, I can do some arithmetic:

  // distance calculation:
  unsigned int distance = (unsigned int)(pulseIn(ECHOPIN, HIGH));
  // because 100/58 is 50/29, which is 2 minus 8/29
  distance = (2*distance) - ((8*distance)/29); // good only up to 141 cm

And then, instead of this:

 char tempString [5] = {distance}; // need help here

I use this:

 char tempString [6]; // the extra character is for "insurance"
 sprintf(tempString, "%2d%02d", distance/100, distance%100);

First off, thanks to both of you for your replies.

It works!!

Here is the code I ended up using:

#include "SevSeg.h"

SevSeg myDisplay;

// using 4 pin ultrasonic module to get distance output 

#define TRIGPIN A4   // send trigger pulse
#define ECHOPIN A5   // receive echo pulse

void setup() {
  
  Serial.begin(9600);
  pinMode(ECHOPIN, INPUT);  // setting pin a5 to input
  pinMode(TRIGPIN, OUTPUT);  // setting pin a4 to output


  
  int displayType = COMMON_CATHODE; 
  
  
       //pins for common ground
       int digit1 = 13; //Pin 1
       int digit2 = 12; //Pin 10
       int digit3 = 11; //Pin 4
       int digit4 = 10; //Pin 6
       
       //anode pins representin' the seven segments and decimal point
       int segA = 2; //Pin 12
       int segB = 3; //Pin 11
       int segC = 4; //Pin 3
       int segD = 5; //Pin 8
       int segE = 6; //Pin 2
       int segF = 7; //Pin 9
       int segG = 8; //Pin 7
       int segDP= 9; //Pin 5
   
  int numberOfDigits = 4; // becaue i'm using 4 digit display

  myDisplay.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
  
  myDisplay.SetBrightness(100); // brightness

}

void loop() {

  digitalWrite(TRIGPIN, LOW);  // process of microbursting trigger pulse 
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);

  // distance calculation:
  unsigned int distance = (unsigned int)((pulseIn(ECHOPIN, HIGH)*100L)/58);

  Serial.print(distance);
  Serial.println(" cm");
  
  char tempString [6];
  sprintf(tempString, "%2d%02d", distance/100, distance%100);

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

  myDisplay.DisplayString(tempString, 2); //(numberToDisplay, decimal point location)
  
  delay(10);
  }
  
}

I didn’t comment on the

char tempString [6];
  sprintf(tempString, "%2d%02d", distance/100, distance%100);

because I don’t yet quite understand what’s going on there… but I’m studying up on that now.

Another question I have now is the delay between readings. I get a 2 second delay both in the serial monitor and on the display. When I just the code for the ultrasonic sensor

// using 4 pin ultrasonic module to get distance output

#define TRIGPIN A4   // send trigger pulse
#define ECHOPIN A5   // receive echo pulse

void setup() {

  Serial.begin(9600);
  pinMode(ECHOPIN, INPUT);  // setting pin a5 to input
  pinMode(TRIGPIN, OUTPUT);  // setting pin a4 to output

}

void loop() {

  digitalWrite(TRIGPIN, LOW);  // process of microbursting trigger pulse 
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);

  int distance = pulseIn(ECHOPIN, HIGH);  //distance calculation
  distance = (distance/58)*100;
  Serial.print(distance);
  Serial.println(" cm");

  delay(200);
}

I get the result every 200ms as written in the code. I tried simply adding the

delay(200);

to the end of my void loop, but nothing changed…

^^ Figured it out. I changed the value in the for loop from 100 to 10 as such;

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

  myDisplay.DisplayString(tempString, 2); //(numberToDisplay, decimal point location)
  
  delay(10);
  }
  delay(10);

and added the 10ms delay to the void loop. I’m now getting closer to the results I’m after, I’ll keep playing with it.

Thanks again.