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