Slave Uno read for lcd.print from Master Mega. Tryhard

Hi all.

This is my third topic about same project but different subjects.

I am stucked on my project which will e-mail me, when 10 200L barrels were not changed for a certain time.

Arduino Mega 2560+ethernet shield +SD+ DS1307 ----> Master

Arduino Uno+lcd keypad shield ----> Slave Which are connected to each other via i2c.

Overlapping pins were arranged. Every component works with no problem except retouches and delay settings.

Now i need to see them from lcd keypad on slave Uno.

Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino was really educational, however i need datas FROM master TO slave for printing. I have a sensor[10] array which holds states of 10 digital pins. I need to use those states on slave.

https://www.arduino.cc/en/Tutorial/MasterWriter was also good ref, however i need to carry 10 digital inputs to another.

Thank you for these codaghetties from now…

Master.ino (10.4 KB)

SLAVE.ino (2.83 KB)

To optimize assistance from this forum, you need to provide MVCE (minimal, verifiable, complete, examples). Then post them in line using code tags. Large atttachments do not get many eyes on them.

Your master write code is not correct. You are only sending one byte sensor[10] which does not actually exist because the array index are 0-9. Because you are sending a byte array, the syntax is simple

{
  Wire.beginTransmission(8);
  //Wire.write(sensor[10]);
   Wire.write(sensor, 10); 
  Wire.endTransmission();
  delay(100);
  }

The slave receiving code is also not correct. receiveEvent() actually an interrupt handler and should be quick and simple. You can not do all the lcd printing and delays. When the data is received set a flag variable that new data is present, and act on that in loop. When the actions are complete, clear the flag and wait for new data.

You should also read the data into sensor not buf.

Arduino Mega 2560+ethernet shield +SD+ DS1307 ----> Master

Arduino Uno+lcd keypad shield ----> Slave Which are connected to each other via i2c.

What’s the reason to not connect the lcd keypad shield directly to the Mega 2560?

void receiveEvent(int howMany){
  for (byte i = 0; i<=9; i++){
  buf[i] = Wire.read();
  }
  if(sensor[0] == 1){
    lcd.setCursor(0,0);
    lcd.print("Azalan urun:");
    lcd.setCursor(0,1);
    lcd.print("Profi Forte");
    delay(2000);
    lcd.clear();

receiveEvent is called in interrupt context, so it must not call any function that needs interrupts to work. delay() depends on timer0 interrupts to work so a call to delay() will never return.

int sensor[10]={0,0,0,0,0,0,0,0,0,0};
...
  Wire.write(sensor[10]);

In an array with 10 elements as defined here there is no element with index 10 as you use in the Wire.write() call.

Cattledog, thank you i am going to try those now. Plus when i compile like this

Wire.write(sensor, 10); error says: no matching function for call to "TwoWire::write(int[10],int)" quick edit i changed int sensor[10] to byte sensor[10] like Cattledog said it is ok now

Pylon, because of the dynamic memory... SD library covers much. Than where am i going to write else if lcd.prints? And how exactly am i supposed to define the array that will be read by slave Uno?

Is it like this?

byte sensor[10]; sensor[0] = digitalRead(28); sensor[1] = digitalRead(29); . . .

I got this working with directly sending the array from master and read from slave; and i guess it is because of i am using one way communication and there is no request interrupt from master… I guess? And of course simplicity came from my each 1byte array data

Those are from slave side:

void loop(){
  for(int i = 0; i<=9; i++){
    sensor[i] = Wire.read();
    }
      if(sensor[0] == 1){
        lcd.setCursor(0,0);
.
.
.
void receiveEvent(int howMany){
  while(10 < Wire.available())
  {
    for(int i = 0; i<=9; i++){
    sensor[i] = Wire.read();
    }
  }
}

I got this working with directly sending the array from master and read from slave; and i guess it is because of i am using one way communication and there is no request interrupt from master… I guess?

Correct, there is only an onReceive() interrupt on the Slave, there is not an onRequest() interrupt to send back data to the Master.

Your data reading approach on the Slave seems wrong. You need to realize that the Wire library has receive and sending buffers

The master sends 10 bytes. It’s not clear to me how frequently it does that given the delays and complexity of the master code.

EDIT: Wire.available should be 10 so its not clear if you ever read any data in the ISR. Read the data in the ISR. Work with sensor data in loop without reading again.

The ISR receiveEvent lets at least two transmissions accumulate in the receive buffer with

while(10 < Wire.available())

Then you read 10 in the isr and essentially throw this data away.

The loop() reads another 10 bytes and you take some actions on the data it reads. How fast is the slave loop repeating? Are the 10 bytes guaranteed to be there?

Read 10 bytes into the sensor array in the receive event handler. Set a flag variable = true that new data is present.

Have code in loop that checks for that flag, and if new data is present, do something with it. Set the flag back to false.

void loop(){
  //for(int i = 0; i<=9; i++){
  //  sensor[i] = Wire.read();
  //  }

if(newData)
  {
     newData = false;

      if(sensor[0] == 1){
        lcd.setCursor(0,0);
.
.
.

Okey Catty you are my hero! Good old flags…

Revised code of slave is like:

bool newData = true;

.
.
.

void loop(){
 // for(int i = 0; i<=9; i++){
    if(newData)
    {
      newData = false;
      if(sensor[0] == 1){
        lcd.setCursor(0,0);

and ends up like:

void receiveEvent(int howMany){
  while(1 < Wire.available())
  {
    for(int i = 0; i<=9; i++){
    sensor[i] = Wire.read();
    newData = true;
    }
  }
}

Slave(lcd) reacts approximately 3-3.5 seconds after pin goes HIGH.

EDIT: System does not send email… Enlight me please
EDIT2: It did sent 1 e-mail only 1 and i could not understand why. Connection has been failing before and after that 1 mail.

EDIT: System does not send email... Enlight me please EDIT2: It did sent 1 e-mail only 1 and i could not understand why. Connection has been failing before and after that 1 mail.

I really think this deserves a new thread. Focus is now on the ethernet issues and not i2c. Focus is now on the Master.

But, you have some troubleshooting and work to do before bringing this back to the forum. Do not post the Master code 10+KB attachment and say "This doesn't send an email".

To optimize assistance from this forum, you need to provide MVCE (minimal, verifiable, complete, examples). Then post them in line using code tags. Large atttachments do not get many eyes on them.

You may want to leave out the i2c and SD and see if you can demonstrate the problem.

There is basic trouble shooting and debug that has to be done.

Can you write minimal code to reliably send an email saying "Hello" at some repeat interval?

When you are good here, you can get on to sending that simple message when the count[] conditions are met. You should use more Serial print statements to check the value of variables being tested.

When that is done, you can advance to the email of the more complex message.