Why is one sketch working and another not?

Hello,

I'm using a SHT15 sensor to read humidity and I'd like a fan to turn on when the humidity drops below a predetermined point and off when it exceeds another point.

I modified the SHT1x example and the fan works as expected

/**
 * ReadSHT1xValues
 *
 * Read temperature and humidity values from an SHT1x-series (SHT10,
 * SHT11, SHT15) sensor.
 *
 * Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
 * www.practicalarduino.com
 */

#include <SHT1x.h>

// Specify data and clock connections and instantiate SHT1x object

#define transistorPin 5
#define dataPin  7
#define clockPin 6
SHT1x sht1x(dataPin, clockPin);

int OnFan = 65;  // If the humidity drops below this value (expressed in $%) turn on fans

void setup()
{
   Serial.begin(9600); // Open serial connection to report values to host
   Serial.println("Starting up");
   pinMode(transistorPin, OUTPUT);
}

void loop()
{
  float temp_c;
  float temp_f;
  float humidity;

  // Read values from the sensor
  temp_c = sht1x.readTemperatureC();
  temp_f = sht1x.readTemperatureF();
  humidity = sht1x.readHumidity();

  // Print the values to the serial port
  Serial.print("Temperature: ");
  Serial.print(temp_c, DEC);
  Serial.print("C / ");
  Serial.print(temp_f, DEC);
  Serial.print("F. Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  delay(5000);
  
  if (humidity < OnFan)
  {
    digitalWrite(transistorPin, HIGH);
  }
  else
  {
    digitalWrite(transistorPin, LOW);
  }
}

Encouraged, I plugged the relevant functioning bits into my real sketch which Tweets the humidity and temp and the fan does not work:

/*
 * A simple sketch that uses WiServer to send a tweet with the current system time every 90 minutes
 */

#include <WiServer.h>
#include <SHT1x.h>

#define WIRELESS_MODE_INFRA      1
#define WIRELESS_MODE_ADHOC      2

#define transistorPin 5
#define dataPin  7
#define clockPin 6
SHT1x sht1x(dataPin,clockPin); //data and clock pins for SHT1x

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};      // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};      // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};      // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"2WIRE187"};            // max 32 bytes

unsigned char security_type = 2;      // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"6XXXXXX"};      // max 64 characters

// WEP 128-bit keys
// sample HEX keys  30F713CD4F1543A7AF63B4E0DF
prog_uchar wep_keys[] PROGMEM = { 0x30, 0xF7, 0x13, 0xCD, 0x4F, 0x15, 0x43, 0xA7, 0xAF, 0x63, 0xB4, 0xE0, 0xDF,      // Key 0
                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 1
                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Key 2
                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00      // Key 3
                        };

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------


// Auth string for the Twitter account
char* auth = "amFjXXXXXXXXXXXXXXmI="; // Base64 encoded USERNAME:PASSWORD
long intervalHum = 30000; //set the interval for temp and humidity to be checked
long intervalTweet = 5400000; // Set the interval for tweets - 1.5 Hrs
float humidity; // Init humidty variable
float temp_f; // Init temp variable
int tweet = 0; /*Used to tweet once on initial startup. Otherwise 
                 would have to wait until <intervalTweet> expires */
long previousMillis = 0;
long previousMillisTweet = 0;
long tweetTime = 0; // Time (in millis) when the next tweet should be sent
int OnFan = 65;  // If the humidity drops below this value (expressed in $%) turn on fans

// This function generates a message with the current system time
void currentTemp() {
   WiServer.print("["); 
   WiServer.printTime(millis()); // Append time Arduino has been running - gets around duplicate tweet filtering
   WiServer.print("] ");
   WiServer.print("Humidor temp and humidity is: ");
   WiServer.print(temp_f);
   WiServer.print("° F and ");
   WiServer.print(humidity);
   WiServer.print("%");
}

// A request that sends a Tweet using the currentTime function
TWEETrequest sentMyTweet(auth, currentTemp);


void setup()
{
    // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages) 
  WiServer.init(NULL);
  
  // Enable Serial output and ask WiServer to generate log messages (optional)
  Serial.begin(9600);
  WiServer.enableVerboseMode(true);

  // set  the transistor pin as output:
  pinMode(transistorPin, OUTPUT);
}

void loop()
{ 
  if (tweet == 0) // Tweets once at inital startup.  Next tweet won't happen for <intervalTweet>
  {
    ++tweet; // Increment initial tweet count
    Serial.println("Tweeting...");
    sentMyTweet.submit();
  }
    
// Set the temp and humidity to be checked every 30 seconds (defined by <intervalHum> global variable
// If you don't set this to a reasonable number the arduino seems to get so busy running the
// temp check functions it doesn't have time to run the WiShield TCP/IP stack - ergo all IP comms break
  if (millis() - previousMillis > intervalHum) 
  {                                                              
    previousMillis = millis();                         
    temp_f = sht1x.readTemperatureF();
    humidity = sht1x.readHumidity();
  }   
  
// Set a tweet to occur every two hours (defined by <intervalTweet> global variable)
  if (millis() - previousMillisTweet > intervalTweet) 
  {
     previousMillisTweet = millis();
     Serial.println("Tweeting");
     sentMyTweet.submit();
  }
  
  if (humidity < OnFan)
  {
    digitalWrite(transistorPin, HIGH);
  }
  else
  {
    digitalWrite(transistorPin, LOW);
  }
  
WiServer.server_task(); 

}

Please help me troubleshoot what is wrong. My guess is that the timing of the humidity measurement is not clearly defined in the second sketch whereas in the first, it's ever 5 seconds. However, I thought that this bit from the 2nd sketch

if (millis() - previousMillis > intervalHum) 
  {                                                              
    previousMillis = millis();                         
    temp_f = sht1x.readTemperatureF();
    humidity = sht1x.readHumidity();
  }

would define a 30 second interval between measurements. That's the only major difference I can think of.

Thank you in advance for any help!

Jacob

Ah, solved it. In order for the 30 second interval to work, the wishield must be connected to the network. During my initial testing it was not. It works fine now. Yay.