Temp and Humidity sensor causing sporadic outputs

Hi,
I am extremely new to arduino (this is my first project). I am using an Arduino Uno R3 with a cc3000 Wifi Board (my Wifi works well). What I am having trouble with is my outputs. This project is supposed to take the temp and humidity and then decide which output to select. What happens is that the temp could stay the same (to where the output should not change ) but the output changes. Sometime it seems to work properly and others times it does not work. I hope that there is just something in my code that is wrong. The voltage into my sensors are correct. Any help would be great. thanks
Attached is my file

Fan_Test_Ectest.ino (8.65 KB)

try change

int tempF; int humid;

to

float tempF; float humid;

Hi, welcome to the forum.

I think the CC3000 uses the SPI bus with pin 12. But you have ‘offPin’ also connected to pin 12.
How is everything connected ? Do you have a CC3000 shield or a module ?

What kind of sensor is connected to A0 and A1 ? I see examples for the DHT11 or DHT22, but no examples for a sensor with analog outputs.
Did you use an example for the temperature and humidity. Can you give a link to that example ?
Is the #include “utility/debug.h” in that example ? I don’t understand it.

Have a look at this line: tempF = (5.0 * tempF * 100.0) / 1024.0;
I prefer that you use float variables for that, and cast the integer from analogRead() to float before doing any calculation.
I also prefer to store them in float variables as robtillaart wrote. You might have to adjust the tempSet() and humSet() functions.

const int pinTemp = A0;
const int pinHumid = A1;

...

// Temperature
// Get the analog value and calculate with float
int rawTemp = analogRead(pinTemp);
float fahrenheit = (5.0 * (float) rawTemp * 100.0 ) / 1024.0;
float celsius = (fahrenheit - 32.0) * 5.0 / 9.0;

// Humidity
// Get the analog value and calculate with float
int rawHumid = analogRead(pinHumid);
float humidity = ( (float) rawHumid - 163.0 ) / 6.32;

// remember them.
tempF = fahrenheit;
humid = humidity;

Peter: I think you are right about pin 12 I looked it up and it says that it’s for MISO. I will change that to something unused (currently I am not even using the offPin but will in the future maybe). Also I am using the shield. On A0 I am using the LM34 temp sensor and on pin A1 I am using HIH-4000 Humidity sensor.

About the #include "utility/debug.h" I am not even sure where I pulled that from. I think that came from something to do with the WiFi part. I did not use any one example for the temp and humidity. I looked at http://forum.arduino.cc/index.php/topic,14384.0.html and http://forum.arduino.cc/index.php/topic,122621.0.html I just thought what I wrote made sense to me from a code standpoint but apparently I was wrong!

I will try the float and see where that gets me this afternoon. I really appreciate your help.

Rob: Thanks for the suggestion I plan on trying that this afternoon.

OK I ran this up and it does not output anything at all. I tried a variation of your code because I wanted to try it on my own and it was no good then I just put your code directly into it and the same thing happened. I may have put something in the wrong place or wrote it wrong. Also I looked at the serial print and I got a varied rang of temps. Any more help would be great. Thank you very much for your help. I have attached the serial print and the code
Thanks Matt

sketch_oct16a.ino (11.9 KB)

Doc1.docx (104 KB)

Please be more careful when writing code. Please add more comments. Use a nice layout of the source code (use always the same style of indents and so on).

You should have used another pin than pin 12.
When you do this : pinMode(offPin, OUTPUT);
you are setting it as output but also LOW, since that is the default.
Later in the sketch you make it high and low.

