Hello,
I’m trying to code my first program and have had some success, learning the syntax is a doozie… However, the issue I currently have is trying to get a fan to turn on based upon the temperature of a DS18B20 sensor. If I load the DS18B20 example sketch it reads correctly; If I load my sketch next it reads correctly. But if I lose power the sensor reads 180.00. If I reload my sketch first it still reads 180.00 So again, I loaded the example sketch and it worked, then my sketch and it works again. But I have to repeat this process every time. I have double checked my wiring, have the 4.7k resistor between the data and 5v. I have also tried 2 other sensors as well and the problem persists. It is way to consistent for me to believe that it is a hardware issue. I basically copied the example DS18B20 sketch and pasted it into mine but maybe I missed something? Any help would be much appreciated.
#include "RTClib.h"
#include "Wire.h"
#include "LiquidCrystal_I2C.h"
#include <TimeAlarms.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//Set the pins on the I2C chip used for LCD connections
//ADDR,EN,R/W,RS,D4,D5,D6,D7
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7); // 0x27 is the default I2C bus address
// Data wire is plugged into pin 2 on the Arduinoq
#define ONE_WIRE_BUS 13
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
RTC_DS3231 RTC;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int redLedOne = 3; //PWM pin
const int redLedTwo = 5; //PWM pin
const int blueLed = 9; //PWM pin
const int whiteLed = 11; //PWM pin
int minTemp = 78; //temp to turn fan off
int maxTemp = 82; //temp to turn fan on
const int TIP120pin = 12; //FAN PIN
boolean a = LOW, b = HIGH;
// the setup routine runs once when you press reset:
void setup() {
lcd.begin (20, 4); //20x4 lcd module
lcd.setBacklightPin(3, POSITIVE); // BL, BL_POL
lcd.setBacklight(HIGH);
Wire.begin();
sensors.begin();
Serial.begin(9600);
delay(3000);
if (! RTC.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (RTC.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
pinMode(3, OUTPUT); // declare pin 3 to be an output
pinMode(5, OUTPUT); // declare pin 5 to be an output
pinMode(9, OUTPUT); // declare pin 9 to be an output
pinMode(11, OUTPUT); // declare pin 11 to be an output
pinMode(53, OUTPUT); // TIP120 for fan control
}
void loop(void)
{
// Get the current time
DateTime now = RTC.now();
lcd.setCursor(0, 0);
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(' ');
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print(sensors.getTempFByIndex(0)); // Why "byIndex"?
Serial.println(sensors.getTempFByIndex(0)); // Why "byIndex"?
fanControl();
if ((now.hour() * 100) >= 500 && (now.hour() * 100) + now.minute() <= 1600) {
lightsOn();
} else {
lightsOff();
}
}
void lightsOn() {
digitalWrite(3, HIGH);
digitalWrite(5, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
lcd.setCursor(0, 2);
lcd.print("Lights are on.");
}
void lightsOff() {
digitalWrite(3, LOW);
digitalWrite(5, LOW);
digitalWrite(9, LOW);
digitalWrite(11, LOW);
lcd.setCursor(0, 2);
lcd.print("Lights are off.");
}
void fanControl() {
int temp = sensors.getTempFByIndex(0);
if (temp >= maxTemp && a == LOW)
{
digitalWrite(TIP120pin, HIGH);
a = HIGH;
b = LOW;
}
else if (temp <= minTemp && b == LOW)
{
digitalWrite(TIP120pin, LOW);
a = LOW;
b = HIGH;
}
}