I am having some trouble connecting my 2 Arduino Nanos with I2C. I have this configuration:
- SLAVE RECEIVER Nano with DS1037 and LCD 16x02 on I2C.
- MASTER SENDER Nano.
After some time the Slave receiver prints on LCD random chars. If I delete the line including library RTCLIB.h everything is working except the clock. Thanks for your help :D
Slave receiver code:
#include <Wire.h> // include la libreria
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
LiquidCrystal_I2C lcd(0x3E, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
RTC_DS1307 RTC; // orologio
static int anno;
static int giorno;
static int mese;
static int ora;
static int secondo;
static int oldSecondo;
static int minuto;
static int oldMinuto;
static int annoGrezzo;
//------------------------------------------------------------
// si ripete la union esattamente come nel master
union Scomp_float {
// si utilizza union x float
float temp;
char byte_s[4];
} S_float;
//------------------------------------------------------------
// si ripete la union esattamente come nel master
union Scomp_long {
// si utilizza union x long
long int mioL;
char byte_sL[4];
} S_long;
//------------------------------------------------------------
//------------- byte temporanei
byte f0 = 0; // contengono i byte ricevuti
byte f1 = 0;
byte f2 = 0;
byte f3 = 0;
byte a1 = 0;
//-------------------------------------------------------------
// qui si introdurranno i dati ricomposti dopo la trasmissione
long int masterLong = 0;
float masterFloat = 0;
int masterInt = 0;
//--------------------------------------------------------------
void setup() {
Wire.begin(80); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
lcd.begin(16, 2);
Serial.begin(9600); // start serial for output
//-----------orologio-----------
RTC.begin();
// Check to see if the RTC is keeping time. If it is, load the time from your computer.
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// This will reflect the time that your sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
//-----------fine orologio-----------
}
void loop() {
//------------------------------------------------------------
// si stampano i dati ricevuti e ricomposti
//------------------------------------------------------------
Serial.println(masterInt);
Serial.println(masterLong);
Serial.println(masterFloat);
DateTime now = RTC.now();
annoGrezzo = now.year();
anno = ( annoGrezzo % 100);
mese = now.month();
giorno = now.day();
ora = now.hour();
minuto = now.minute();
lcd.setCursor(0, 0);
lcd.print(masterInt);
lcd.setCursor(0, 1);
lcd.print(giorno);
lcd.print(" ");
lcd.print(mese);
lcd.print(" ");
lcd.print(anno);
lcd.print(" ");
lcd.print(ora);
lcd.print(":");
lcd.print(minuto);
delay (1000);
lcd.clear();
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
if (howMany == 2) {
// se in arrivo due dati
f0 = Wire.read(); // si leggono i byte
f1 = Wire.read();
masterInt = ((f1 << 8) | f0); // si ricompone l'intero
}
if (howMany == 4) {
// se in arrivo 4 dati è un float
f0 = Wire.read(); // si leggono i byte
f1 = Wire.read();
f2 = Wire.read();
f3 = Wire.read();
S_float.byte_s[0] = f0; // si ricompone il float
S_float.byte_s[1] = f1;
S_float.byte_s[2] = f2;
S_float.byte_s[3] = f3;
masterFloat = S_float.temp;
}
if (howMany == 5) {
// se in arrivo 5 dati è un long
f0 = Wire.read(); // si leggono i byte
f1 = Wire.read();
f2 = Wire.read();
f3 = Wire.read();
a1 = Wire.read();
S_long.byte_sL[0] = f0; // si ricompone il long
S_long.byte_sL[1] = f1;
S_long.byte_sL[2] = f2;
S_long.byte_sL[3] = f3;
masterLong = S_long.mioL;
}
}
Master sender code:
#include <Wire.h> // si include la libreria
//---------------------------------------------------------
union Scomp_float {
// si utilizza union x float
float temp;
char byte_s[4];
} S_float;
//---------------------------------------------------------
union Scomp_long {
// si utilizza union x long
long int mioL;
char byte_sL[4];
} S_long;
//----------------------------------------------------------
// questi sono i dati che si vogliono trasmettere
long int mioLong = 1234567801;
float mioFlo = 123456.29;
//----------------------------------------------------------
byte a1 = 0; // conterrà i primo byte dell'int
byte a2 = 0; // conterrà il secondo byte dell'int
byte f0 = 0; // contengono i byte del float e del long
byte f1 = 0;
byte f2 = 0;
byte f3 = 0;
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
}
void loop() {
int mioInt = random(30000);
//-----------------------------------------------------
// trasmissione di un intero ( 2 byte)
//-----------------------------------------------------
a1 = byte(mioInt); // si scompone l'intero in 2 byte
a2 = byte( mioInt >> 8);
Wire.beginTransmission(80); // inzia trasmissione allo slave
Wire.write(a1); // invia un byte
Wire.write(a2); // invia il secondo byte
Wire.endTransmission(); // stop transmitting
//------------------------------------------------------
delay (2000);
//------------------------------------------------------
// trasmissione intero lungo 4 byte
//------------------------------------------------------
S_long.mioL = mioLong; // si scompone in 4 byte
f0 = S_long.byte_sL[0];
f1 = S_long.byte_sL[1];
f2 = S_long.byte_sL[2];
f3 = S_long.byte_sL[3];
a1 = 1; // si trasmette questo byte solo per segnalare allo slave
// che stiamo trasmettendo un long
// una volta scomposto si trasmettono i 4 byte
Wire.beginTransmission(80); // trasmette allo slave
Wire.write(f0); // invia un byte
Wire.write(f1); //
Wire.write(f2); //
Wire.write(f3); // invia un byte
Wire.write(a1);
Wire.endTransmission(); // fine trasmissione
//-------------------------------------------------------
delay (2000);
//------------------------------------------------------
// trasmissione di un float 4 byte
//------------------------------------------------------
S_float.temp = mioFlo; // si scompone in 4 byte
f0 = S_float.byte_s[0];
f1 = S_float.byte_s[1];
f2 = S_float.byte_s[2];
f3 = S_float.byte_s[3];
// una volta scomposto si trasmettono i 4 byte
Wire.beginTransmission(80); // trasmette allo slave
Wire.write(f0); // invia un byte
Wire.write(f1); //
Wire.write(f2); //
Wire.write(f3); // invia un byte
Wire.endTransmission(); // fine trasmissione
//-------------------------------------------------------
delay (3000);
}
//------ end master ----------------------------------