I could use some programming help

Hi, I'm new to the Arduino and I could use some help with programming.

I've set the Arduino up with the DHT Temperature Sensor program and that program works just fine. I've then moved on to including a second instantiation of the program to read two sensors simultaneously and that works just fine too.

From there I built a project using 3 shift registers to drive 3 Seven Segment LED displays. Again, I had success with that and ever wrote my own program for that from scratch.

Now I'm working on combining these two programs. And for that I need to convert the FLOAT temperature that is being returned from the DHT Temperature sensor program into two individual Integers to display on my LED display. I only need to display the first two digits of the temperature.

Following is a stripped down version of my program for simplicity. This program doesn't contain any of the shift register or LED stuff. Right now I'm just working on trying to convert the FLOAT temperature into two individual single-digit Integers. (that's what I need for my LED display program)

I have two problems. One is that when I separate these digits out I don't understand why I have to use the code I'm using, but it seems to work. I just don't understand why it's working.

Let me cover that question first:

Here's my code:

// Temperature Display Program
// Define Global Variables
#include "DHT.h"
#define DHTPIN_1 A0         // Data input pin!
#define DHTTYPE DHT11       // DHT 11
DHT dht(DHTPIN_1, DHTTYPE); // Initialize DHT sensor.
int Integer;                // Used to hold a coversion from FLOAT
int LED_Digit[2];           // Will be used to hold integer digist to be displayed
String stringOne;           // Used to hold a conversion from Integer to String
String strDigit[2];         // Used to hold individual numerals. 

void setup() {
 
  Serial.begin(9600); // Set Baud Rate
  Serial.println("DHTxx test!"); // Initial message.
  dht.begin(); // Set up the temperature sensor program
}

void loop() {
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  Serial.print(f);
  Serial.print(" *F\t    ");

  Integer = int(f);                       // Converting the FLOAT f into an INT Integer
  stringOne = String(Integer, DEC);       // Converting the INT Interger into a String called stringOne.
 
  strDigit[0]= stringOne.substring(0,1);  // getting the first string digit into an string array. 
  strDigit[1]= stringOne.substring(2,1);  // getting the second string digit into an string array.  Should be (1,1) though?
 
 Serial.print(strDigit[0]);               // print my first digit
 Serial.print(" ");
 Serial.print(strDigit[1]);               // print my second digit
 Serial.println( " <---My two individual digits");

   delay(10000); // Ten second delay between reads
}

I'm working near the bottom right after the line: Serial.print(" *F\t ");

The next line I use: Integer = int(f); to convert the FLOAT into an INT.

I'm only interested in the integer part so this is fine.

Then I use stringOne = String(Integer, DEC); to covert the integer to a string.

The only reason I'm doing this step is because it's the only way I know to separate out the individual digits.

To do that I use strDigit[0]= stringOne.substring(0,1); This assigns the first digit in zeroth position a single character to the strDigit[0] array.

That's the first digit I need. The most significant digit.

Then I use strDigit[1]= stringOne.substring(2,1); to get the second digit. This is the part I don't understand. Shouldn't this work with (1,1)? It doesn't. I have to use a 2 here which doesn't make any sense to me. But at least it works this way.

Then I Serial.print the two string characters which looks like the following on my serial display:

DHTxx test!
71.60 *F 7 1 <---My two individual digits
71.60 *F 7 1 <---My two individual digits
71.60 *F 7 1 <---My two individual digits
71.60 *F 7 1 <---My two individual digits
71.60 *F 7 1 <---My two individual digits
71.60 *F 7 1 <---My two individual digits

This is what I expected and this is working just fine (except I don't understand why I need to use a 2 instead of a 1 as the index for the second digit in the substring function.


**And now for my real question:**

Now that I have my single digits separated out as individual string digits how do I turn these back into integers. They need to be single digit integers to work in my LED display program. I have it set up to read integers not strings. 

So how do I convert these single digit strings back into integers?'

Or alternatively, is there a better way to get from a FLOAT number to two individual integers?

Thanks,
James
int tens = temperature / 10;
Int ones = temperature % 10;

Temperature is your float value.

If I'm reading that correctly you are converting a float to a string so you can pick out individual digits that you now want to change to an int?

Why not just go straight to an int when you read?

int f = (int) dht.readTemperature(true) + .5;  // round off

First of all, get rid of the String variables and use C strings instead. The String class is a resource hog. You can convert the data. This gives you a place to start:

void setup() {
  int val;
  float temp = 71.60;
  float remainder;

  Serial.begin(9600);
  val = (int) temp;         // This truncates the floating point value
  remainder = temp - val;   // This gives what's left
  
  Serial.print(val);
  Serial.print("  ");
  Serial.println(remainder);

  // If you need strings:

  char strTemp[5];
  char strRemainder[4];
  itoa(val, strTemp, 10);
  dtostrf(remainder, 3, 3, strRemainder);
  Serial.print(strTemp);
  Serial.print("  ");
  Serial.println(strRemainder);
}

void loop() {
}

You don't need strings at all. Do like reply 1 and keep it as numbers.

Wow! I sure pays to post on this forum!

You guys are on the ball.

@Jimmy60, thanks for the heads-up on reading integer directly. I didn't think I could do that, I thought the DTH function required a float variable. I just never thought to try changing that to int. I'll definitely start with that.

@groundfungus, I was thinking about dividing by 10 to get the most significant digit. But I wasn't sure how to get the single digit. I tried your code and it works slick. I confess that I wasn't aware that I could use the modulo function to get the least significant digit. Thanks! That's the part I didn't know how to do. Although I guess I could have also re-multiplied the first digit by ten and just subtracted that too. But using modulo saves a step!

I'm almost embarrassed here how easy you guys made this. Thanks!

@econjack, I won't need to use strings at all now. But I still want to thank you for the code example you posted because I can still learn from that example for future projects when I might need to deal with strings or characters.

Thanks for the quick replies.

Delta_G:
You don't need strings at all. Do like reply 1 and keep it as numbers.

Yep, that's definitely what I'm going to do now. I should have realized that I could do it all with math.

I'm new to this stuff. You'll have to excuse my ignorance.

I'll probably be back later with some more really stupid questions because I have a lot of projects I'm working on. But feeling embarrassed here beats trying to figure it all out on my own. (ha ha)

By the way, does anyone know why I needed to use a 2 in the substring function for the second digit?

Even though I won't be using that code it still bugs me why a 1 didn't work for the second digit. Zero worked as the index for the first digit. The index 1 doesn't return anything at all, and I had to use a 2 to access the second digit of the string.

That just wasn't making sense to me at all.

But, I don't need to use substring now. Still bugs me why it wasn't working the way I thought it should.

I mean, why did I need to use a 2 as the index on the second substring function:

strDigit[0]= stringOne.substring(0,1);  // getting the first string digit into an string array.
strDigit[1]= stringOne.substring(2,1);  // getting the second string digit into an string array.  Should be (1,1) though?

If the first index of 0 worked for the first digit, shouldn't 1 work as the index for the second digit?

I had to use a 2 there. If I put a 1 there I just comes back blank.

The string should just contain the two numerals "71".

It's not important now, but that was messing me up when I was writing this program.