Can you search for things like this: if (offPin = HIGH) {
It must be: if (offPin == HIGH) {
There are about 5 of those.

I want to do this step by step. So it might have worked a little already, but I let’s go back one step make sure that the temperature is okay with a test sketch.

const int pinTemp = A0;
const int pinHumid = A1;

int stored_temp;
int stored_hum;

void setup(void)
{
  Serial.begin(9600);
  Serial.println(F("Hello test sketch"));
}

void loop(void)
{
  // Temperature
  // Get the analog value and calculate with float
  int rawTemp = analogRead(pinTemp);
  float fahrenheit = (5.0 * (float) rawTemp * 100.0 ) / 1024.0;
  float celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
  stored_temp = fahrenheit;

  // Humidity
  // Get the analog value and calculate with float
  int rawHumid = analogRead(pinHumid);
  float humidity = ( (float) rawHumid - 163.0 ) / 6.32;
  stored_hum = humidity;

  Serial.print("Temperature = ");  //Print the temperature in the serial printer
  Serial.print(fahrenheit);
  Serial.print(" (");
  Serial.print(stored_temp);
  Serial.print(") *F,   ");
  Serial.print("Humidity = ");  //Print the humidity in the serial printer
  Serial.print(humidity);
  Serial.print(" (");
  Serial.print(stored_hum);
  Serial.println(") %");
  delay(2000);
}

It is not needed to set a pin to input when analogRead is used.

If the values still change too much, there might be nothing connected to A0 and A1.

Thanks for your quick reply and all of you help. You have guided me on the right path and I finally got it working. The main reason my temp was jumping around and not being stable was that LM34 output needed to be filtered. Once I did that things started to work well with my original program. I still couldn't get any output out of the one you wrote. I must have put something in the wrong place. Thanks again for all your help.

Do you use a hardware filter with a capacitor (and a resistor) ? You can do the same in software by using many samples and calculating the average.

When you encounter any (new) problem, just ask. You made already a few functions, that is very good. You could also make functions for the temperature and humidity, like getTemperature() and getHumidity().

In the Arduino IDE on the upper-right is a drop-down-menu. You can add new files, they will open in new tabs. When you add a file "TempHum" (without extension) the file TempHum.ino will be added to your project. You could place all the temperature and humidity functions there.

Have fun.

OK I need help again. In this code I should be able to press 1,2,3,4 or ? at any time to change a speed of my fan. But I can press any number and in any order it works up to 3 times then it does not set anymore outputs. For example I could press 2 ,4,3, then it doesn’t work anymore. I just need help on how to get it to be able to repeat.

if (client) // If the client is connected run this section of code
  {
    Serial.println(F("Connected"));
    client.println("Connected");
     // Check if there is data available to read.
     while (1)
     {
       if (client.available() > 0) 
       {
       // Read a byte and write it to all clients.
         uint8_t ch = client.read();
         // Respond to fan telnet input
				//when 1 is pressed then the off pin output will engage the fan off
				if (ch == '1') {
					digitalWrite(offPin, HIGH);
					digitalWrite(lowPin, LOW);
					digitalWrite(medPin, LOW);
					digitalWrite(highPin, LOW);
					//prints the speed of the fan over telnet
					client.fastrprint("Fan is ");
                                        delay(5);
					if (offPin = HIGH) {
						client.fastrprintln("OFF.");
                                                delay(5);
					}

				}
				//when 2 is pressed then the low pin output will engage the low speed
				else if (ch == '2') {
					digitalWrite(offPin, LOW);
					digitalWrite(lowPin, HIGH);
					digitalWrite(medPin, LOW);
					digitalWrite(highPin, LOW);
					client.fastrprint("Fan is on ");
                                        delay(5);
					//prints the speed of the fan over telnet
					if (lowPin = HIGH) {
						client.fastrprintln("low speed.");
                                                delay(5);
					}
				}
				//when 3 is pressed then the med pin output will engage the medium speed
				else if (ch == '3') {
					digitalWrite(offPin, LOW);
					digitalWrite(lowPin, LOW);
					digitalWrite(medPin, HIGH);
					digitalWrite(highPin, LOW);
					//prints the speed of the fan over telnet
					client.fastrprint("Fan is on ");
                                        delay(5);
					if (medPin = HIGH) {
						client.fastrprintln("medium speed.");
                                                delay(5);
					}
				}
				//when 4 is pressed then the high pin output will engage the high speed
				else if (ch == '4') {
					digitalWrite(offPin, LOW);
					digitalWrite(lowPin, LOW);
					digitalWrite(medPin, LOW);
					digitalWrite(highPin, HIGH);
					//prints the speed of the fan over telnet
					client.fastrprint("Fan is on ");
                                        delay(5);
					if (highPin = HIGH) {
						client.fastrprintln("high speed.");
                                                delay(5);
					}

				}
				//when ? is pressed the status of the fan will be read
				if (ch == '?') {
					if (offPin == HIGH) {
						client.fastrprintln("Fan is off.");
                                                delay(5);
					}
					else if (lowPin == HIGH) {
						client.fastrprintln("Fan is Low speed.");
                                                delay(5);
					}
					else if (medPin == HIGH) {
						client.fastrprintln("Fan is Medium speed.");
                                                delay(5);
					}
					else if (highPin == HIGH) {
						client.fastrprintln("Fan is High speed.");
                                                delay(5);
					}
                                    }
       }
     }

I don't know. You can add a test command 't' or '9' that simulates pressing 10 normal command with a delay in between. Could there be a bug in client.fastrprintln() that allocates memory but does not release it ?

OK thanks I will look to see if there is a bug. Thanks again

Perhaps you are hitting the sram border. You could report the available memory (while running) to the serial monitor. http://playground.arduino.cc/Code/AvailableMemory

Peter I am going to look into that available memory page you suggested, but I did come across this: http://forums.adafruit.com/viewtopic.php?f=31&t=48929#p248928 Being new I am having trouble understanding everything they are talking about but I believe that it is the same problem I am having. I just don’t really understand what they did to resolve it. I did use their spi-div2 and it did nothing for me.

That would be bad, the CC3000 modules having troubles with the SPI. But those posts are from February 2014, and it should be fixed by now.

Do you know the firmware version of the CC3000. I read in another topic that it can be upgraded.

This doesn't look promising : http://github.com/cmagagna/CC3000Patch/blob/master/README.md The CC3000 is buggy ? and you should use the CC3200 or CC3100 ! That's ... very ... I don't know :~

Alright that kinda helps point me in the direction of the cc3200 then. I do have that latest patch and will see about upgrading chips. Thanks I am glad you came across that.

Peter I got it to work. I posted in the adafruit cc3000 forum and they told me that I needed a digital read in front of the pin output.

client.fastrprint("Fan is on ");
                                        delay(5);
                    //prints the speed of the fan over telnet
                    if (digitalRead(lowPin) == HIGH) {
                        client.fastrprintln("low speed.");
                                                delay(5);

I can't believe that I missed it. I appreciate all of your help and thought I would just post this so you know that it is resolved. Thanks again.

Oooooooooooo, I totally missed that also. I'm glad you have it working. Thanks for letting me know. Did you also solve all the single '=' inside the if-statements ?

Yeah I fixed all those to "==" I didn't understand why that did not work earlier now I see why!