I am trying to send 2 variables with wire.h
. I tried with this code:
MASTER RECEIVER:
#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;
char t[10] = {};
char v[10] = {};
void setup() {
Wire.begin(); // join i2c bus with address #4
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
//------------------------------------------------------------
Wire.requestFrom(8, 6);
int i = 0;
while (Wire.available()) {
Serial.println(i);
t[i] = Wire.read();
// every character that arrives it put in order in the empty array "t"
i = i + 1;
}
Serial.println(t);
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(t);
lcd.setCursor(5,0);
lcd.print(v);
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();
}
SLAVE SENDER
#include <Wire.h> // si include la libreria
//---------------------------------------------------------
char t[10];
char v[10];
void setup() {
Wire.begin(8);
// join i2c bus (address optional for master)
Wire.onRequest(requestEvent);
}
void loop() {
int mioInt = random(30000);
int mioInt2 = 1234;
dtostrf(mioInt, 3, 2, t);
//convers the float or integer to a string. (floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, empty array);
dtostrf(mioInt2, 3, 2, v);
//------------------------------------------------------
delay (500);
}
void requestEvent() {
Wire.write(t);
Wire.write(v);
}
I'm unable to receive v
in my master receiver. Thanks for your help.
1 Answer 1
I2C is 8-bit data bus, where address can be 7 bit or 10 bit, you can't send more than 8 bit at a time, if you want to send data more than 8 bit you will be sending them like this, for example let you need to send 16 bit data then you send 8 bit LSB first and then 8 bit MSB or vice versa as per your configuration.
Secondly I had noticed you code and noticed many errors, the major one is that you hadn't followed the rules of I2C protocol in your code. I am here attached a link for sample program of arduino i2c slave and master. Go through it you will get your answer.
v
.