I am working on a project. Where I am using two Arduino(UNO, Pro Mini), one ds1307 rtc, one 24LC512 eeprom, one Nokia5110 LCD, Some 4digit 7 segment.
In this project I do some calculation, read/write/send and receive data among two arduino , external eeprom using i2c communication.
For better understanding please see the picture :
In Arduino UNO:
I read time from ds1307 and some data(which i previously stored) from 24LC512 eeprom using same Analog pin A4,A5 using i2c communication with Wire.h library.
And also send the time to another Arduino pro mini via I2C which is connected to the Uno using A4 and A5.
Edit time using push button and show them nokia5110.
In Arduino Pro Mini:
Here i received data from master UNO and read some data from 24LC512 eeprom and the do some calculation.
Finally display the calculated value in 4 digit 7 segment.
All is working fine but when i added some new feature the code size is getting big.
And when i upload the code the two device working smoothly for some moment.
And then suddenly the master Hang.
Code of Master UNO:
#include <RTClib.h>
#include "Wire.h"
#define EEPROM_I2C_ADDRESS 0x50
RTC_DS1307 rtc;
int hour, minute, second, day, month, year;
static const char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup() {
Wire.begin();
rtc.begin();
if (! rtc.isrunning()) {
//Serial.println("RTC is NOT running!");
// Set the date and time at compile time
rtc.adjust(DateTime(__DATE__, __TIME__));
}
delay(1000);
//startMillis = millis();
}
void loop() {
DateTime now = rtc.now();
hour = now.hour();
minute = now.minute();
second = now.second();
day = now.day();
month = now.month();
year = now.year();
Wire.beginTransmission(9);//9 here is the address of the slave board
Wire.write(hour);//Transfers the value of potentiometer to the slave board
Wire.write(minute);
Wire.write(second);
Wire.write(day);
Wire.write(month);
Wire.write(lowByte(year));
Wire.write(highByte(year));
Wire.endTransmission();
homePage();
//delay(100);
//isButtonDown(btnEnt) == false;
if (isButtonDown(btnEnt) == true) {
menuHome();
}
delay(1000);
}
int readIndex(int index) {
int newStrLen = readAddress(index);
return newStrLen;
}
String read_String(int index) {
int len = readAddress(index);
char data[len + 1];
for (int i = 0; i < len; i++) {
data[i] = (char)readAddress(i + index + 1);
}
data[len] = '\0';
return String(data);
}
int writeString(int index, String data) {
int strLen = data.length() + 1;
char string[strLen];
data.toCharArray(string, strLen);
writeAddress(index, strLen);
int i = 0;
for (i = 0; i < strLen; i++) {
writeAddress(i + index + 1, string[i]);
}
writeAddress(index + i + 1, strLen);
return index + strLen + 1;
}
void writeAddress(int address, byte val) {
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
Wire.write(val);
Wire.endTransmission();
delay(5);
}
byte readAddress(int address) {
byte rData = 0xFF;
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
rData = Wire.read();
delay(10);
return rData;
}
Sketch uses
Code of slave Pro mini:
#include"Wire.h"
#define EEPROM_I2C_ADDRESS 0x50
void setup() {
Wire.begin(9);
//Wire.begin();
//Serial.begin(9600);
Wire.onReceive(receiveEvent);
delay(1000); // wait 1000 ms
};
void receiveEvent(int bytes) {
while (0 < Wire.available()) {
hour = Wire.read();
minute = Wire.read();
second = Wire.read();
day = Wire.read();
month = Wire.read();
year = Wire.read() | (Wire.read() << 8);
}
}
void loop() {
String data = read_String(1806);
int dataLen = data.length();
int dashIndex = data.lastIndexOf('-');
int comma = 1 + data.lastIndexOf(',');
String latitudeStr, longitudeStr, latiLongiStr;
latiLongiStr = data.substring(dashIndex + 1, dataLen);
latitudeStr = data.substring(dashIndex + 1, comma - 1);
longitudeStr = data.substring(comma, dataLen);
latitude = latitudeStr.toDouble();
longitude = longitudeStr.toDouble();
delay(1000);
};
int readIndex(int index) {
int newStrLen = readAddress(index);
return newStrLen;
}
String read_String(int index) {
int len = readAddress(index);
char data[len + 1];
for (int i = 0; i < len; i++) {
data[i] = (char)readAddress(i + index + 1);
}
data[len] = '\0';
return String(data);
}
int writeString(int index, String data) {
int strLen = data.length() + 1;
char string[strLen];
data.toCharArray(string, strLen);
writeAddress(index, strLen);
int i = 0;
for (i = 0; i < strLen; i++) {
writeAddress(i + index + 1, string[i]);
}
writeAddress(index + i + 1, strLen);
return index + strLen + 1;
}
void writeAddress(int address, byte val) {
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
Wire.write(val);
Wire.endTransmission();
delay(5);
}
byte readAddress(int address) {
byte rData = 0xFF;
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((int)(address >> 8)); // MSB
Wire.write((int)(address & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);
rData = Wire.read();
delay(10);
return rData;
}
Sketch uses
My question is:
- Why the master hang or stop responding?
- Is it for multiple I2C communication at a time using same pin(All of them use only A4 And A5 ) ?
- Is the code size is too big and at tha same time transmit data that's why hang or stop?
New problem: The UNO receive unknown value from ds1307.