Serial print freezing

Hey all, so I have and Arduino Mega 2560 r3 which runs fine for a seemingly random period of time (minutes, hours, sometimes days), then the serial monitor (putty as well) freezes. The program is still running (reoccurring events are unaffected) but nothing on the serial, and uploads are unsuccessful. If I close the aruino software and unplug/replug usb then it works.

I have the arduino reading temperatures, actuating relays, and displaying the temperatures. Given that the serial monitor constitutes a third of my desired functions, this is quite annoying. Any thoughts or ideas will be greatly appreciated.

The problem lies in line 27.

That's just a guess.

Please read the sticky threads at the top of the section.

Here’s my code (part 1)

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Time.h>  
#include <dht.h>

dht DHT;

//Time Declarations
#define statehour 11
#define stateminute 38

//Min and Max temps

//Day Hot Hide
#define DHHmax 83.8
#define DHHmin 78
//Day Hot Side
#define DHSmax 95
#define DHSmin 85
//Day Cold Side
#define DCSmax 80
#define DCSmin 70
//Night Hot Side
#define NHSmax 95
#define NHSmin 85
//Night Cold Side
#define NCSmax 85
#define NCSmin 75
//Night Hot Hide
#define NHHmax 85
#define NHHmin 76


//oneWire sensors definitions
#define temp_bit 12
#define pin 22

//Relay pin definitions
#define dayrelay 31 // relay1
#define nightrelay 32 //relay 2
#define ceramicrelay 33 //relay 3
#define uthrelay 34 //relay 4
#define fogrelay 35 //relay5
#define fanrelay 39 //relay 9

#define dhtPin 47 //pin connected to data line of dht22

//oneWire and Dallas calls
OneWire oneWire(pin);
DallasTemperature sensors(&oneWire);

//oneWire device adresses
DeviceAddress HotHideTemp = { 0x28, 0x80, 0x33, 0x84, 0x04, 0x00, 0x00, 0x77 };
DeviceAddress ColdSideTemp = { 0x28, 0x69, 0xFD, 0x83, 0x04, 0x00, 0x00, 0xCE };
DeviceAddress HotSideTemp = { 0x28, 0x8D, 0x3F, 0x84, 0x04, 0x00, 0x00, 0x1C };


//Global Variables

int counter;
int internalcounter;
int fogcontrol;

//Relay Modes
int daymode = 0;
int nightmode = 0;
int ceramicmode = 1;
int uthmode = 1;
int fogmode = 0;
int fanmode = 0;

//Subroutine Modes
//Day Hot Hide
int dhh2hMode = 0;
int dhh2cMode = 0;
//Day Hot Side
int dhs2hMode = 0;
int dhs2cMode = 0;
//Day Cold Side
int dcs2hMode = 0;
//Night Hot Side
int nhs2hMode = 0;
int nhs2cMode = 0;
//Night Cold Side
int ncs2hMode = 0;
int ncs2cMode = 0;
//Night Hot Hide
int nhh2hMode = 0;
int nhh2cMode = 0;

int humidityCheckMode = 0;

int daytime = 0;
int nighttime = 0;

//Min Max Averages
float DHHavrg;
float DHSavrg;
float DCSavrg;
float NHSavrg;
float NCSavrg;
float NHHavrg;


