Converting Lightning Sensors Kilometers to Miles

Hi guys, I’m a newbee and fumbling my way through programing and I’m having some success. What I can’t figure out how to do is convert the kilometers that is read out of the lightning sensor and printed on the display into miles. I know the equation to do that but I’m not sure how to take the value from the code below which appears to print AO as a kilometers. At this time it prints 1km or 22km and I realize that converting to miles will give me a sort of odd number like 0.62 or 13.67.

Can someone tell me how to convert that value into miles. The code is below or for more detailed information into the program that I am using see this link: https://www.sparkfun.com/news/2968

Thanks,

Curt

void lightningData()

{

delay(100);

Wire.begin();

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.

printTitle(“Storm!”, 0);

oled.clear(PAGE); // Clear the screen

oled.setFontType(0); // Set font to type 0

oled.setCursor(0, 0); // Set cursor to top-left

delay(50); // Wait 500ms before next example

oled.clear(PAGE); // Clear the display

oled.setCursor(0, 0); // Set cursor to top-left

oled.setFontType(0); // Smallest font

oled.print("Distance: ");

oled.setCursor(16, 12);// Print “A0”

oled.setFontType(2);

oled.print(distanceview);

oled.setCursor(0, 34);

oled.setFontType(0);

oled.print(“Km Away!”);

oled.display();

Wire.end();

}

void printTitle(String title, int font)

{

int middleX = oled.getLCDWidth() / 2;

int middleY = oled.getLCDHeight() / 2;

oled.clear(PAGE);

oled.setFontType(font);

// Try to set the cursor in the middle of the screen

oled.setCursor(middleX - (oled.getFontWidth() * (title.length() / 2)),

middleY - (oled.getFontHeight() / 2));

// Print the title:

oled.print(title);

oled.display();

delay(1500);

oled.clear(PAGE);

}

A0 is a comment. The variable containing the distance is “distanceview” two lines down from that comment. It is declared as an integer, so performing math on a distance in Kilometers (KM * 0.621371) will result in an integer without any places after the decimal.

Is there a way to convert to get two places after the decimal?

Sure, just replace the “oled.print(distanceview);” statement with the following:

int miles =  ((float)distanceview * 0.621371) * 100.0; // convert to miles, shift over 2 places to truncate to 2 digits after the decimal point
oled.print(miles/100); // shift over 2 places to show just the miles using integer math
oled.print("."); // show the decimal point
oled.print(miles - ((miles/100)*100)); // and display the 2 places after the decimal point

Wow! Can’t wait to try this. Thank you very much!

When I was in grade school they started teaching us the metric system becuase. “the whole world was going to switch to it”. That was 50 years ago and the U.S. has never fully adopted it.