Hello,
I am using the I2C backpack from Adafruit in SPI mode to drive an LCD panel. It works perfectly and I am quite familiar with this product having bought 50+ so far. This uses the LiquidCrystal library. I also use a DS1307 RTC module which is an I2C device, default address 0x68, connected to SDA/SCL on a Leonardo. The RTC works perfectly using RTClib.
The problem is when the code is combined. The RTC still reports the time correctly with Serial.print();, but the LCD remains blank.
LCD/Backpack connections are: (SPI jumper bridged)
- 5V to Arduino 5V pin
- GND to Arduino GND pin
- CLK to Digital 2
- DAT to Digital 3
- LAT to Digital 4
The RTC
- 5V to Arduino 5V pin
- GND to Arduino GND pin
- SCL to Leonardo SCL
- SDA to Leonardo SDA
Code attached
Any help appreciated!!!!!
// *****************************************************************************
// ** SGH_RTC_Alarm
// ** Timers, alarms and real time clock (RTC) example
// **
// ** Uses a DS1307 RTC module connected to I2C pins SDA/SCL.
// ** Default I2C addressing assumed
// ** --------------------------------------------------------------------------
// ** Coding by: Don Gibson info@SmartGreenHouse.org
// ** Based on original code from "Arduino Cookbook" Michael Margolis / O'Reilly Press
// ** Created: May 17 2013
// ** Version: 1.2
// ** Last update: added weekday alarm 2013/05/19
// ** Modified by: DG
// ** This code is public domain, but please always include the original header
// ** in any subsequent copies
// *****************************************************************************
// ** By default in timeAlarms.h there are 6 alarms declared
// ** to add more alarms, edit timeAlarms.h as follows:
// ** dtNBR_ALARMS 12 which increases the number of alarms to 12 (255 max)
// *****************************************************************************
/* The circuit:
* 5V to Arduino 5V pin
* GND to Arduino GND pin
* CLK to Digital 2
* DAT to Digital 3
* LAT to Digital 4
*/
#include <Wire.h>
#include <RTClib.h> //assumes SDA/SCL connections to RTC module
#include <Time.h>
#include <TimeAlarms.h>
#include "LiquidCrystal.h"
#define DS1307_I2C_ADDRESS 0x68 // the default I2C address of DS1307 RTC
int currentSeconds = 0;
RTC_DS1307 RTC; // create the RTC object instance
// Connect LCD via SPI. Data pin is #3, Clock is #2 and Latch is #4
// Relates to CATkit & Kitten pins: K2; K3; K1 for remote connection
LiquidCrystal lcd(3, 2, 4);
int iYear;
int iMonth;
int iDay;
int iHour;
int iMinute;
int iSecond;
int alarmOneInterval = 600; // seconds for the timer interval in alarmOne
int alarmTwoInterval = 60; // seconds for the timer interval in alarmTwo
boolean ON = 1;
boolean OFF = 0;
boolean waterState = OFF; // use waterState to ensure the valves are either ON or OFF and the
// status of the valve is always known
int solenoidPin = 7; // attach D7 to a 5v relay control signal to control the water valve
void setup()
{
Serial.begin(57600);
Wire.begin(); //set address here default is 0x68
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is not running - you may need to replace the battery or check connections");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
//RTC.adjust(DateTime(__DATE__, __TIME__)); //uncomment to set time for the 1st time using the RTC
DateTime now = RTC.now(); // get the current time from the RTC and load the following variables
iYear = int(now.year());
iMonth = int(now.month());
iDay = int(now.day());
iHour = int(now.hour());
iMinute = int(now.minute());
iSecond = int(now.second());
pinMode(solenoidPin, OUTPUT); // set the pin direction
setTime(iHour,iMinute,iSecond,iDay,iMonth,iYear); // this initialises the timer with .now()
Alarm.timerRepeat(alarmOneInterval, alarmOne); // timer triggers every alarmOneInterval
Alarm.timerRepeat(alarmTwoInterval, alarmTwo); // timer triggers every alarmTwoInterval
Alarm.alarmRepeat(10,0,0, dayAlarmOne); // alarm at 10:00:00 each day
Alarm.alarmRepeat(10,30,0, dayAlarmTwo); // alarm at 10:30:00 each day
Alarm.alarmRepeat(dowTuesday,20,30,0, weekAlarm); // alarm at 20:30:00 each Tuesday
// set up the LCD's number of rows and columns:
lcd.begin(20, 4);
lcd.setBacklight(HIGH);
// Print a message to the LCD.
lcd.print(" SmartGreenHouse!");
lcd.setCursor(0,1);
lcd.print("Using SPI protocol");
lcd.setCursor(0,3);
lcd.print("CAT5 cable upto 100m");
}
void alarmOne()
{
Serial.print("Alarm One triggered at : ");
Serial.print(" ");
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// do something
}
void alarmTwo()
{
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ..... at : ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// do something else
}
void dayAlarmOne()
{
Serial.print("Day alarm ONE triggered at : ");
DateTime now = RTC.now();
Serial.print(dayStr(weekday())); // print the name of the day
Serial.print(" ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
waterState= ON;
waterTimer(waterState); // turn on the water
}
void dayAlarmTwo()
{
Serial.print("Day alarm TWO triggered at : ");
DateTime now = RTC.now();
Serial.print(dayStr(weekday())); // print the name of the day
Serial.print(" ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
waterState = OFF;
waterTimer(waterState); // turn off the water
}
void weekAlarm()
{
Serial.print("Week alarm triggered at : ");
DateTime now = RTC.now();
Serial.print(dayStr(weekday())); // print the name of the day
Serial.print(" ==============> ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
// do something nice, once per week. Take your friend out for lunch perhaps?
}
void waterTimer(boolean action)
{
if (action)
{
digitalWrite(solenoidPin, HIGH); // turn on or off the water solenoid/relay
Serial.print("Water turned on at : ");
} else {
digitalWrite(solenoidPin, LOW); // turn on or off the water solenoid/relay
Serial.print("Water turned off at : ");
}
DateTime now = RTC.now();
Serial.print(dayStr(weekday())); // print the name of the day
Serial.print(" ===============> ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
}
void loop()
{
// do processing here
if(second() != currentSeconds) // just a loop to trigger the timer/alarm scheduler
{
currentSeconds = second();
Alarm.delay(1);
}
// now do something useful, like read sensors, send a Tweet, put on the coffee
// add action code to the alarm functions to take an action at a specific time or time interval
// you can also embed a timed event within another alarm function,
// e.g. turn on the lawn sprinkler for 10 minutes every Tuesday at 10:00 pm
// but remember to turn it off again!
// http://smartgreenhouse.org
}