void setup(void)
{
  Serial.begin(9600);
  
  sensors.begin();
  
  setTime(statehour, stateminute, 00, 27, 01, 2013);
  
  //Locate oneWire Devices and Check Power Mode
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
  
  Serial.print("Parasite power is: "); 
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
  
  //Get oneWire Device Addresses
  if (!sensors.getAddress(HotHideTemp, 0)) Serial.println("Unable to find address for Device 0");
  if (!sensors.getAddress(ColdSideTemp, 1)) Serial.println("Unable to find address for Device 1");
  if (!sensors.getAddress(HotSideTemp, 2)) Serial.println("Unable to find address for Device 2");

  
  //Print oneWire Addresses
  Serial.print("Device 0 Address: ");
  printAddress(HotHideTemp);
  Serial.println();

  Serial.print("Device 1 Address: ");
  printAddress(ColdSideTemp);
  Serial.println();
  
  Serial.print("Device 2 Address: ");
  printAddress(HotSideTemp);
  Serial.println();
  
  //Set oneWire Device Resolutions
  sensors.setResolution(HotHideTemp, temp_bit);
  sensors.setResolution(ColdSideTemp, temp_bit);
  sensors.setResolution(HotSideTemp, temp_bit);
  
  //Print oneWire Device Resolutions
  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(HotHideTemp), DEC); 
  Serial.println();

  Serial.print("Device 1 Resolution: ");
  Serial.print(sensors.getResolution(ColdSideTemp), DEC); 
  Serial.println();
  
  Serial.print("Device 2 Resolution: ");
  Serial.println(sensors.getResolution(HotSideTemp), DEC); 
  Serial.println();
  
  //Min Max Average Calculations
  
  //Day Hot Hide
  DHHavrg = DHHmax + DHHmin;
  DHHavrg /= 2;
  
  //Day Hot Side
  DHSavrg = DHSmax + DHSmin;
  DHSavrg /= 2;
  
  //Day Cold Side
  DCSavrg = DCSmax + DCSmin;
  DCSavrg /= 2;
  
  //Night Hot Side
  NHSavrg = NHSmax + NHSmin;
  NHSavrg /= 2;
  
  //Night Cold Side
  NCSavrg = NCSmax + NCSmin;
  NCSavrg /= 2;
  
  //Night Hot Hide
  NHHavrg = NHHmax + NHHmin;
  NHHavrg /= 2;
  
  
  //Relay Pin Initializations
  pinMode(dayrelay, OUTPUT);
  pinMode(nightrelay, OUTPUT);
  pinMode(ceramicrelay, OUTPUT);
  pinMode(uthrelay, OUTPUT);
  pinMode(fogrelay, OUTPUT);
  pinMode(fanrelay, OUTPUT);
  
  
  //Quescent State
  digitalWrite(dayrelay, HIGH);
  digitalWrite(nightrelay, HIGH);
  digitalWrite(ceramicrelay, LOW);
  digitalWrite(uthrelay, LOW);
  digitalWrite(fogrelay, HIGH);
  digitalWrite(fanrelay, HIGH);
 
}//--------------------------------------------

When it freezes can it be restarted by pressing the arduino's reset button?

Grumpy_Mike:
When it freezes can it be restarted by pressing the arduino’s reset button?

It restarts, but doesn’t fix the problem.

I'd say the problem is with USB drivers on the computer not the Arduino.

Code part 2

