2 things, something's not correct as the temp is flashing every second or so, which in turn somehow effects the seconds on the time on the top row, that change every 2 or 3 seconds, but keeps up time. Also, is there any way to make the temp printout just the degrees with no decimal places
Im using a Arduino Mega with the Freetronics Display Shiels mounted on it. I have a DS18B20 and an RTC on a Breadboard with Jumpers
Here's my code
Code: Select all
// Libraries
#include <Wire.h>
#define DS3232_I2C_ADDRESS 0x68
#include <OneWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
#include <DallasTemperature.h> //will find addresses of sensors automatically
const char degree = 223; //This will give us the degree symbol after the temp
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
// Data wire from DS18B20 to Arduino Digital PIn 31
#define ONE_WIRE_BUS 31
// Setup oneWire instance to communicate with devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int n = 1;
void setup()
{
Wire.begin();
lcd.begin(16, 2);
// set the initial time here:
// DS3232 seconds, minutes, hours, day, date, month, year
//setDS3232time(0, 32, 17, 3, 21, 6, 16);
}
void setDS3232time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3232
Wire.beginTransmission(DS3232_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3232time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3232_I2C_ADDRESS);
Wire.write(0); // set DS3232 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3232_I2C_ADDRESS, 7);
// request seven bytes of data from DS3232 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3232
readDS3232time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send the data to the LCD shield
lcd.clear();
lcd.setCursor(4,0);
lcd.print(hour, DEC);
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.setCursor(0,1);
switch(dayOfWeek)
{
case 1:
lcd.print("Sun");
break;
case 2:
lcd.print("Mon");
break;
case 3:
lcd.print("Tue");
break;
case 4:
lcd.print("Wed");
break;
case 5:
lcd.print("Thu");
break;
case 6:
lcd.print("Fri");
break;
case 7:
lcd.print("Sat");
break;
}
lcd.print(" ");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(month, DEC);
lcd.print("/");
lcd.print(year, DEC);
sensors.begin();
}
void readtemp(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures(); // Get temps
lcd.setCursor ( 12, 1 ); //Print Room temp on bottom line
lcd.print(sensors.getTempCByIndex(0)); //Use this if you want C
//lcd.print(sensors.getTempCByIndex(0)* 9/5 * .54); //convert to F
}
void loop()
{
displayTime(); // display the real-time clock time on the LCD,
delay(1000); // every second
readtemp();
delay (1000);//reads temp every .5 second
}
Chris