Hello new to the forum and also new to arduino, my problem is when I add the Wire.begin to my code it cause the time not to display right on my LCD screen. When I take it out it works fine. I'm trying to add i2c to one mega and talk to another mega. I'm using the examples to make sure that they work and getting the same problem with the time, even throw I can talk to the other arduino. The time display is wrong
Is the LCD I2C as well. An address conflict perhaps? I can only guess as i can't see code or schematic.
no the Lcd is a CTE 7 inch that uses the UTFT library, how do you post parts of the code
In the row above the smileys is the # sign. copy your code and paste between the code] and [/code. It is often better to post the whole sketch because snippets may not have all the #include, #define, variable definitions, etc. and can make it harder to be of informed assistance.
Do you have an RTC on the I2C bus?
Yes the RTC is on the i2c buss
void setup()
{
Serial.begin(9600);
TCCR5A = B00101011; // Fast PWM change at OCR5A (Timer 5 - pins 44 & 45)
TCCR5B = B10001; // No Prescalering (Timer 5 - pins 44 & 45)
pinMode(ledPinSump, OUTPUT);
pinMode(ledPinBlue, OUTPUT);
pinMode(ledPinWhite, OUTPUT);
pinMode(ledPinRoyBlue, OUTPUT);
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinUV, OUTPUT);
pinMode(ledPinMoon, OUTPUT);
pinMode(HoodFansPWM, OUTPUT);
pinMode(SumpFanPWM, OUTPUT);
pinMode(HoodFansTranzPin, OUTPUT);
pinMode(SumpFanTranzPin, OUTPUT);
pinMode(tempHeatPin, OUTPUT);
pinMode(tempChillPin, OUTPUT);
pinMode(tempAlarmPin, OUTPUT);
pinMode(WaveMakerTop, OUTPUT);
pinMode(WaveMakerBottom, OUTPUT);
pinMode(autoFeeder, OUTPUT);
myGLCD.InitLCD(LANDSCAPE);
myGLCD.clrScr();
myTouch.InitTouch(LANDSCAPE);
myTouch.setPrecision(PREC_MEDIUM);
sensors.begin(); //start up temperature library
// set the resolution to 9 bit
sensors.setResolution(waterThermometer, 9);
sensors.setResolution(hoodThermometer, 9);
sensors.setResolution(sumpThermometer, 9);
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 464, 799, 478); //Bottom Bar
rtc.halt(false); //Set the clock to run-mode
min_cnt= (t.hour*60)+t.min;
ReadFromEEPROM();
LED_levels_output();
wave_output();
screenReturn();
screenSaver();
mainScreen(true);
}
and the loop
void loop()
{
t = rtc.getTime();
if ((myTouch.dataAvailable()) && (screenSaverCounter>=setScreenSaverTimer))
{ LEDtestTick = false;
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 464, 799, 478); //Bottom Bar
screenSaverCounter=0;
clearScreen();
mainScreen(true);
dispScreen=0;}
else
{ if (myTouch.dataAvailable())
{ processMyTouch();}}
if (waveMakerOff==false)
{ wave_output();}
else
{ PumpTstate=LOW;
PumpBstate=LOW;}
unsigned long currentMillis = millis();
if (currentMillis - previousMillisFive > 5000) //check time, temp and LED levels every 5s
{ previousMillisFive = currentMillis;
t = rtc.getTime();
feedingTimeOutput();
if (screenSaverCounter<setScreenSaverTimer)
{ TimeDateBar(); }
checkTempC();
OCR5A = 16000000.0 / (2*25000.0); //25kHz PWM - above our audible range so fans are quiet
OCR5B = 16000000.0 / (2*25000.0) * SumpPWM; //"SumpPWM" is the % duty cycle for pin 45
OCR5C = 16000000.0 / (2*25000.0) * HoodPWM; //"HoodPWM" is the % duty cycle for pin 44
min_cnt= (t.hour*60)+t.min;
LED_levels_output();
screenReturn();
screenSaver();
if ((dispScreen == 0) && (screenSaverCounter<setScreenSaverTimer))
{ mainScreen();}
}
}
and if I put Wire.begin after the Serial.print my time is messed up
wgraham:
and if I put Wire.begin after the Serial.print my time is messed up
I don't see Wire.begin in your code, nor Serial.print.
How about posting the code that fails, not some other part?
Or preferably, the whole thing? You can attach code you know.
I took Wire.begin out to make the display work. I can put it back in and post it. The code has about 6100 lines in it. Do you want me to post the whole thing
This is the beginning
//LIBRARIES
#include <UTFT.h>
#include <UTouch.h>
//#include <ITDB02_Graph16.h>
#include <avr/pgmspace.h>
//#include <ITDB02_Touch.h>
#include <Wire.h>
#include <EEPROM.h>
#include "writeAnything.h"
#include <DS1307.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//Default Controller Settings
boolean RECOM_RCD = true; //For Mean Well drivers change "true" to "false"
//LCD TOUCH PANEL and ITDB02 MEGA SHIELD v1.1
//(Mega Shield utilizes pins 5V, 3V3, GND, 2-6, 20-41, & (50-53 for SD Card))
UTFT myGLCD(CTE70, 38,39,40,41); // Remember to change the model parameter to suit your display module!
UTouch myTouch(6,5,4,3,2);
//Initialize the DS1307
DS1307 rtc(20, 21);
DS1307_RAM ramBuffer; //Declare a buffer for use
This is just adding Wire.begin();
void setup()
{
Serial.begin(9600);
Wire.begin();
TCCR5A = B00101011; // Fast PWM change at OCR5A (Timer 5 - pins 44 & 45)
TCCR5B = B10001; // No Prescalering (Timer 5 - pins 44 & 45)
pinMode(ledPinSump, OUTPUT);
pinMode(ledPinBlue, OUTPUT);
pinMode(ledPinWhite, OUTPUT);
pinMode(ledPinRoyBlue, OUTPUT);
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinUV, OUTPUT);
pinMode(ledPinMoon, OUTPUT);
pinMode(HoodFansPWM, OUTPUT);
pinMode(SumpFanPWM, OUTPUT);
pinMode(HoodFansTranzPin, OUTPUT);
pinMode(SumpFanTranzPin, OUTPUT);
pinMode(tempHeatPin, OUTPUT);
pinMode(tempChillPin, OUTPUT);
pinMode(tempAlarmPin, OUTPUT);
pinMode(WaveMakerTop, OUTPUT);
pinMode(WaveMakerBottom, OUTPUT);
pinMode(autoFeeder, OUTPUT);
myGLCD.InitLCD(LANDSCAPE);
myGLCD.clrScr();
myTouch.InitTouch(LANDSCAPE);
myTouch.setPrecision(PREC_MEDIUM);
sensors.begin(); //start up temperature library
// set the resolution to 9 bit
sensors.setResolution(waterThermometer, 9);
sensors.setResolution(hoodThermometer, 9);
sensors.setResolution(sumpThermometer, 9);
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 464, 799, 478); //Bottom Bar
rtc.halt(false); //Set the clock to run-mode
min_cnt= (t.hour*60)+t.min;
ReadFromEEPROM();
LED_levels_output();
wave_output();
screenReturn();
screenSaver();
mainScreen(true);
}
/*********************************** END of SETUP ************************************/
/********************************** BEGIN MAIN LOOP **********************************/
void loop()
{
t = rtc.getTime();
if ((myTouch.dataAvailable()) && (screenSaverCounter>=setScreenSaverTimer))
{ LEDtestTick = false;
myGLCD.setColor(64, 64, 64);
myGLCD.fillRect(0, 464, 799, 478); //Bottom Bar
screenSaverCounter=0;
clearScreen();
mainScreen(true);
dispScreen=0;}
else
{ if (myTouch.dataAvailable())
{ processMyTouch();}}
if (waveMakerOff==false)
{ wave_output();}
else
{ PumpTstate=LOW;
PumpBstate=LOW;}
unsigned long currentMillis = millis();
if (currentMillis - previousMillisFive > 5000) //check time, temp and LED levels every 5s
{ previousMillisFive = currentMillis;
t = rtc.getTime();
feedingTimeOutput();
if (screenSaverCounter<setScreenSaverTimer)
{ TimeDateBar(); }
checkTempC();
OCR5A = 16000000.0 / (2*25000.0); //25kHz PWM - above our audible range so fans are quiet
OCR5B = 16000000.0 / (2*25000.0) * SumpPWM; //"SumpPWM" is the % duty cycle for pin 45
OCR5C = 16000000.0 / (2*25000.0) * HoodPWM; //"HoodPWM" is the % duty cycle for pin 44
min_cnt= (t.hour*60)+t.min;
LED_levels_output();
screenReturn();
screenSaver();
if ((dispScreen == 0) && (screenSaverCounter<setScreenSaverTimer))
{ mainScreen();}
}
}
As I stated before it works fine until I uses the Wire.begin command
Serial.print was just to see the example code in the serial monitor
don't need it in my code everything is displayed to the LCD
Wire.begin () activates the internal pull-ups. Do you have external pull-up resistors on SDA/SCL?
Where did you get your DS1307.h from? Mine doesn't have a constructor that takes two arguments.
I got this code from a guy that we control our saltwater reef tank with. I don't have to put the other arduino on the i2c buss just by adding the Wire.begin in the code messes up the time and date and I can't change it. When I do hook up the other arduino to this one I can see the example code working with this one in the serail monitor, it just meeses up the date and time.
Here is the ds1307 library
/*
DS1307.cpp - Arduino library support for the DS1307 I2C Real-Time Clock
Copyright (C)2010 Henning Karlsen. All right reserved
You can find the latest version of the library at
http://www.henningkarlsen.com/electronics
This library has been made to easily interface and use the DS1307 RTC with
the Arduino without needing the Wire library.
If you make any modifications or improvements to the code, I would appreciate
that you share the code with me so that I might include it in the next release.
I can be contacted through http://www.henningkarlsen.com/electronics/contact.php
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "DS1307.h"
#define REG_SEC 0
#define REG_MIN 1
#define REG_HOUR 2
#define REG_DOW 3
#define REG_DATE 4
#define REG_MON 5
#define REG_YEAR 6
#define REG_CON 7
/* Public */
Time::Time()
{
this->year = 2010;
this->mon = 1;
this->date = 1;
this->hour = 0;
this->min = 0;
this->sec = 0;
this->dow = 5;
}
DS1307_RAM::DS1307_RAM()
{
for (int i=0; i<56; i++)
cell[i]=0;
}
DS1307::DS1307(uint8_t data_pin, uint8_t sclk_pin)
{
_sda_pin = data_pin;
_scl_pin = sclk_pin;
pinMode(_scl_pin, OUTPUT);
}
Time DS1307::getTime()
{
Time t;
_burstRead();
t.sec = _decode(_burstArray[0]);
t.min = _decode(_burstArray[1]);
t.hour = _decodeH(_burstArray[2]);
t.dow = _burstArray[3];
t.date = _decode(_burstArray[4]);
t.mon = _decode(_burstArray[5]);
t.year = _decodeY(_burstArray[6])+2000;
return t;
}
void DS1307::setTime(uint8_t hour, uint8_t min, uint8_t sec)
{
if (((hour>=0) && (hour<24)) && ((min>=0) && (min<60)) && ((sec>=0) && (sec<60)))
{
_writeRegister(REG_HOUR, _encode(hour));
_writeRegister(REG_MIN, _encode(min));
_writeRegister(REG_SEC, _encode(sec));
}
}
void DS1307::setDate(uint8_t date, uint8_t mon, uint16_t year)
{
if (((date>0) && (date<=31)) && ((mon>0) && (mon<=12)) && ((year>=2000) && (year<3000)))
{
year -= 2000;
_writeRegister(REG_YEAR, _encode(year));
_writeRegister(REG_MON, _encode(mon));
_writeRegister(REG_DATE, _encode(date));
}
}
void DS1307::setDOW(uint8_t dow)
{
if ((dow>0) && (dow<8))
_writeRegister(REG_DOW, dow);
}
char *DS1307::getTimeStr(uint8_t format)
{
char *output= "xxxxxxxx";
Time t;
t=getTime();
if (t.hour<10)
output[0]=48;
else
output[0]=char((t.hour / 10)+48);
output[1]=char((t.hour % 10)+48);
output[2]=58;
if (t.min<10)
output[3]=48;
else
output[3]=char((t.min / 10)+48);
output[4]=char((t.min % 10)+48);
output[5]=58;
if (format==FORMAT_SHORT)
output[5]=0;
else
{
if (t.sec<10)
output[6]=48;
else
output[6]=char((t.sec / 10)+48);
output[7]=char((t.sec % 10)+48);
output[8]=0;
}
return output;
}
char *DS1307::getDateStr(uint8_t slformat, uint8_t eformat, char divider)
{
char *output= "xxxxxxxxxx";
int yr, offset;
Time t;
t=getTime();
switch (eformat)
{
case FORMAT_LITTLEENDIAN:
if (t.date<10)
output[0]=48;
else
output[0]=char((t.date / 10)+48);
output[1]=char((t.date % 10)+48);
output[2]=divider;
if (t.mon<10)
output[3]=48;
else
output[3]=char((t.mon / 10)+48);
output[4]=char((t.mon % 10)+48);
output[5]=divider;
if (slformat==FORMAT_SHORT)
{
yr=t.year-2000;
if (yr<10)
output[6]=48;
else
output[6]=char((yr / 10)+48);
output[7]=char((yr % 10)+48);
output[8]=0;
}
else
{
yr=t.year;
output[6]=char((yr / 1000)+48);
output[7]=char(((yr % 1000) / 100)+48);
output[8]=char(((yr % 100) / 10)+48);
output[9]=char((yr % 10)+48);
output[10]=0;
}
break;
case FORMAT_BIGENDIAN:
if (slformat==FORMAT_SHORT)
offset=0;
else
offset=2;
if (slformat==FORMAT_SHORT)
{
yr=t.year-2000;
if (yr<10)
output[0]=48;
else
output[0]=char((yr / 10)+48);
output[1]=char((yr % 10)+48);
output[2]=divider;
}
else
{
yr=t.year;
output[0]=char((yr / 1000)+48);
output[1]=char(((yr % 1000) / 100)+48);
output[2]=char(((yr % 100) / 10)+48);
output[3]=char((yr % 10)+48);
output[4]=divider;
}
if (t.mon<10)
output[3+offset]=48;
else
output[3+offset]=char((t.mon / 10)+48);
output[4+offset]=char((t.mon % 10)+48);
output[5+offset]=divider;
if (t.date<10)
output[6+offset]=48;
else
output[6+offset]=char((t.date / 10)+48);
output[7+offset]=char((t.date % 10)+48);
output[8+offset]=0;
break;
case FORMAT_MIDDLEENDIAN:
if (t.mon<10)
output[0]=48;
else
output[0]=char((t.mon / 10)+48);
output[1]=char((t.mon % 10)+48);
output[2]=divider;
if (t.date<10)
output[3]=48;
else
output[3]=char((t.date / 10)+48);
output[4]=char((t.date % 10)+48);
output[5]=divider;
if (slformat==FORMAT_SHORT)
{
yr=t.year-2000;
if (yr<10)
output[6]=48;
else
output[6]=char((yr / 10)+48);
output[7]=char((yr % 10)+48);
output[8]=0;
}
else
{
yr=t.year;
output[6]=char((yr / 1000)+48);
output[7]=char(((yr % 1000) / 100)+48);
output[8]=char(((yr % 100) / 10)+48);
output[9]=char((yr % 10)+48);
output[10]=0;
}
break;
}
return output;
}
char *DS1307::getDOWStr(uint8_t format)
{
char *output= "xxxxxxxxx";
Time t;
t=getTime();
switch (t.dow)
{
case MONDAY:
output="Monday";
break;
case TUESDAY:
output="Tuesday";
break;
case WEDNESDAY:
output="Wednesday";
break;
case THURSDAY:
output="Thursday";
break;
case FRIDAY:
output="Friday";
break;
case SATURDAY:
output="Saturday";
break;
case SUNDAY:
output="Sunday";
break;
}
if (format==FORMAT_SHORT)
output[3]=0;
return output;
}
char *DS1307::getMonthStr(uint8_t format)
{
char *output= "xxxxxxxxx";
Time t;
t=getTime();
switch (t.mon)
{
case 1:
output="JAN";
break;
case 2:
output="FEB";
break;
case 3:
output="MAR";
break;
case 4:
output="APR";
break;
case 5:
output="MAY";
break;
case 6:
output="JUN";
break;
case 7:
output="JLY";
break;
case 8:
output="AUG";
break;
case 9:
output="SEP";
break;
case 10:
output="OCT";
break;
case 11:
output="NOV";
break;
case 12:
output="DEC";
break;
}
if (format==FORMAT_SHORT)
output[3]=0;
return output;
}
void DS1307::halt(bool enable)
{
uint8_t _reg = _readRegister(REG_SEC);
_reg &= ~(1 << 7);
_reg |= (enable << 7);
_writeRegister(REG_SEC, _reg);
}
void DS1307::setOutput(bool enable)
{
uint8_t _reg = _readRegister(REG_CON);
_reg &= ~(1 << 7);
_reg |= (enable << 7);
_writeRegister(REG_CON, _reg);
}
void DS1307::enableSQW(bool enable)
{
uint8_t _reg = _readRegister(REG_CON);
_reg &= ~(1 << 4);
_reg |= (enable << 4);
_writeRegister(REG_CON, _reg);
}
void DS1307::setSQWRate(int rate)
{
uint8_t _reg = _readRegister(REG_CON);
_reg &= ~(3);
_reg |= (rate);
_writeRegister(REG_CON, _reg);
}
/* Private */
void DS1307::_sendStart(byte addr)
{
pinMode(_sda_pin, OUTPUT);
digitalWrite(_sda_pin, HIGH);
digitalWrite(_scl_pin, HIGH);
digitalWrite(_sda_pin, LOW);
digitalWrite(_scl_pin, LOW);
shiftOut(_sda_pin, _scl_pin, MSBFIRST, addr);
}
void DS1307::_sendStop()
{
pinMode(_sda_pin, OUTPUT);
digitalWrite(_sda_pin, LOW);
digitalWrite(_scl_pin, HIGH);
digitalWrite(_sda_pin, HIGH);
pinMode(_sda_pin, INPUT);
}
void DS1307::_sendNack()
{
pinMode(_sda_pin, OUTPUT);
digitalWrite(_scl_pin, LOW);
digitalWrite(_sda_pin, HIGH);
digitalWrite(_scl_pin, HIGH);
digitalWrite(_scl_pin, LOW);
pinMode(_sda_pin, INPUT);
}
void DS1307::_sendAck()
{
pinMode(_sda_pin, OUTPUT);
digitalWrite(_scl_pin, LOW);
digitalWrite(_sda_pin, LOW);
digitalWrite(_scl_pin, HIGH);
digitalWrite(_scl_pin, LOW);
pinMode(_sda_pin, INPUT);
}
void DS1307::_waitForAck()
{
pinMode(_sda_pin, INPUT);
digitalWrite(_scl_pin, HIGH);
while (_sda_pin==LOW) {}
digitalWrite(_scl_pin, LOW);
}
uint8_t DS1307::_readByte()
{
pinMode(_sda_pin, INPUT);
uint8_t value = 0;
uint8_t currentBit = 0;
for (int i = 0; i < 8; ++i)
{
digitalWrite(_scl_pin, HIGH);
currentBit = digitalRead(_sda_pin);
value |= (currentBit << 7-i);
delayMicroseconds(1);
digitalWrite(_scl_pin, LOW);
}
return value;
}
void DS1307::_writeByte(uint8_t value)
{
pinMode(_sda_pin, OUTPUT);
shiftOut(_sda_pin, _scl_pin, MSBFIRST, value);
}
uint8_t DS1307::_readRegister(uint8_t reg)
{
uint8_t readValue=0;
_sendStart(DS1307_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_sendStop();
_sendStart(DS1307_ADDR_R);
_waitForAck();
readValue = _readByte();
_sendNack();
_sendStop();
return readValue;
}
void DS1307::_writeRegister(uint8_t reg, uint8_t value)
{
_sendStart(DS1307_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_writeByte(value);
_waitForAck();
_sendStop();
}
void DS1307::_burstRead()
{
_sendStart(DS1307_ADDR_W);
_waitForAck();
_writeByte(0);
_waitForAck();
_sendStop();
_sendStart(DS1307_ADDR_R);
_waitForAck();
for (int i=0; i<8; i++)
{
_burstArray[i] = _readByte();
if (i<7)
_sendAck();
else
_sendNack();
}
_sendStop();
}
uint8_t DS1307::_decode(uint8_t value)
{
uint8_t decoded = value & 127;
decoded = (decoded & 15) + 10 * ((decoded & (15 << 4)) >> 4);
return decoded;
}
uint8_t DS1307::_decodeH(uint8_t value)
{
if (value & 128)
value = (value & 15) + (12 * ((value & 32) >> 5));
else
value = (value & 15) + (10 * ((value & 48) >> 4));
return value;
}
uint8_t DS1307::_decodeY(uint8_t value)
{
uint8_t decoded = (value & 15) + 10 * ((value & (15 << 4)) >> 4);
return decoded;
}
uint8_t DS1307::_encode(uint8_t value)
{
uint8_t encoded = ((value / 10) << 4) + (value % 10);
return encoded;
}
void DS1307::writeBuffer(DS1307_RAM r)
{
_sendStart(DS1307_ADDR_W);
_waitForAck();
_writeByte(8);
_waitForAck();
for (int i=0; i<56; i++)
{
_writeByte(r.cell[i]);
_waitForAck();
}
_sendStop();
}
DS1307_RAM DS1307::readBuffer()
{
DS1307_RAM r;
_sendStart(DS1307_ADDR_W);
_waitForAck();
_writeByte(8);
_waitForAck();
_sendStop();
_sendStart(DS1307_ADDR_R);
_waitForAck();
for (int i=0; i<56; i++)
{
r.cell[i] = _readByte();
if (i<55)
_sendAck();
else
_sendNack();
}
_sendStop();
return r;
}
void DS1307::poke(uint8_t addr, uint8_t value)
{
if ((addr >=0) && (addr<=55))
{
addr += 8;
_sendStart(DS1307_ADDR_W);
_waitForAck();
_writeByte(addr);
_waitForAck();
_writeByte(value);
_waitForAck();
_sendStop();
}
}
uint8_t DS1307::peek(uint8_t addr)
{
if ((addr >=0) && (addr<=55))
{
uint8_t readValue;
addr += 8;
_sendStart(DS1307_ADDR_W);
_waitForAck();
_writeByte(addr);
_waitForAck();
_sendStop();
_sendStart(DS1307_ADDR_R);
_waitForAck();
readValue = _readByte();
_sendNack();
_sendStop();
return readValue;
}
else
return 0;
}
Let know if this helps
I just want to add i2c ph and network to the code
You can attach files, you don't have to split them into small pieces.
Good morning,
What I have found out is that I took the example analog clock program and I add the wire library to it and put Wire.begin in the void setup and it do the same thing. Has anybody got the clock to work with the wire command. You don't even have to hook anything up to it, just put in that library and command.
Thanks
I always do a Wire.begin before using the clock. It is the sensible thing to do.
That's a very, very strange DS1307 library you posted. Where did you get it?
It uses bit-banging for I2C which explains your problems completely. Once you do Wire.begin the hardware takes over the SDA/SCL pins completely. The "bit banged" version will stop working.
Use a DS1307 library that uses the Wire library. Shorter, simpler and more reliable.
Here, for example from the Adafruit tutorial:
Yes I have found that out. Thanks for your help
The library is Henning Karlsen