Pins not changing

First Arduino question ever… I normally program in JS and C#, some of my terminology here may reflect that and I’d like to apologize upfront.

I’m writing an environment controller using an Mega2560, a DHT22 and an array of relays and Potentiometers outputting to a 16X2 LCD.

I’m trying to read two pots, one represents the low temp and the represents the high. I wanna kick a relay on or off for heating an cooling. If/when the pot is adjusted or the temp range falls or rises I want the relay to open or close accordingly. Sounded simple enough. However, the state of the relay doesn’t seem to change.

Attached is my code file.

I think I’m doing it right. The pinModes are in the setup routine. Using timers instead of delays, and my serial output looks like this if I adjust the pots.

This should be cooling

Temp Heat Event
62/68 49/60
T: 69.4 H: 43.3
Avg T:65Avg H:54

This should be heating

Temp Heat Event
52/59 49/60
T: 69.4 H: 43.2
Avg T:55Avg H:54

I think I’m missing something simple

HumidityThaw.ino (12.6 KB)

coderbond:
However, the state of the relay doesn’t seem to change.

How do you know?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom... :slight_smile:

@Coding Badly

The serial message doesn’t change, the LED doesn’t change and neither does the relay.

If I uncomment the bootSequence method in the startup routine it runs thru and activates and deactivates each relay/led pair as expected.

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display


#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_22
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

int humHighSettingPin = A12; //Humidity High
int humHighValue = 0;

int humLowSettingPin = A13; //Humidity Low
int humLowValue = 0;

int tempHighSettingPin = A14; //TempHigh
int tempHighValue = 0;

int tempLowSettingPin = A15;  //TempLow
int tempLowValue = 0;

int medianTemp = 0;
int medianHumdity = 0;

float temperaturePin;
float temperatureValue;

float humidityPin;
float humidityValue;

void setup() {
  // put your setup code here, to run once:
    // set up the LCD's number of columns and rows:
lcd.init();
lcd.backlight();
Serial.begin(9600);
pinMode(23, OUTPUT); //Unknown
pinMode(24, OUTPUT); //Unknown
pinMode(25, OUTPUT); //Unknown
pinMode(26, OUTPUT); //Unknown
pinMode(27, OUTPUT); //Unknown
pinMode(28, OUTPUT); //Unknown
pinMode(29, OUTPUT); //Unknown
pinMode(30, OUTPUT); //Unknown
pinMode(31, OUTPUT); //Unknown
pinMode(32, OUTPUT); //Unknown
pinMode(33, OUTPUT); //Unknown
pinMode(34, OUTPUT); //Unknown
pinMode(35, OUTPUT); //Unknown
pinMode(36, OUTPUT); //Unknown
pinMode(37, OUTPUT); //Unknown

//bootSequence();

}

unsigned long previousMillis = 0;  

long interval = 1000;   //Humidifier Cold Cycle Length
int ledState = LOW;             // ledState used to set the LED

long lcdScrollInterval = 750;


void loop() {

  unsigned long currentMillis = millis(); //Get the current cycle

  //This is checking an event if you will....
  //In this case the measure environment. which is using a DHT22.
  //Note: the DHT22 is limited to one read every 4 seconds. 
  //This is the theoretical maximum of the machine+
  if( measure_environment( &temperaturePin, &humidityPin ) == true )
  {
    temperatureValue = temperaturePin;
    humidityValue = humidityPin;
  }
  
  if (measure_pots(&tempLowSettingPin) == true)
  {
    tempLowValue = map(analogRead(tempLowSettingPin), 0, 1023, 20, 90);
    tempHighValue = map(analogRead(tempHighSettingPin), 0, 1023, 25, 98);
    humLowValue = map(analogRead(humLowSettingPin), 0 , 1023, 10, 90);
    humHighValue = map(analogRead(humHighSettingPin), 0, 1023, 10, 90);

    medianTemp = ((tempLowValue + tempHighValue) / 2);
    medianHumdity = ((humLowValue + humHighValue) / 2);
  }
    
  if (measure_updateDisplay() == true)
  {
    //displayRunMode(temperatureValue, tempLowValue, tempHighValue, humidityValue, humLowValue, humHighValue);  
    displayRunMode(temperatureValue, tempLowValue,tempHighValue,humidityValue, humLowValue,humHighValue);
    serialDisplay(temperatureValue, tempLowValue, tempHighValue, humidityValue, humLowValue, humHighValue, ((tempLowValue + tempHighValue) / 2), ((humLowValue + humHighValue) / 2));      

      if (temperatureValue > medianTemp)
      { 
        Serial.print("Temp Cool Event");
        Serial.print("\n");
        digitalWrite(30, HIGH);        //Cool pin on   
        digitalWrite(33, HIGH);       //LED Indicator
        digitalWrite(28, LOW);
        digitalWrite(31, LOW);    
      } 
      else if (temperatureValue < medianTemp)
      {
        Serial.print("Temp Heat Event");
        Serial.print("\n");
        digitalWrite(28, HIGH);
        digitalWrite(31, HIGH);
        digitalWrite(30, LOW);
        digitalWrite(33, LOW);        
      }else
      {
        Serial.print("Temp Rest Event");
        Serial.print("\n");
        digitalWrite(28, LOW);
        digitalWrite(31, LOW);
        digitalWrite(30, LOW);
        digitalWrite(33, LOW);                
      }
  }
}


/*
 * Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.
 */

 
 //This defines an event based on a Timer if you will...
 //It's referenced in the main Loop event
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 );
}

static bool measure_pots(int *tempLow){
    static unsigned long measurement_timestamp = millis( );

      if( millis( ) - measurement_timestamp > 300ul )
      {
      measurement_timestamp = millis( );
      return( true );            
  }
  return( false );
}

