Main Site Documentation

DHT11 float and string question


#1

Hello,
I am trying to do an if statement. I am getting a error [quote]Operator ‘<’ cannot be applied to operands of type ‘string’ and 'string[/quote] How do I use the < operator in this case?
Thanks,
I am a beginner.
Sensor Page: http://code.tinyclr.com/project/289/dht11---temperature-and-humidity-sensor/


namespace Sensor Test
{
    public class Program
    {

        public static void Main()
        {
            DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7);
            if (MyDHT11.Humidity.ToString() < "26") 

            }
        }
    }


#2

You need to convert value to an int and then check if it is less then 26.


#3

To convert:

int x = int.Parse(MyDHT11.Humidity.ToString());

Note 1: This code doesn’t do error checking (like if the Humidity value == “too high!!”).

For that look into int.TryParse().

Note 2: If MyDHT11.Humidity is already a string value, then you don’t need to do .ToString() again.


#4

Thank You,


#5

Sorry, I am reciving a error

Here is the code


DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7);

            while (true)
            {
                if (MyDHT11.ReadSensor())
                {
                    int x = int.Parse(MyDHT11.Humidity.ToString());
                    if (x < "26")                 
                        Debug.Print("The humidity is less than 26");                  
                    else 
                        Debug.Print("The humidity is more than 26");
                }
                else 
                    Debug.Print("DHT11 Error : " + MyDHT11.LastError);

Thanks, :slight_smile:


#6

you’ve converted the humidity back to an integer, but you’re still comparing it to “26” which is a string.

Remove the quotes and you will be fine.

if (x < 26)    

#7

Thank You,
It works!


#8

Guys,

why all the fuzz with that conversion to int’s or strings??

The driver returns a float so you can just do:


DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7);
 
            while (true)
            {
                if (MyDHT11.ReadSensor())
                {
                    if (MyDHT11.Humidity < 26f)                 
                        Debug.Print("The humidity is less than 26");                  
                    else 
                        Debug.Print("The humidity is more than 26");
                }
                else 
                    Debug.Print("DHT11 Error : " + MyDHT11.LastError);

#9

Doh! :wall:


#10

Thanks,


#11

actually, let’s blame Gus. I think he must have been iPhoning and said “yes” when asked was it a string - which got us to where we are now :slight_smile: :slight_smile:


#12

How can I make this up to yo guys? What about another FEZ?


#13

announcement inbound? :o


#14

I finally, after many long hours, got the four LEDs fading from one to another in a kind-of circle on my STM32F4-Discovery board.

Wow, it really makes you appreciate NETMF and especially GHI’s support and documentation!

So, Gus, to answer your question, yes, another FEZ will make it all better.


#15

You got the port working on Discovery?


#16

Haha, no, I wish. I’m just working in C/ASM in Atollic’s free version.


#17

I am learning a lot about porting kit and that board in particular. I like it.