void loop(void)
{
  sensors.requestTemperatures();
    
  time_t t = now();
  
  counter++;
  
  float hottempavrg;
  
  //DHT22 Vars
  float tempF;
  float humid;
  
  //Get oneWire Temps
  float hottempC = sensors.getTempC(HotSideTemp);
  float hottempF = DallasTemperature::toFahrenheit(hottempC);
  
  delay(1000);
  
  float coldtempC = sensors.getTempC(ColdSideTemp);
  float coldtempF = DallasTemperature::toFahrenheit(coldtempC);
  
  delay(1000);
  
  float hothidetempC = sensors.getTempC(HotHideTemp);
  float hothidetempF = DallasTemperature::toFahrenheit(hothidetempC);
  
  delay(1000);
  
  // Read DHT22 Temp
  Serial.print("DHT22, \t");
  int chk = DHT.read22(dhtPin);
  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }
  
  delay(1000);
  
  //Import DHT Temp and Convert C to F
  tempF = DHT.temperature * 9;
  tempF = tempF / 5;
  tempF = tempF + 32;
  
  //Import humidity
  humid = DHT.humidity;
  
  //Average Hot Side Temps
  hottempavrg = tempF + hottempF;
  hottempavrg = hottempavrg / 2;

  //Print time, counter, temps and humidity
  
  Serial.println(counter);
  
  Serial.print(hour(t));
  Serial.print(":");
  Serial.println(minute(t));
 
  Serial.print("Hot Hide Temp: ");
  Serial.print(hothidetempF);
  Serial.println(" F");
  
  Serial.print("Hot Side Temps: Average: ");
  Serial.print(hottempavrg);
  Serial.print(" F ");
  
  Serial.print("oneWire: ");
  Serial.print(hottempF);
  Serial.print(" F ");
  
  Serial.print("DHT22: ");
  Serial.print(tempF);
  Serial.println(" F ");
  
  Serial.print("Cold Side Temp: ");
  Serial.print(coldtempF);
  Serial.println(" F");
  
  Serial.print("DHT22 Relative Humidity: ");
  Serial.print(humid);
  Serial.println("%");
  
  
  
  //Determine Day or Night
  if(hour(t) >= 7 && hour(t) < 19)
  {
    daytime = 1;
    nighttime = 0;
  }
  
  if(hour(t) >= 19 || hour(t) < 7)
  {
    nighttime = 1;
    daytime = 0;
  }
  
  
  //Make Day or Night
  if (daytime)
  {
    digitalWrite(dayrelay, LOW);
    daymode = 1;
    
    if (nightmode != 3)
    {
      digitalWrite(nightrelay, HIGH);
      nightmode = 0;
    }
  }
  
  if (nighttime)
  {
    digitalWrite(nightrelay, LOW);
    nightmode = 1;
    digitalWrite(dayrelay, HIGH);
    daymode = 0;
  }
  
  
  //Fog Timer
  
  if (daytime)
  {
    //Determine if Current Time is within the first 15 min
    if (minute(t) >= 0 && minute(t) < 15)
    {
      fogcontrol = 1;
    }
  
    //Determine if Current Time is between 30 and 45
    else if (minute(t) >= 30 && minute(t) < 45)
    {
      fogcontrol = 1;
    }
  
    else
    {
      fogcontrol = 0;
    }
  }
  
  if (nighttime)
  {
    //Night fog timer is set for 5 minute runs every fifteen minutes
    //On the hour
    if (minute(t) >= 0 && minute(t) < 10)
    {
      fogcontrol = 1;
    }
    else if (minute(t) >= 30 && minute(t) < 40)
    {
      fogcontrol = 1;
    }
    else
    {
      fogcontrol = 0;
    }
  }
  
  //Turn on fog on the hour and half hour.
  if (fogcontrol && fogmode == 0)
  { 
    digitalWrite(fogrelay, LOW);
    fogmode = 1;
  }
  
  //Turn off fog 15 after, and 45 after.
  if (!fogcontrol && fogmode == 1)
  {
      digitalWrite(fogrelay, HIGH);
      fogmode = 0;
  }
  
  
  //Subroutine Calls
  
  //Day Hot Hide
  DHH2H (hothidetempF);
  DHH2C (hothidetempF);
  
  //Day Hot Side
  DHS2H (hottempavrg, hothidetempF);
  DHS2C (hottempavrg);
  
  //Day Cold Side
  DCS2H (coldtempF, hothidetempF);
  
  //Night Hot Side
  NHS2H (hottempavrg);
  NHS2C (hottempavrg);
  
  //Night Cold Side
  NCS2H (coldtempF);
  NCS2H (coldtempF);
  
  //Night Hot Hide
  NHH2H (hothidetempF);
  NHH2C (hothidetempF);
   
  printrunningrelays();
  printrunningprograms();
  
  delay(56000);
}//------------------------------------------


//
//FUNCTION DECLARATIONS
//


