1

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.

  • I don't have the library to hand, but I think reading requires you to request a certain number of bytes from an I2C address. You don't appear to be doing that. Suggest you look more closely at a working I2C library and see how they do it. – Code Gorilla Feb 1 '17 at 16:08
  • You're requesting 6 bytes when you should be requesting 20, if I understand it correctly, and there is no code in the Master to write to v. – Mark Smith Feb 3 '17 at 7:51
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.