Working project, added 18b20, got random crashes, check my sketch

My project was working fine, but since I added 18b20 I got random crashes, so I'm thinking if everything was OK before I should stop looking for h/w problem and let some1 check my sketch, maybe I missed something or I did something wrong in the code.

I hope you don't mind if the sketch is a bit messy, but I'm newb to coding and electronics
sketch_apr23a.ino (6.0 KB)

I'm using mega 2560, DHT22, lcd16x2, RTC1307, 2 and 4 channel relays

In the Arduino IDE, use Ctrl T to format your code then copy the complete sketch.

Use the </> button from the ‘reply menu’ to attach the copied sketch.

Why to you call lcd.begin() every 10 minutes?

Alarm.timerRepeat(10 * 60, LCDflash);

LCD get stuck sometimes and show blank fields, so it gets reset every 10mins

Sry my mistake :slight_smile:

#include <DallasTemperature.h>
#include <OneWire.h>
#include <Wire.h>
#include "DHT.h"
#include "LiquidCrystal.h"
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define DHTPIN 8
#define DHTTYPE DHT22
DHT sensor(DHTPIN, DHTTYPE);
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h>
AlarmId alarmRepeat;
AlarmId timerRepeat;
int ONE_WIRE_BUS = 34;      // 18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

const int vent = 33;        // ex. fan 1
const int vent4 = 35;       //mix fan
const int humy = 37;        // was humidifier now ex. fan 2
const int wat = 39;         // water pump
const int vent2 = 30;       //12v fan
const int vent3 = 32;       //12v fans


float maxTemp = 29.60;
float minTemp = 29.40;
float maxHumy = 77.50;
float minHumy = 72.00;

float maxtv2 = 29.40;
float mintv2 = 29.10;
float maxhv2 = 75.00;
float minhv2 = 70.00;

float maxtv3 = 25.60;
float mintv3 = 25.00;
float maxhv3 = 66.00;
float minhv3 = 60.00;

float maxtv4 = 26.20;
float mintv4 = 25.90;
float maxhv4 = 74.80;
float minhv4 = 71.00;

float maxHumy1 = 29.80;
float minHumy1 = 29.50;

void setup() {
  lcd.begin(16, 2);
  sensor.begin();
  sensors.begin();
  lcd.clear();
  digitalWrite(vent, HIGH);
  pinMode(vent, OUTPUT);
  digitalWrite(vent2, HIGH);
  pinMode(vent2, OUTPUT);
  digitalWrite(vent3, HIGH);
  pinMode(vent3, OUTPUT);
  digitalWrite(vent4, HIGH);
  pinMode(vent4, OUTPUT);
  digitalWrite(humy, HIGH);
  pinMode(humy, OUTPUT);
  digitalWrite(wat, HIGH);
  pinMode(wat, OUTPUT);

  Serial.begin(9600);
  while (!Serial) ;
  setSyncProvider(RTC.get);
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

  Alarm.alarmRepeat(8, 4, 50, wat1on);
  Alarm.alarmRepeat(8, 4, 57, wat1off);
  Alarm.alarmRepeat(8, 5, 0, wat1on);
  Alarm.alarmRepeat(8, 9, 10, wat1off);

  Alarm.alarmRepeat(10, 4, 50, wat1on);
  Alarm.alarmRepeat(10, 4, 57, wat1off);
  Alarm.alarmRepeat(10, 5, 0, wat1on);
  Alarm.alarmRepeat(10, 7, 55, wat1off);

  Alarm.alarmRepeat(12, 4, 50, wat1on);
  Alarm.alarmRepeat(12, 4, 57, wat1off);
  Alarm.alarmRepeat(12, 5, 0, wat1on);
  Alarm.alarmRepeat(12, 7, 55, wat1off);

  Alarm.alarmRepeat(14, 4, 50, wat1on);
  Alarm.alarmRepeat(14, 4, 57, wat1off);
  Alarm.alarmRepeat(14, 5, 0, wat1on);
  Alarm.alarmRepeat(14, 8, 5, wat1off);

  Alarm.alarmRepeat(16, 4, 50, wat1on);
  Alarm.alarmRepeat(16, 4, 57, wat1off);
  Alarm.alarmRepeat(16, 5, 0, wat1on);
  Alarm.alarmRepeat(16, 8, 5, wat1off);

  Alarm.alarmRepeat(18, 4, 50, wat1on);
  Alarm.alarmRepeat(18, 4, 57, wat1off);
  Alarm.alarmRepeat(18, 5, 0, wat1on);
  Alarm.alarmRepeat(18, 8, 5, wat1off);

  Alarm.alarmRepeat(20, 4, 50, wat1on);
  Alarm.alarmRepeat(20, 4, 57, wat1off);
  Alarm.alarmRepeat(20, 5, 0, wat1on);
  Alarm.alarmRepeat(20, 6, 45, wat1off);

  Alarm.timerRepeat(10 * 60, LCDflash);
}