void printrunningrelays(void)
{
  Serial.println("Relays Currently On:");
  if (daymode == 1)
  {
    Serial.println("Day Light");
  }
  if (nightmode == 1)
  {
  Serial.println("Night Light");
  }
  if (nightmode == 3)
  {
    Serial.println("Night Program");
  }
  if (ceramicmode == 1)
  {
    Serial.println("Ceramic Light");
  }
  if (ceramicmode == 3)
  {
    Serial.println("Ceramic Program");
  }
  if (uthmode == 1)
  {
    Serial.println("UTHeater");
  }
  if (uthmode == 3)
  {
    Serial.println("UTH Program");
  }
  if (fanmode == 1)
  {
    Serial.println("Fan");
  }
  if (fanmode == 3)
  {
    Serial.println("Fan Program");
  }
  if (fogmode == 1)
  {
    Serial.println("Fogger");
  }
  if (fogmode == 3)
  {
    Serial.println("Fog Program");
  }
  
  Serial.println();
}//--------------------------------------------


void printrunningprograms (void)
{
  if (dhh2hMode)
  {
    Serial.println("Day Hot Hide Too Hot");
  }
  if (dhh2cMode)
  {
    Serial.println("Day Hot Hide Too Cold");
  }
  if (dhs2hMode)
  {
    Serial.println("Day Hot Side Too Hot");
  }
  if (dhs2cMode)
  {
    Serial.println("Day Hot Side Too Cold");
  }
  if (dcs2hMode)
  {
    Serial.println("Day Cold Side Too Hot");
  }
  if (nhs2hMode)
  {
    Serial.println("Night Hot Side Too Hot");
  }
  if (nhs2cMode)
  {
    Serial.println("Night Hot Side Too Cold");
  }
  if (ncs2hMode)
  {
    Serial.println("Night Cold Side Too Hot");
  }
  if (ncs2cMode)
  {
    Serial.println("Night Cold Side Too Cold");
  }
  if (nhh2hMode)
  {
    Serial.println("Night Hot Hide Too Hot");
  }
  if (nhh2cMode)
  {
    Serial.println("Night Hot Hide Too Cold");
  }
  if (humidityCheckMode)
  {
    Serial.println("Humidity Low");
  }
  
  Serial.println();
  Serial.println();
}//---------------------------------------------

Shpaget:
I'd say the problem is with USB drivers on the computer not the Arduino.

Thats a good point, have they come out with windows 8 drivers yet?

Last but not least

void DHH2H (float hothide)
{
  if (!dhh2hMode && daytime)
  {
    if (hothide >= DHHmax)
    {
      digitalWrite(uthrelay, HIGH);
      uthmode = 0;
      
      dhh2hMode = 1;
    }
  }
  
  else if (dhh2hMode)
  {
    if (hothide < DHHavrg)
    {
      digitalWrite(uthrelay, LOW);
      uthmode = 1;
      
      dhh2hMode = 0;
    }
  }
}//--------------------------------------


void DHH2C (float hothide)
{
  if (!dhh2cMode && daytime)
  {
    if (hothide < DHHmin)
    {
      digitalWrite(nightrelay, LOW);
      nightmode = 3;
      
      digitalWrite(uthrelay, LOW);
      uthmode = 1;
      
      dhh2cMode = 1;
    }
  }
  
  else if (dhh2cMode)
  {
    if (hothide >= DHHavrg)
    {
      digitalWrite(nightrelay, HIGH);
      nightmode = 0;
      
      dhh2cMode = 0;
    }
    if (nighttime) dhh2cMode = 0;
  }
}//-----------------------------------------


void DHS2H (float hotside, float hothide)
{
  if (!dhs2hMode && daytime)
  {
    if (hotside >= DHSmax && hothide >= DHHavrg)
    {
      digitalWrite(fanrelay, LOW);
      fanmode = 1;
      
      dhs2hMode = 1;
    }
  }
  
  else if (dhs2hMode)
  {
    if (hotside < DHSavrg)
    {
      digitalWrite(fanrelay, HIGH);
      fanmode = 0;
      
      dhs2hMode = 0;
    }
  }
}//-------------------------------------------