static bool measure_updateDisplay()
{
    static unsigned long measurement_timestamp = millis();
  if (millis() - measurement_timestamp > 300ul)
  {
    measurement_timestamp = millis();
    return(true);  
  }  
  return(false);
}


void bootSequence()
{
  //// Device Boot Test
lcd.print("Enviro Con 1.0");
delay(1000);
lcd.clear();

lcd.print("Test Power On");
delay(2000);
lcd.clear();

//Dehumidify Defrost
//TURN ON THE OTHER HUM LIGHT

lcd.setCursor(0,0);
lcd.print("Pin 23 Open");
lcd.setCursor(0,1);
lcd.print("Dehum circuit open");
  digitalWrite(23, HIGH);
delay(4000);
  digitalWrite(23, LOW); //dehum circuit on
lcd.clear();

//Dehumidify Run -- Running
lcd.setCursor(0,0);
lcd.println("Pin 25 Open");
lcd.setCursor(0, 1);
lcd.println("Dehumidify (+)");
  digitalWrite(23, HIGH); //dehum circuit on
  digitalWrite(24, HIGH); //dehum light on
  digitalWrite(25, HIGH); //dehum pos voltage
delay(4000);
  digitalWrite(23, LOW); //dehum circuit off
  digitalWrite(24, LOW); //dehum light off
  digitalWrite(25, LOW); //defum negative voltage
lcd.clear();

//Dehum Run -- Defrost Cycle
lcd.setCursor(0,0);
lcd.println("Pin 27 Open");
lcd.setCursor(0,1);
lcd.println("Dehumidify (-)");
  digitalWrite(23, HIGH); //dehum circuit on
  digitalWrite(24, HIGH); //dehum light on
  digitalWrite(26, HIGH); //hum light on
  digitalWrite(27, HIGH); //dehum neg voltage off
delay(4000);
  digitalWrite(23, LOW); //dehum circuit off
  digitalWrite(24, LOW); //dehum light off
  digitalWrite(26, LOW); //hum light off
  digitalWrite(27, LOW); //dehum neg voltage off
lcd.clear();

//Humdifier on
lcd.setCursor(0,0);
lcd.println("Pin 29 Open");
lcd.setCursor(0,1);
lcd.println("Humidier Run");
  digitalWrite(26, HIGH); //hum light off
  digitalWrite(29, HIGH); //humdifier circuit on
delay(4000);
  digitalWrite(26, LOW); //hum light off
  digitalWrite(29, LOW); //humifier curcuit off
lcd.clear();

//Heat Run
lcd.setCursor(0,0);
lcd.println("Pin 31 Open");
lcd.setCursor(0,1);
lcd.println("Heat Run");
  digitalWrite(28, HIGH);
  digitalWrite(31, HIGH);
delay(4000);
  digitalWrite(28, LOW);
  digitalWrite(31, LOW);
lcd.clear();

//Cool Run
lcd.setCursor(0,0);
lcd.println("Pin 33 Open");
lcd.setCursor(0,1);
lcd.println("Cool Run");
  digitalWrite(30, HIGH);
  digitalWrite(33, HIGH);
delay(4000);
  digitalWrite(30, LOW);
  digitalWrite(33, LOW);
lcd.clear();
}




//Logs output to serial display for debug purposes
void serialDisplay(float _temp, int _tempLow, int _tempHigh, float _humidity, int _humidityLow, int _humidityHigh, int _medianTemp, int _medianHumidity){
      Serial.print(_tempLow);
      Serial.print("/");
      Serial.print(_tempHigh);
      Serial.print("  ");
      Serial.print(_humidityLow);
      Serial.print("/");
      Serial.print(_humidityHigh);
      Serial.print( "\n" );
      Serial.print("T: ");
      Serial.print(((_temp * 9/5) + 32), 1); 
      Serial.print("    H: ");
      Serial.print( _humidity, 1 );
      Serial.print( "\n" );
      Serial.print("Avg T:");
      Serial.print(_medianTemp);
      Serial.print("Avg H:");
      Serial.print(_medianHumidity);
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );
      Serial.print( "\n" );      
  }

Not 100% sure (can't verify now).

static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );
  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {

measurement_timestamp is set to millis(). Immediately after that you check millis() minus measurement_timestamp; chances that the difference between the two is zero are about 100%.

sterretje:
Not 100% sure (can’t verify now).

static bool measure_environment( float *temperature, float *humidity )

{
 static unsigned long measurement_timestamp = millis( );
 /* Measure once every four seconds. */
 if( millis( ) - measurement_timestamp > 3000ul )
 {



*measurement_timestamp* is set to millis(). Immediately after that you check millis() minus measurement_timestamp; chances that the difference between the two is zero are about 100%.

Note that ‘measurement_timestamp’ is ‘static’ so it only gets initialized are startup time. It is not like a local variable that gets initialized each time through the function. That piece of code should work as expected.

static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );
  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {

This is working as expected. From what I read it was a basic pattern for using timers vs delays…

But I think I may have found my problem…

void serialDisplay(float _temp, int _tempLow, int _tempHigh, float _humidity, int _humidityLow, int _humidityHigh, int _medianTemp, int _medianHumidity){
.....
      Serial.print(((_temp * 9/5) + 32), 1); 
.....
};

This is displaying the value as fahrenheit and the pots scaled to a farenheit range.

      if (temperatureValue > medianTemp)
      {
         ....
      }
      else if (temperatureValue < medianTemp)
      {
          ....
      }else
      {
          ....
      }

but the conditional statement is using the raw values from the DHT22 which are celsius. I’ll report back when I get home and test this theory.