Hello, I've been building off of a code that worked for the DHT11 Temperature and Humidity Sensor that was included with a kit that I got, which was for an Arduino Mega 2560 (which is what was used for this), and I tried to add a way for a user to input temperatures and convert them into the other temperatures.
For example, if they enter the temperature 0 degrees Celsius, the Arduino would display Fahrenheit as 36 degrees Fahrenheit, and display Kelvin as 273.15.
In reality, though my code would display Celsius at 675283008.00 degrees Celsius when I enter 0 for the input temperature and display 1215509376.00 degrees Fahrenheit for Fahrenheit, and 675283264.00 degrees Kelvin.
This was the code given by the kit I purchased
//www.elegoo.com
//2018.10.25
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 12;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
/*
* Initialize the serial port.
*/
void setup( )
{
Serial.begin(9600);
pinMode (12,INPUT);
}
/*
* Poll for a measurement, keeping the state machine alive. Returns
* true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if( millis( ) - measurement_timestamp > 3000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
/*
* Main program loop.
*/
void loop( )
{
float Ctemperature;
float humidity;
float Ftemperature;
float Ktemperature;
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if( measure_environment( &Ctemperature, &humidity ) == true )
{
Ftemperature = (((Ctemperature * 9) / 5) + 32);
Ktemperature = ((((Ftemperature - 32) * 5) / 9) + 273.15);
Serial.print( "Temperature = " );
Serial.print( Ftemperature, 2 );
Serial.print( " degrees Fahrenheit, ");
Serial.print( Ctemperature, 2 );
Serial.print( " degrees Celsius, ");
Serial.print( Ktemperature, 2 );
Serial.print( " degress Kelvin, Humidity = " );
Serial.print( humidity, 2 );
Serial.println( "%" );
}
}
This is the code after I worked on conversions and tried adding in the user-defined converter.
//www.elegoo.com
//2018.10.25
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
static const int DHT_SENSOR_PIN = 12;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
String userMessage = "Enter 1 for thermometer, Enter 2 for temperature conversions.";
int userInput;
/*
Initialize the serial port.
*/
void setup( )
{
Serial.begin(9600);
pinMode (12, INPUT);
}
/*
Poll for a measurement, keeping the state machine alive. Returns
true if a measurement is available.
*/
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
/* Measure once every four seconds. */
if ( millis( ) - measurement_timestamp > 3000ul )
{
if ( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return ( true );
}
}
return ( false );
}
/*
Main program loop.
*/
void loop( )
{
float Ctemperature;
float humidity;
float Ftemperature;
float Ktemperature;
String userUnitMessage = "Would you like to input in Celsius, Fahrenheit, or Kelvin? Enter 1 for Celsius, 2 for Fahrenheit, 3 for Kelvin.";
int userUnitInput;
String userTemperatureMessage = "Please enter the temperature you would like to convert";
int userTemperatureInput;
start:
Serial.println (userMessage);
while (Serial.available() == 0) {}
userInput = Serial.parseInt();
if (userInput = 1) {
/* Measure temperature and humidity. If the functions returns
true, then a measurement is available. */
if ( measure_environment( &Ctemperature, &humidity ) == true )
{
Ftemperature = (((Ctemperature * 9) / 5) + 32);
Ktemperature = ((((Ftemperature - 32) * 5) / 9) + 273.15);
Serial.print( "Temperature = " );
Serial.print( Ftemperature, 2 );
Serial.print( " degrees Fahrenheit, ");
Serial.print( Ctemperature, 2 );
Serial.print( " degrees Celsius, ");
Serial.print( Ktemperature, 2 );
Serial.print( " degress Kelvin, Humidity = " );
Serial.print( humidity, 2 );
Serial.println( "%" );
goto start;
}
}
if (userInput = 2) {
Serial.println (userUnitMessage);
while (Serial.available() == 0) {}
userUnitInput = Serial.parseInt();
if (userUnitInput = 1) {
Serial.println (userTemperatureMessage);
while (Serial.available() == 0) {}
userTemperatureInput = Serial.parseInt();
userTemperatureInput = Ctemperature;
goto Converter;
}
if (userUnitInput = 2) {
Serial.println (userTemperatureMessage);
while (Serial.available() == 0) {}
userTemperatureInput = Serial.parseInt();
userTemperatureInput = Ftemperature;
goto Converter;
}
if (userUnitInput = 3) {
Serial.println (userTemperatureMessage);
while (Serial.available() == 0) {}
userTemperatureInput = Serial.parseInt();
userTemperatureInput = Ktemperature;
goto Converter;
}
Converter:
Ftemperature = (((Ctemperature * 9) / 5) + 32);
Ktemperature = ((((Ftemperature - 32) * 5) / 9) + 273.15);
Ctemperature = (((Ftemperature - 32) * 5) / 9);
Serial.println( "Temperature" );
Serial.println( "--------------------");
Serial.print( Ftemperature, 2 );
Serial.println( " degrees Fahrenheit");
Serial.print( Ctemperature, 2 );
Serial.println( " degrees Celsius");
Serial.print( Ktemperature, 2 );
Serial.println( " degress Kelvin");
}
}
I was able to convert the readings from the sensor easily by adding 2 equations to convert to Kelvin and Fahrenheit and thought that being able to utilize user input to use those equations and also adding an equation to convert to Celsius would suffice, but the calculations don't seem to calculate how I would like them to.
Also, if I enter the converter code to its own file, it only shows the conversions if Celsius equals 0 degrees, even if I input 32 degrees for Celsius. So if I'm not mistaken I feel like my error is somewhere within gathering the user's input for the value of the type of temperature they choose to convert.
Any help would be helpful, as I am somewhat a beginner to Arduino, thanks for any advice or help in advance.