void DHS2C (float hotside)
{
  if (!dhs2cMode && daytime)
  {
    if (hotside < DHSmin)
    {
      digitalWrite(fanrelay, HIGH);
      fanmode = 2;
      
      dhs2cMode = 1;
    }
  }
  
  else if (dhs2cMode)
  {
    if (hotside >= DHSavrg)
    {
      fanmode = 0;
      
      dhs2cMode = 0;
    }
  }
}//---------------------------------------------


void DCS2H (float coldside, float hothide)
{
  if (!dcs2hMode && daytime)
  {
    if (coldside >= DCSmax && hothide >= DHHavrg)
    {
      digitalWrite(ceramicrelay, HIGH);
      ceramicmode = 0;
      
      if (fanmode != 2)
      {
        digitalWrite(fanrelay, LOW);
        fanmode = 1;
      }
      
      dcs2hMode = 1;
    }
  }
  
  else if (dcs2hMode)
  {
    if (coldside < DCSavrg)
    {
      digitalWrite(ceramicrelay, LOW);
      ceramicmode = 1;
      digitalWrite(fanrelay, HIGH);
      fanmode = 0;
      
      dcs2hMode = 0;
    }
  }
}//----------------------------------------------


void NHS2H (float hotside)
{
  if (!nhs2hMode && nighttime)
  {
    if (hotside >= NHSmax)
    {
      digitalWrite(uthrelay, HIGH);
      uthmode = 2;
      
      if (fanmode != 2)
      {
        digitalWrite(fanrelay, LOW);
        fanmode = 1;
        
        nhs2hMode = 1;
      }
    }
  }
  
  else if (nhs2hMode)
  {
    if (hotside < NHSavrg)
    {
      digitalWrite(uthrelay, LOW);
      uthmode = 1;
      
      if (fanmode == 1)
      {
        digitalWrite(fanrelay, HIGH);
        fanmode = 0;
      }
      
      nhs2hMode = 0;
    }
  }
}//---------------------------------------------


void NHS2C (float hotside)
{
  if (!nhs2cMode && nighttime)
  {
    if (hotside < NHSmin)
    {
      digitalWrite(uthrelay, LOW);
      uthmode = 3;
      digitalWrite(fanrelay, HIGH);
      fanmode = 2;
      
      nhs2cMode = 1;
    }
  }
  
  else if (nhs2cMode)
  {
    if (hotside >= NHSavrg)
    {
      uthmode = 1;
      fanmode = 0;
      
      nhs2cMode = 0;
    }
  }
}//--------------------------------------------


void NCS2H (float coldside)
{
  if (!ncs2hMode && nighttime)
  {
    if (coldside >= NCSmax)
    {
      digitalWrite(ceramicrelay, HIGH);
      ceramicmode = 0;
      
      if (fanmode != 2)
      {
        digitalWrite(fanrelay, LOW);
        fanmode = 1;
      }
      
      ncs2hMode = 1;
    }
  }
  
  else if (ncs2hMode)
  {
    if (coldside < NCSavrg)
    {
      digitalWrite(ceramicrelay, LOW);
      ceramicmode = 1;
      
      if (fanmode == 1)
      {
        digitalWrite(fanrelay, HIGH);
        fanmode = 0;
      }
      
      ncs2hMode = 0;
    }
  }
}//--------------------------------------------


void NCS2C (float coldside)
{
  if (!ncs2cMode && nighttime)
  {
    if (coldside < NCSmin)
    {
      digitalWrite(ceramicrelay, LOW);
      ceramicmode = 1;
      digitalWrite(fanrelay, HIGH);
      fanmode = 2;
      
      ncs2cMode = 1;
    }
  }
  
  else if (ncs2cMode)
  {
    if (coldside >= NCSavrg)
    {
      fanmode = 0;
      
      ncs2cMode = 0;
    }
  }
}//----------------------------------------------


