Hi all, first post so hope this is the appropriate area.
I am new to C programming would like to build an environment controller. Initially I have bought an Arduino Micro to get started.
The setup I am having trouble with at the moment is a sparkfun DT1307 RTC module and a 16x2 LCD fitted with an Adafruit backpack that only works in SPI mode I'm afraid (Adafruit are sending a replacement) I have had the LCD correctly displaying the temperature with a TMP36 but cannot get the RTC up and running.
I noticed the RTClib include is black in the IDE but it is installed in the user libraries only.
The LCD is wired for SPI as In this Adafruit tutorial: Arduino SPI Use | I2C/SPI LCD Backpack | Adafruit Learning System
The DT1307 is wired as in this bildr tutorial: http://bildr.org/2011/03/ds1307-arduino/
The code below compiles, uploads, the LCD then blinks and turns off completely -
#include <LiquidCrystal.h>
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
LiquidCrystal lcd(3, 2, 4);
void setup() {
Serial.begin(9600);
Wire.begin();
RTC.begin();
lcd.begin(16, 2);
lcd.print("LCD Started..");
//lcd.noAutoscroll();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
delay(2000);
}
void loop() {
DateTime now = RTC.now();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Testing..");
lcd.setCursor(0,1);
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.print(" ");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
delay(1000);
}
This is the working code that displays the TMP36 correctly - I would like to integrate this with a working RTC code.
// An example of displaying temperature on LCD.
// Temp sensor is TMP36.
// TMP36 --- Arduino --- LCD.
// by MHLAB
// 5/2012
//
// References
// http://arduino.cc/en/Tutorial/LiquidCrystal
// http://www.ladyada.net/learn/sensors/tmp36.html
#include <LiquidCrystal.h>
#include <Wire.h>
#define aref_voltage 3.30 // To set the aref to ~3.3v
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Connect via SPI. Data pin is #3, Clock is #2 and Latch is #4
LiquidCrystal lcd(3, 2, 4);
// Variables to control the reading time interval and to average the reading:
int index = 0; // the index of the current reading
int readings = 0; // the readings from the analog input
unsigned long total = 0; // the sum of total readings
float average = 0.0; // the average of readings
float temp1 = 0.0; // temp oC of sensor 1
unsigned long time1 = 0; // Timing 1
unsigned long time2 = 0; // Timing 2
// constants won't change
const int numReadings = 1000; // sampling numbers for analog readings
const unsigned long timeInterval = 1000; // Setting for the interval of the output
// Pin numbers asignment TMP36 sensors
int inputPin1 = A1; // Read voltage of sensor tmp36
void setup()
{
analogReference(EXTERNAL); // To set the aref to something other than 5v
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD starting at [0,0]
lcd.setCursor(1, 0); // Move cursor to our second line
lcd.print("TEMP:");
}
void loop() {
// Get the time by millis()
time1 = millis();
// reading average ADC of sensor 1
average = averageTempAd(inputPin1);
// convert ADC and convert it to Celsius
temp1 = temperatureSensorTMP35(average);
// Print out to LCD
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(7, 0);
// print the number of seconds since reset:
lcd.print(temp1);
lcd.print(char(223));
lcd.print("C");
//lcd.print("/");
// Print out to LCD (oF)
// set the cursor to column 8, line 1
//lcd.setCursor(8, 1);
//lcd.print(temp1*9/5+32.0);
//lcd.print(char(223));
//lcd.print("F");
// Controll the data output timing: Using millis()
while(time1 + timeInterval > millis()){
// do nothing
}
}
// Average temp function
float averageTempAd(int pinNumber){
int aveADC;
while (index < numReadings){
readings = analogRead(pinNumber);
// add the reading to the total:
total= total + readings;
// advance to the next position in the array:
index = index + 1;
}
// Average AD number of temp
aveADC = total / numReadings;
// ...reset, wrap around to the beginning:
index = 0;
total = 0;
return aveADC;
}
// --- Convert to digit number to Temp oC ---
// TMP35 Temperature sensor calculation
// rawADC (10bit = 1024) to Temp (oC)
// Schematic:
// [Arduino] -- {TMP35}
// [Vcc (5 or 3.3v)] -- {Pin#1}
// [A1] -- {Pin#2}
// [Ground] -- {Pin#3}
// converting from 10 mv per degree wit 500 mV offset
// to degrees ((volatge - 500mV) times 100)
float temperatureSensorTMP35(float rawADC) {
float Temp; // Dual-Purpose variable to save space
Temp = rawADC*aref_voltage/1024.0; // convert from Digital to voltage.
Temp = (Temp - 0.5) * 100.0 ; // convert to oC
return Temp;
}