void wat1on() {
  Serial.println("wat1on");
  digitalWrite(wat, LOW);
}
void wat1off() {
  Serial.println("wat1off");
  digitalWrite(wat, HIGH);
}
void LCDflash() {
  Serial.println("LCD");
  lcd.begin(16, 2);
}
void digitalClockDisplay() {
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
void loop() {
  float t = sensor.readTemperature();
  float h = sensor.readHumidity();
  h = map(h, 21.8, 91.2, 15.6, 77.6);
  // Checking if the sensor is sending values or not
  if (isnan(t) || isnan(h)) {
    lcd.print("Failed");
    return;
  }
  digitalClockDisplay();
  sensors.requestTemperatures();
  float tmp = sensors.getTempCByIndex(0);
  Alarm.delay(1000); // wait one second between clock display

  lcd.setCursor(0, 0);
  lcd.print("T");
  lcd.print(t);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("H");
  lcd.print(h);
  lcd.print("%");
  lcd.setCursor(8, 0);
  if (digitalRead(vent))
    lcd.print("Off");
  else
    lcd.print("On ");
  lcd.setCursor(8, 1);
  if (digitalRead(humy))
    lcd.print("Off");
  else
    lcd.print("On ");
  lcd.setCursor(12, 0);
  if (digitalRead(vent2))
    lcd.print("Off");
  else
    lcd.print("On ");
  lcd.setCursor(12, 1);   //NEW
  if (digitalRead(vent3))
    lcd.print("Off");
  else
    lcd.print("On ");

  if (digitalRead(vent)) {
    if (t > maxTemp || h > maxHumy) {
      digitalWrite(vent, LOW);
    }
    if (t > maxTemp && h < minHumy) {
      digitalWrite(vent, LOW);
    }
    if (t < minTemp && h > maxHumy) {
      digitalWrite(vent, LOW);
    }
  }
  else if (t < minTemp && h < minHumy) {
    digitalWrite(vent, HIGH);
  }

  if (digitalRead(vent2)) {
    if (t > maxtv2 || h > maxhv2) {
      digitalWrite(vent2, LOW);
    }
    if (t > maxtv2 && h < minhv2) {
      digitalWrite(vent2, LOW);
    }
    if (t < mintv2 && h > maxhv2) {
      digitalWrite(vent2, LOW);
    }
  }
  else if (t < mintv2 && h < minhv2) {
    digitalWrite(vent2, HIGH);
  }

  if (digitalRead(vent3)) {
    if (t > maxtv3 || h > maxhv3) {
      digitalWrite(vent3, LOW);
    }
    if (t > maxtv3 && h < minhv3) {
      digitalWrite(vent3, LOW);
    }
    if (t < mintv3 && h > maxhv3) {
      digitalWrite(vent3, LOW);
    }
  }
  else if (t < mintv3 && h < minhv3) {
    digitalWrite(vent3, HIGH);
  }

  if (digitalRead(vent4)) {
    if (tmp > maxtv4 || h > maxhv4) {
      digitalWrite(vent4, LOW);
    }
    if (tmp > maxtv4 && h < minhv4) {
      digitalWrite(vent4, LOW);
    }
    if (tmp < mintv4 && h > maxhv4) {
      digitalWrite(vent4, LOW);
    }
  }
  else if (tmp < mintv4 && h < minhv4) {
    digitalWrite(vent4, HIGH);
  }

  if (t > maxHumy1) {
    digitalWrite(humy, LOW);
  }
  else if (t < minHumy1) {

    digitalWrite(humy, HIGH);
  }

}
double map(double x, double in_min, double in_max, double out_min, double out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

The arguments of the ‘map’ function are ‘type’ long.


  else
  lcd.print("On ");
  lcd.setCursor(12, 0);

Did you mean:

  else
{
  lcd.print("On ");
  lcd.setCursor(12, 0);
}

I'm sorry I need to research about map and type long, not sure what are you pointing at

About {} I picked up that trick from older member from here, that it can be done without under some circumstances

My sketch worked fine without 18b, onewire and dallas I just don't know if some of the code could interfere, I can post up previous working code if needed, but it's not that much different

Here’s the whole ‘map’ function

long map(long x, long in_min, long in_max, long out_min, long out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

OP has a custom map() function at the end of the file

double map(double x, double in_min, double in_max, double out_min, double out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

can you describe what a crash looks like?

If that happend to me, I would run just the 18b20 code, for a day or so and see if it was 'stable'.

Hey, sorry for bothering you all
I have removed 18b20 and found out it has to do something with h/w, I think relays are dead, octocoupling might be damaged
I have some kind of EMI that spikes DC voltage and lead to Arduino crash. . .

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.