void NHH2H (float hothide)
{
  if (!nhh2hMode && nighttime)
  {
    if (hothide >= NHHmax)
    {
      if (uthmode != 3)
      {
        digitalWrite(uthrelay, HIGH);
        uthmode = 0;
      }
      
      nhh2hMode = 1;
    }
  }
  
  else if (nhh2hMode)
  {
    if (hothide < NHHavrg)
    {
      if (uthmode == 0)
      {
        digitalWrite(uthrelay, LOW);
        uthmode = 1;
      }
      
      nhh2hMode = 0;
    }
  }
}//-----------------------------------------------


void NHH2C (float hothide)
{
  if (!nhh2cMode && nighttime)
  {
    if (hothide < NHHmin && uthmode != 2)
    {
        digitalWrite(uthrelay, LOW);
        uthmode = 1;
    }
  }
}//------------------------------------------------


void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}//---------------------------------------------


void humidityCheck(float RH)
{
  if (!humidityCheckMode)
  {
    if (RH < 30 && daytime)
    {
      digitalWrite(fogrelay, LOW);
      fogmode = 3;
      
      internalcounter = 0;
      humidityCheckMode = 1;
    }
  }
  
  else
  {
    if (RH >= 60 || internalcounter == 30)
    {
      digitalWrite(fogrelay, HIGH);
      fogmode = 0;
      
      humidityCheckMode = 0;
    }
    internalcounter ++;
  }
  
  if (RH > 80)
  {
    digitalWrite(fogrelay, HIGH);
    fogmode = 2;
  }
  
  if (fogmode == 2 && RH < 50)
  {
    fogmode = 0;
  }
}//-------------------------------------------------

I'd be looking at a bundle of F() macros, but it's a Mega, so perhaps not such a problem.
Good job you picked this problem up before the sketch got too big.

Please read the sticky threads at the top of the section.

So you didn't read the bit about posting long code then?

AWOL:
I'd be looking at a bundle of F() macros, but it's a Mega, so perhaps not such a problem.
Good job you picked this problem up before the sketch got too big.

Thank you. I'm not familiar with F() macros. :~ What does it do?

Grumpy_Mike:

Please read the sticky threads at the top of the section.

So you didn't read the bit about posting long code then?

I guess I missed it, sorry.

So I threw in a handful of F() macros in what I believe to be the problem area, so lets see if that helps. crossing fingers

So after throwing in a bunch of F() macros, the problem still persists.

Anyone know anything about windows 8 drivers? I haven't been able to find anything.

Here is the code all in one, including the new F() macros.

main3.txt (16.8 KB)

  //Get oneWire Device Addresses
  if (!sensors.getAddress(HotHideTemp, 0)) Serial.println("Unable to find address for Device 0");
  if (!sensors.getAddress(ColdSideTemp, 1)) Serial.println("Unable to find address for Device 1");
  if (!sensors.getAddress(HotSideTemp, 2)) Serial.println("Unable to find address for Device 2");

Why don't these have F()?

  switch (chk)
  {
    case DHTLIB_OK:  
		Serial.print("OK,\t"); 
		break;
    case DHTLIB_ERROR_CHECKSUM: 
		Serial.print("Checksum error,\t"); 
		break;
    case DHTLIB_ERROR_TIMEOUT: 
		Serial.print("Time out error,\t"); 
		break;
    default: 
		Serial.print("Unknown error,\t"); 
		break;
  }

Or these?

    daytime = 1;
    nighttime = 0;

It's either night or day, isn't it? Why do you need 2 ints to store one byte of data?

Thanks, I missed those when adding the F()s.

As for the night/day, a previous incarnation of this had the day temperature profiles starting a few hours before the day light gets turned on. I plan on reincorporating that later, since my snake goes to sleep early.

I plan on reincorporating that later, since my snake goes to sleep early.

We really ought to have a "quote of the day" topic

So after adding F() macros to every constant string, it still freezes on me. I have also updated my drivers and no change. Any more ideas?